File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Downcasting and the value
 
Similar Threads
pRINTING AN OBJECT WITH NULL REFERENCE
a char and int interconversion doubt!
pRINTING AN OBJECT WITH NULL REFERENCE
wierd problems..
overriding member fields in interfaces