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
Joined: Nov 04, 2000
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');
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.