Author Topic:   Conversion
Umesh
ranch hand
posted April 13, 2000 08:44 PM             

public class Class1
{
public static void main(String args[])
{
int i =1;
char c = '2';
c += i; // i += c;
System.out.println("char: " + c); // System.out.println("int: " + i);
}
}

o/p:
char: 3
int: 51 //if I use the commented line in place of the original

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;
i += c;
Integer value of char '2' is 50 (from ASCII table)
(Also, I know chars' in java are Unicode)

So, the above stmt evaluates the value of 'i' to 51
and then prints it.

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;
byte b = b1 + b2; // Compile Error.
// int cannot be assigned to byte.

int i = 1;
byte b3 = b1 + i; // Compiles Okay and result is 2

Same if you replace byte with short.

Also,
b1 += 1; /Compiles and result is 2.

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(true); //prints 'true'
System.out.println(false); //prints 'false'
System.out.println(null); //prints null
System.out.println("maha"+null); //prints 'mahanull'
System.out.println("Umesh"+true); //prints 'Umeshtrue'

System.out.println(false+true); //NOT OK
System.out.println(false+null); //NOT OK
System.out.println(true+null); //NOT OK

StringBuffer sb= new StringBuffer("java");
System.out.println(sb+10); //NOT OK because atleast 1 must be String

So, coming back to your qstn,
System.out.println("char: " + c);
In the above statement for the + operator one operand is String. So the other's toString() is called and added to the first. Since the 2nd operand id 'char' primitive a 'Character' Wrapper class is created automatically and its toString() is called. The toString() of Charactor returns the charactor itself. NOT the int value. So the result of the above statement is char:3

System.out.println("int: " + i);
But for this, an Integer wrapper object is created and its toString() is called. Integer class's toString() returns the 'int' value of the Integer object. So the result shows as int:51
regds
maha anna

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,
the compound operators (anyoperand)= (+= , -= ,*= , /= , %= etc)
will convert the result automatically back to the type of the left hand side var.It is equivalent of the foll.
b3+=1; is same as b3 = (byte)(b3+1);
Simillarly
b3 += i; also will work; It is SAME as b3 = (byte)(b3+i);

regds
maha anna

regds
maha anna

satya5
ranch hand
posted April 13, 2000 11:46 PM             
Maha Anna:

While working out this example, I stumbled on this:

int i = 1;
byte b = 0;
byte b1 = 2;

b += i; // Works okay
b1 = b1 + i; // Fails to compile int cannot be converted to byte.

Whats' the difference?
( Not sure if I have to start a new thread ....)

Thanks.

- satya


OOPS, looks like both of us are online at the same time ...I gotit ...

- 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 !!!!!

int i =1;
char c = '2';
i += c;
System.out.println(i); //prints 51

int i =1;
char c = '2';
c += i;
System.out.println(c); //prints 3

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.
regds
maha anna

[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.

|