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 char to int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "char to int " Watch "char to int " New topic
Author

char to int

Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Hi Friends,
int can't be assigned to char without explicit casting.
then why this compiles
char c=7;
and why it doesn't
Character ch=new Character(7);

[This message has been edited by Nasir Khan (edited November 28, 2000).]
Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
Hi Nasir,
the compiler checks if the integral value you pass to the char fits in a char. If this is the case, assignment is possible...
You can do a whereas a won't work.
If you try to create a Character-object, he doesn't make this check, it is required that you pass in a char... So if you wanna make a new Character(7) you have to explicitly cast the 7 to a char...
hope that helps,
as always, correct me if i'm wrong
cheers
Oliver
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Thanks Oliver
As you said for Character-object we have to explicitly cast an
int to char i think this explicitly casting is enforced for
Chararcter only For example all these compile without any
explicitly casting
Integer in=new Integer('e');
Float f=new Float(4);
Long l=new Long('e');

Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3253
    
    2
The point is the constuctor of Character takes char as an argument. In method parameters, implicit narrowing conversion does not happen that's why you can pass only a char. Now, as you can convert an int to char by explicit casting, you are thinking that it is enforced by Character class but actually it is a standard rule and can happen to any method that takes a char.
On the other hand, Interger takes and int, Long take a long, Float takes a float and all of these will easily accept a char as a char can be implicitly "widened" to any of these data types. Again, this has nothing to do with these classes. It is a standard rule that a small data type can be automatically widened to a bigger datatype.
HTH,
Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Thanks paul
I'm gonna keep the point on my mind
Implicit narrowing conversion can't occur in method call or
in constructor call but it can be while declaring or assigning
a variable like char c=20;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: char to int
 
Similar Threads
instance variables initialization
String assignment confusion
One last problem
ASCII - UNICODE
Type casting a character to an Integer