• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Silly Quz about conversion

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a snip code:
public class T{
public static void main(String []agrs){
char c = 'c';
int i;
i = ++c;
System.out.println(c);
System.out.println(i);
}
}
And here is the output:
d
100
I have no question regarding i and its output, but I hava no idea about the char c, why its output is d instead of 100? shouldn't it be converted to int automaticlly?
Thanks
Jane Z.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane,
You declared c as char. Why you expect it output an integer? Although I'm new to Java, I don't think this is correct for any programming languages.
Just my thought.
Kevin
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jane,
Kevin is right. The char type uses Unicode characters which all have numerical equivalents. The small letter 'c' is \u0099. ++c = \u0100 which is why 'int i' printed as 100 and char c printed as 'd'.
Hope that helps.
------------------
Jane
 
Jane Zheng
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I agree with your guys at that point. But what I try to figure out is that when char, short, or byte is ++ operand, then they should be "Promoted to an int". Which the meaning of the rule?
Now I just assume that if
char c ='c';
then ++c just return an int, but c will still remain in char, right?
Thanks
Jane Z.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have got the "promotion" concept wrong. It will get "promoted" to serve the purpose of others.
It doesnt modify its type!
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. public class T{
2. public static void main(String []agrs){
3. char c = 'c';
4. int i;
5. i = ++c;
6. System.out.println(c);
7. System.out.println(i);
8. }
9. }


As in the above code snippet you have intialised char c with 'c' and in the line 5, you are pre incrementing the c.In this case the java interpreter first type cast c variable to int type implicitly whose value is 99.And then inceremented it i.e. 99 + 1 =100 and assign to i variable in line 5.
In the line 6 you are printing the value of c which will become d whose value is 100. As java allow incremementing and decrementing oprations on string literals.
I think your query has been solved.



------------------
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jane,
JLS �15.15.1 Prefix Increment Operator
<code>
The type of the prefix expression is the type of the variable....
... the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion (�5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (�5.1.3) to the type of the variable before it is stored.
</code>
Think this explains why the type of 'c' remains as a char vs being changed to an int
Hope that helps.
------------------
Jane
[This message has been edited by Jane Griscti (edited October 22, 2000).]
 
Jane Zheng
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Guys:
Thanks for the info, it is clear now.....

Janes
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic