• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

assignment operator

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nebody plz explain y this is valid?

char ch = '1' + '8';
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thak you awais, Reid and Jose.
Your replies really were of help to me.
-Asif
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic