Why can't I write ch=ch+1; instead of ch++; though they have same meaning
Asked Answered
E

3

7
package practicejava;

public class Query {

    public static void main(String[] args) {
        char ch = 66;
        System.out.println("character= " + ch);

        ch++;

        System.out.println("character = " + ch);

    }
}

Technically ch++; and ch=ch+1; are the same but why do I get an error when I write ch=ch+1; instead of ch++;?

Electrometer answered 15/12, 2018 at 9:57 Comment(4)
Welcome to Stack Overflow! Please have a look around, and read through the help center, in particular How do I ask a good question? "but why is here occur error" Always quote the exact error, rather than just saying you get an error. In this case, we can probably guess what error you're talking about, but you don't want to make the people trying to help you guess.Cage
It’s a confusing behaviour that has been taken over from C++ and its forerunners.Exalt
an int plus a char is an intBaresark
Note that Java is one of the few languages that actually retains a distinct character type from C, in most languages characters are actually strings and won't compile (or will throw a runtime error) if you try to treat them as integers.Stodge
U
6

You need to provide a cast in order to do that :

ch = (char) (ch + 1);

This is because the expression ch + 1 is is promoted (upcast) to an int. In order for you to reassign this expression to a char you need to explicitly downcast it.

Unsought answered 15/12, 2018 at 9:59 Comment(0)
P
5

By ch+1, the char ch will be promoted to int first, just like ((int)ch) + 1, so the result will be an int.

When you try assign an int(32 bit) back to a char(16 bit), it might loss accuracy, you need to do it explictly ch = (char)(ch + 1);


This is called Binary Numeric Promotion:

Binary numeric promotion is performed on the operands of certain operators:

...

The addition and subtraction operators for numeric types + and - (§15.18.2)

and it will perform

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

If either operand is of type double, the other is converted to double.

Otherwise, if either operand is of type float, the other is converted to float.

Otherwise, if either operand is of type long, the other is converted to long.

Otherwise, both operands are converted to type int.

Preciosa answered 15/12, 2018 at 10:0 Comment(0)
O
3

First of all, note that a char is 2 bytes large (16 bit), and an int is 32bit.

1. When typing ch++:

to apply the ++ operator, there is no type cast but the operator simply causes the bit represent of that char to increase by 1 to itself. Refer to JLS11 chapter 15.14.2,page 575:

The type of the postfix increment expression is the type of the variable.

2. When typing ch=ch+1:

ch is firstly casted to int, then it is added by 1(still an int), and the = is actually tring to cast the int which has 32bits into a char which has only 16 bits, note that this may lose accuracy. So without an explicitly cast, the compiler will complain about that, which is the cause of the error.

Odlo answered 15/12, 2018 at 10:12 Comment(4)
I did not understand the no. 1 point .if ch is casted into int when printing the value of ch after ch++; operation why it shows character instead of int.. Can You plz explain the first term detailsElectrometer
[sorry for my bad English ] I mean : suppose at first I assign 'A' into ch .after I do the ch++; operation. here if ch would cast into int and then add value 1 . here corresponding Unicode value of A which is 65 then add 1 .total 66 so after when I perform the action System.out.print("character "+ch); it should show the output like this : character 66 . that all I understood from your second comment . the I asked you that why the output is character B if ch is casted into int then add int value 1Electrometer
what does it mean by ''the bit represent of that char"Electrometer
By saying that I mean the value of that char, and that value is stored in the memory by a set of bits(16 bits)Odlo

© 2022 - 2024 — McMap. All rights reserved.