This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Downcasting and the value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Downcasting and the value" Watch "Downcasting and the value" New topic
Author

Downcasting and the value

nishant vats
Greenhorn

Joined: Feb 11, 2005
Posts: 23
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

Joined: Apr 01, 2005
Posts: 431
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???


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, NEXT--->IBM XML
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Here's a hint:
System.out.println(Character.MAX_VALUE + 13354);


Tony Morris
Java Q&A (FAQ, Trivia)
 
wood burning stoves
 
subject: Downcasting and the value
 
Similar Threads
a char and int interconversion doubt!
pRINTING AN OBJECT WITH NULL REFERENCE
pRINTING AN OBJECT WITH NULL REFERENCE
wierd problems..
overriding member fields in interfaces