Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Silly Quz about conversion 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 "Silly Quz about conversion" Watch "Silly Quz about conversion" New topic
Author

Silly Quz about conversion

Jane Zheng
Greenhorn

Joined: Oct 17, 2000
Posts: 11
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.
Kevin Yip
Ranch Hand

Joined: Oct 17, 2000
Posts: 110
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
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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 Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Jane Zheng
Greenhorn

Joined: Oct 17, 2000
Posts: 11
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.
vijay krishna
Greenhorn

Joined: Oct 17, 2000
Posts: 8
I think you have got the "promotion" concept wrong. It will get "promoted" to serve the purpose of others.
It doesnt modify its type!
midrisi
Greenhorn

Joined: Oct 22, 2000
Posts: 7

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

Joined: Aug 30, 2000
Posts: 3141
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

Joined: Oct 17, 2000
Posts: 11
Hi, Guys:
Thanks for the info, it is clear now.....

Janes
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Silly Quz about conversion
 
Similar Threads
question from Sun Guoqiao's homepage
CHAR
Arrays
reference to doh is ambiguous
doubt on char assignment