• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

char to int

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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');

 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
reply
    Bookmark Topic Watch Topic
  • New Topic