Author | Topic: Conversion |
Umesh ranch hand |
posted April 13, 2000 08:44 PM
o/p: What type of conversion takes place in the 2nd case ?
|
satya5 ranch hand |
posted April 13, 2000 09:06 PM
In the second case ie; So, the above stmt evaluates the value of 'i' to 51 Is there anything more to this ? Regds. - satya
|
Umesh ranch hand |
posted April 13, 2000 09:24 PM
In the first case why not char '2' is 50(unicode value) ? Any help ?
|
satya5 ranch hand |
posted April 13, 2000 11:20 PM
I see the light...and its really weird ... Check these out: byte b1=1, b2 = 2; int i = 1; Same if you replace byte with short. Also, Not sure whats going on here ..... - satya
|
maha anna bartender |
posted April 13, 2000 11:31 PM
Umesh, In Java, the '+' operator acts like this. 1. primitive + primitive //arithmetic result 2. String + primitive // the prmitive is internally converted to the corresponding wrapper object and the wrapper object's toString() method is called and this result is concatenated to the first operand. Also note that in order for the String conversion of the other operand to take place, one of the 2 operands MUST be a String object. In other words, aStringBufferObject + primitive WILL NOT work. Simillarly there is a corresponding String conversion for the literals true/false/null. But they can be either used individually / (literal +String) for the System.out.println(..) method in order to work. In other words, System.out.println(false+true); //NOT OK StringBuffer sb= new StringBuffer("java"); So, coming back to your qstn, System.out.println("int: " + i);
|
maha anna bartender |
posted April 13, 2000 11:44 PM
satya, in your previous post byte b3= b1+i; where b1,b3 are bytes and i is int WILL not compile. Please verify again. Because any arithmetic operation on the RHS will be atleast of 'int' type . YOu should get 'Explicit cast needed to convert int to byte' compile time error for the above statement. Regarding the next qstn, b3+=1; //compiles and runs ok, regds regds
|
satya5 ranch hand |
posted April 13, 2000 11:46 PM
Maha Anna: While working out this example, I stumbled on this: int i = 1; b += i; // Works okay Whats' the difference? Thanks. - satya
- satya [This message has been edited by satya5 (edited April 13, 2000).]
|
Umesh ranch hand |
posted April 14, 2000 07:37 PM
Confused again....I removed the String attached to the primitives while printing, then !!!!!
|
maha anna bartender |
posted April 14, 2000 10:40 PM
Umesh, See, System.out is a 'PrintStream'. PrintStream class has got so many overloaded print(....) as well as println(...). In your code when you call System.out.println(char c), the perfect fitting println is the one which takes a 'char' as an argument, and this println(char c) method prints the 'character' as shown in API here. Simillarly System.out.println(int i); prints the integer value to the console. Please refer to the API and do some research. [This message has been edited by maha anna (edited April 14, 2000).]
|
Umesh ranch hand |
posted April 14, 2000 11:04 PM
Thanx Maha Anna. Excellent Advice!!! I will certainly follow u.
|
| | |