File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes int to char casting 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 "int to char casting" Watch "int to char casting" New topic

int to char casting


Joined: Oct 10, 2000
Posts: 6

This code has no Compilation and Runtime error. How does the code work? There is no implicit casting of int to char
Can anyone explain?
class Main{
public static void main(String argv[])
char i= 10;
int j='h';

Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3635
This may help you...
Get Certified, Guaranteed!
(Now Revised for the new Pattern)

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Ira Jain
Ranch Hand

Joined: Sep 06, 2000
Posts: 70
Hi ,
It does not display an error in case of char i=10 because 10 is within the valid range of values for a char literal.If we try to store a value which is above its range like 1234567890 it will give an error.
In case ,of the second statement int i='h',it is a case of widening conversion . An integer literal can hold short,char and byte literals without any conversion.
Hope the above will help u.
I agree. Here's the link:
subject: int to char casting
It's not a secret anymore!