• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.out.printf("%1$c",65536);

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we know the range of char is 0 to 65535..then why does this compile ?
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
primative types don't have the elaborate boundary checking like arrays would


what does this mean?
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then why does char c=65536; doesnt compile?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and how does integer value is converted to char internally?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the cast is provided internally? is there any rule stating so?
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
me too.....waiting for someone...
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am still looking for a rule which says about internal casting....because i know that internally casting is done
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got "?" from System.out.printf("%1$c",65536);
don't know why
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic