Thursday, September 29, 2011

SCJP 6 Part II

Character c3 = 127;
Character c4 = 127;

if (c3 == c4) {
System.out.println("Literals Character less than 127 is the same character");
}

Byte b3 = 127;
Byte b4 = 127;

if (b3 == b4) {
System.out.println("Literals Byte less than 127 is the same byte");
}

Short sh3 = 127;
Short sh4 = 127;

if (sh3 == sh4) {
System.out.println("Literals Short less than 127 is the same short");
}

Integer int5 = 100;
Integer int6 = 100;

if (int5 == int6) {
System.out.println("Literals Integer less than 127 is the same integer");
}

Long l3 = (long) 127;
Long l4 = (long) 127;

if (l3 == l4) {
System.out.println("Literals Long less than 127 is the same long");
}

Float f3 = (float) 66;
Float f4 = (float) 66;

if (f3 ==f4) {
System.out.println("Literals Float is the same float");
}

Double d3 = (double) 6;
Double d4 = (double) 6;

if (d3 == d4) {
System.out.println("Literals Double is the same double");
}

Boolean bol3 = true;
Boolean bol4 = true;

if (bol3 == bol4) {
System.out.println("Literals Boolean is the same boolean");
}

Result :

Literals Character less than 127 is the same character
Literals Byte less than 127 is the same byte
Literals Short less than 127 is the same short
Literals Integer less than 127 is the same integer
Literals Long less than 127 is the same long
Literals Boolean is the same boolean

We can conclude that two different objects created by assigning primitive to it, if the primitive is between -128 and 127, they are equal object for Charater, Byte, Short, Integer, and Long but not equal for Float and Double.

Obviously, if the primitives are not between -128 and 127, they are two different objects.

0 comments:

 

©2009 Stay the Same | by TNB