Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assignment operator

 
vinita Kh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nebody plz explain y this is valid?

char ch = '1' + '8';
 
Gautam Sewani
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its a simple question:
would this work
byte b=20+30
yes it would
coz the addition is for constant values and the compiler can determine that the result would be in range of byte.
Thats the case with the char values as well.
But if u compile the following code:
char ch='2';
char ab='3';
char a=ch+ab
Then this wont compile,coz the addition is between variables and this will be subjected to binary numeric promotion.
 
Ana P
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char ch = '1' + '8';
System.out.println("ch= " + ch); // prints i
char ch2='2';
char ch3='1';
ch3 += ch2;
System.out.println("ch3= " + ch); // this also prints i
why so?
 
Eduard Manas
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ana,
check it out again, you should get the following:
1) char c='1'+'8' makes c='i'
2) char c='1'+'2' makes c='c'
What happens here is that the compiler will make an integer calculation and then allocate that value to the char "c".
In the first example it will calculate 49+56=106, which is the Unicode value of 'i'. In the second example it will calculate 49+50=99, which is the Unicode value of 'c'.
Eduard
 
Asif Masood
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am bit confused from the different behaviour for almost same expresion.

char ch2 = '2';
char ch3 = '1';
ch3 += ch2; //this works fine

But the following expression gives compiler error.
ch3 = ch3 + ch2;
Why it's so.
 
awais syed
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Asif Masood:
I am bit confused from the different behaviour for almost same expresion.

char ch2 = '2';
char ch3 = '1';
ch3 += ch2; //this works fine

But the following expression gives compiler error.
ch3 = ch3 + ch2;
Why it's so.

because java compiler reads
ch3 += ch2 as ch3 = (char)ch3 + ch2;
 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Asif Masood:
I am bit confused from the different behaviour for almost same expresion.
...
Why it's so.

Well, the hard-core answer is "because the Java language spec says so, and that is what you will be tested on". Practically, it is a mixture of language design (and hence arbitrary) and the difficulties in writing a compiler to make deductions about ranges of values based on variables and previous statements.
The issue you've spotted may seem inconsistent, but oh well, it is there. Think of "+=" as having more in common with "++" than a sequence of "a+b; a=b" statements. "++" doesn't cause promotion either.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an implicit cast in +=, thus no compiler error.
if "char3 + char3" no explicit cast exists . Thus it is promoted to int and compiler complains about assigning an int to a char.
 
Asif Masood
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thak you awais, Reid and Jose.
Your replies really were of help to me.
-Asif
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic