Author | Topic: char z =1; no single quotes OK? |
Bill Curr greenhorn |
posted March 21, 2000 11:52 AM
Taking Marcus Green's mock exam, I thought initializing char z = 1; would create an error because it had no single quotes. But it appears to compile. Why is this? Is it because char is considered an unsigned integer primitive, and the one (1) is being interpreted as unicode? passing strange... Thanks for any help. ------------------
|
Steve Butcher greenhorn |
posted March 21, 2000 01:02 PM
Yep, you can get your unicode into a char anyway you see fit. In fact, char c1 = '\u0041'; yield the same value for the c's when printed out ('A'). You just can't cast your char to anything of lesser or equal bitwidth. Steve
|
maha anna bartender |
posted March 21, 2000 07:48 PM
Bill, To add to what Steve said, this kind of assignment is a special kind. Special in the sense, assigning an integer literal to a primitive type var. The rule is the integer literal's value should be within the allowed value of of the assigned left hand side var type. Otherwise the compiler will complain. So byte b = 1; //is OK [This message has been edited by maha anna (edited March 22, 2000).]
|
Kavitha unregistered |
posted March 22, 2000 09:12 AM
When we try giving something like float f = 4.4; would give an error saying explicit cast needed to convert double to float. I guess this is a special case.
|
maha anna bartender |
posted March 22, 2000 11:27 AM
Yes. All integer literals default to primitive type 'int'. All floating point literals default to type 'double'. regds maha anna
|
Jim Yingst sheriff |
posted March 22, 2000 11:47 AM
...and in M.A.'s previous post the special case she mentioned was for integer literals only (well, integral compile-time constants), not floating-points:
|
Steve Butcher greenhorn |
posted March 22, 2000 11:47 AM
Actually, this is one of the strangest things about Java--all integral literals are considered to be int (32 bits) and all floating point literals are considered to be double (64 bits) (1st "anomaly"). For integral types, you don't have to "cast down" the literal int into a smaller type such as byte, short, char so: byte b = 2; // ok although 2 is an int float f = 2.0; // BAD compiler barfs: "Explicit cast needed to convert double to float" If a cast is need to "narrow" for floating point variables why isn't it needed for integral types? (2nd "anomaly") byte b1 = 2, b2 = 3, b3 = 3; Where did the int come from? Arithmetic promotion--all operands to binary operators are promoted to int automatically if they are not int, float or double. So why use byte or short? (3rd "anomaly") So... And this language was "designed" from scratch? Steve
|
Jim Yingst sheriff |
posted March 22, 2000 11:50 AM
Wow - beat you by less than 60 seconds.
|
Steve Butcher greenhorn |
posted March 22, 2000 11:55 AM
Actually, in reference to to Bill's original post, one important point has been neglected: char c = 1; // is okay as we've all seen however... Unicode \u0001 is the control character "soh". To get a '1' to print out you have to put 49 (\u0031) into c. Steve
|
newtojava unregistered |
posted March 22, 2000 11:07 PM
hi all , when i happen to print the same i mean char c = 1; System.out.println( "The char c is: " + c ); the out put is the "char c is " how come..? plz clarify
|
Frank Tamminga greenhorn |
posted March 23, 2000 06:32 AM
The 'toString()' method of a character will return the String representation of the character. So, if you initialize a character like: char c = 57; a '9' will be returned. It is just another way to initialize characters by their number. The number you try to get is not a normal character. All characters beneath 32 are special (antique) control characters. Most of them don't return anything (usefull). f.e. The BELL (7) character made a printer make a bell sound.
|
newtojava unregistered |
posted March 25, 2000 01:55 AM
got it frank . thank's a lot. regards
|
maha anna bartender |
posted March 25, 2000 12:33 PM
Frank, Just a small correction. The 'toString()' method of a character will return the String representation of the character. The 'toString()' method of Character class will return the String representation of the character it contains. [This message has been edited by maha anna (edited March 25, 2000).]
|
| | |