Thursday, September 29, 2011

SCJP 6 Part I

String str1 = new String("My name is Budi");
String str2 = new String("My name is Budi");

if (str1 == str2) {
System.out.println("Object String is the same string");
}

String str3 = "Your name is Budi";
String str4 = "Your name is Budi";

if (str3 == str4) {
System.out.println("Literals String is the same string");
}

Character c1 = new Character((char) 19);
Character c2 = new Character((char) 19);

if (c1 == c2) {
System.out.println("Object Character is the same character");
}

Byte b1 = new Byte((byte) 8);
Byte b2 = new Byte((byte) 8);

if (b1 == b2) {
System.out.println("Object Byte is the same byte");
}

Short sh1 = new Short((short) 9);
Short sh2 = new Short((short) 9);

if (sh1 == sh2) {
System.out.println("Object Short is the same short");
}

Integer int1 = new Integer(1000);
Integer int2 = new Integer(1000);

if (int1 == int2) {
System.out.println("Object Integer is the same integer");
}

Long l1 = new Long(18);
Long l2 = new Long(18);

if (l1 == l2) {
System.out.println("Object Long is the same long");
}

Float f1 = new Float(17);
Float f2 = new Float(17);

if (f1 == f2) {
System.out.println("Object Float is the same float");
}

Double d1 = new Double(5000);
Double d2 = new Double(5000);

if (d1 == d2) {
System.out.println("Object Double is the same double");
}

Boolean bol1 = new Boolean(true);
Boolean bol2 = new Boolean(true);

if (bol1 == bol2) {
System.out.println("Object Boolean is the same boolean");
}

Result : Literals String is the same string

We can conclude that two different objects with same literals are always two different objects.

0 comments:

 

©2009 Stay the Same | by TNB