wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conversion" Watch "Conversion" New topic
Author

Conversion

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
<pre>
publicclass 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);
}
}
</pre>
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 ?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

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
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In the first case why not char '2' is 50(unicode value) ?
Any help ?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

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
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Confused again....I removed the String attached to the primitives while printing, then !!!
<pre>
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
</pre>
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanx Maha Anna. Excellent Advice!!! I will certainly follow u.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Conversion
 
Similar Threads
Unicode
a question about conversion
A Simple Control Flow Question
a quiz on character conversion to String
"conversion" of primitive types