| Author |
System.out.printf("%1$c",65536);
|
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
we know the range of char is 0 to 65535..then why does this compile ?
|
 |
Travis Hein
Ranch Hand
Joined: Jun 06, 2006
Posts: 161
|
|
well, the number is an integer expression. primative types don't have the elaborate boundary checking like arrays would.
so when integer this value is converted to a char internally, it is going to truncate the 17th bit, and render the remaining part that does fit into the char storage.
By that logic, i would expect these two statements to emit the same output.
|
Error: Keyboard not attached. Press F1 to continue.
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
primative types don't have the elaborate boundary checking like arrays would
what does this mean?
|
 |
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
|
|
|
He means if you had an array of 3 objects, and tried to do array[4], it would throw an exception. With primitives, however, if you go outside their bounds they simply drop the bits that are outside their range and use what is left.
|
SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
then why does char c=65536; doesnt compile?
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
and how does integer value is converted to char internally?
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
the cast is provided internally? is there any rule stating so?
|
 |
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
|
|
Ankur kothari wrote:the cast is provided internally? is there any rule stating so?
It seems like there is a cast, but I don't know. I'm searching now, but hoping some Java guru will pop in knowing what is up.
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
me too.....waiting for someone...
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Ankur kothari wrote:me too.....waiting for someone...
Perhaps you could clarify what your question is now. It isn't clear on what you know now.
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
am still looking for a rule which says about internal casting....because i know that internally casting is done
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
I don't think there's any such rule. I don't even think that "internal" casting is a concept in Java. Did you read that phrase somewhere?
|
 |
Edwin Dalorzo
Ranch Hand
Joined: Dec 31, 2004
Posts: 961
|
|
First of all, the compiler does not know that your printf expression is expecting a char character. The method actually being invoked in printf is:
Therefore, your 65536 are autimatically converted to an Integer object thanks to autoboxing.
|
 |
zheng li
Ranch Hand
Joined: Jun 16, 2009
Posts: 56
|
|
I got "?" from System.out.printf("%1$c",65536);
don't know why
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Edwin Dalorzo wrote:First of all, the compiler does not know that your printf expression is expecting a char character. The method actually being invoked in printf is:
Therefore, your 65536 are autimatically converted to an Integer object thanks to autoboxing.
Agreed. This question has nothing to do with casting -- and whether it should compile or not. The method is implemented as varargs of objects, so there is no way for the compiler to even detect that the type is different than expected. It must be done at runtime.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
zheng li wrote:I got "?" from System.out.printf("%1$c",65536);
don't know why
It is a "?" mark because there is no character font data for that unicode character. As for how printf() behaves when you pass an int to be formatted as a %c, the javadoc is generally the best resource.
http://java.sun.com/javase/6/docs/api/java/util/Formatter.html
Basically, when you pass a value that is larger than a char, it will be treated as a code point unicode.
If it is out of range, it will throw an exception. And the reason it doesn't throw an exception here, is because a code point unicode character is actually larger than 16 bits. It can span from 0 to 0x10FFFF.
Henry
|
 |
 |
|
|
subject: System.out.printf("%1$c",65536);
|
|
|