This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Downcasting and the value

 
nishant vats
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code
char s1 = (char)78890;
int i1 = s1;
System.out.println(i1);

output
13354


while downcasting there is modulo divison..
am nt able to get the above value
 
S.L.Narayanan
Ranch Hand
Posts: 431
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

The casting logic is very simple. For Char the maximum value that you can put is 65536. So if u cast 78890 to assign to Char it will go to 0 (LIKE CYCLIC after completing one cycle of 65536) and assign
13354 since 78890-65536=13354.

Similarly if u assign char c=(char)-100, it is equivalent to 65536-100=65436. This is the casting logic that takes place for 'char'. For int,byte, etc...also it is the same except that after completing a cycle it will go to the -Ve minimum.

Clear???
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a hint:
System.out.println(Character.MAX_VALUE + 13354);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic