jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Mohammed Niaz M.
Greenhorn

Joined: Apr 28, 2007
Posts: 27
Code 1:

String Str = "A";
String lStr = new String("A");

System.out.println(Str.equals(lStr));
System.out.println(Str == lStr);

output: true false

Code 2:
String Str = null;
String lStr = new String();

Str = "A";
lStr = "A";
System.out.println(Str.equals(lStr));
System.out.println(Str == lStr);

output : true true

------------------------
Why in code 1 while using '==' operator its giving "false"
and Why code 2 ==> true.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
String objects created with new operator will result false if you compare
both the references pointing to those object references using ==.






String Object created at line #1 is lost, when you reassign a newly object
to the lStr in further line.

Thanks,
cmbhatt
[ May 24, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Mohammed Niaz M.
Greenhorn

Joined: Apr 28, 2007
Posts: 27
String Str = "A";
String lStr = new String("A");

System.out.println(Str.equals(lStr));
System.out.println(Str == lStr); // line 1

output: true false

Code 2:
String Str = null; //Line 3
String lStr = new String();

Str = "A";
lStr = "A";
System.out.println(Str.equals(lStr));
System.out.println(Str == lStr); //line 2
----------------------------------
In code:1 the Str n lStr are differents string objects.
From your reply i understood that the String is reassign then the NEW reference to that String object will lost. Then it is same as the String Str?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
When you write:

String str;

No object is created, str is a reference variable of type String that
can refer to the String object.

One String object created using new operator and another created
at compile time (without new) will yield false result when you apply ==.


Inside a method or block code, writing String str; and String str =null;
have different meanings. You can't use the precious one (because it is
neither initialized nor set to null) whereas you can use the further one
as System.out.println(str); //prints null

Note: using equals() will always return true if the content of
two strings match irrespective of whether they are same or not.



Thanks,
cmbhatt
[ May 24, 2007: Message edited by: Chandra Bhatt ]
Omer Haderi
Ranch Hand

Joined: Sep 27, 2006
Posts: 42
Hi Mohammed,

First of all the "==" tests if the two references point to the same object. So in code 1:

Str and lStr does not point to the same object because when you use the "new" operator a new object is created rather than looking in the String pool, so there are two different objects and therefore false.

in code 2:

Str and lStr ( Str = "A"; lStr = "A"; ) points to the same value in the String pool so the "==" returns true.

the book of K&B have a nice explanation for this.

Cheers.
[ May 24, 2007: Message edited by: Omer Haderi ]
 
GeeCON Prague 2014
 
subject: Strings