This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Mohammed Niaz M.
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mohammed Niaz M.
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic