File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assignment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assignment operator" Watch "assignment operator" New topic
Author

assignment operator

vinita Kh
Ranch Hand

Joined: Feb 19, 2002
Posts: 49
nebody plz explain y this is valid?

char ch = '1' + '8';
Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
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

Joined: May 02, 2002
Posts: 10
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

Joined: May 12, 2002
Posts: 69
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

Joined: Dec 13, 2000
Posts: 20
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

Joined: Mar 17, 2002
Posts: 7
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;


awais
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
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.


Reid - SCJP2 (April 2002)
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Asif Masood
Greenhorn

Joined: Dec 13, 2000
Posts: 20
Thak you awais, Reid and Jose.
Your replies really were of help to me.
-Asif
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assignment operator