• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Strings

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code:1
String str = null; //Line 1
String lStr = new String();

str = "";
lStr = "";
if(lStr.equals(str)){
System.out.println(str.equals(lStr));
}else{
System.out.println("Else");
}
------------------------
Code:2
String str = new String;//Line 2

String lStr = new String();

str = "";
lStr = "";
if(lStr.equals(str)){
System.out.println(str.equals(lStr));
}else{
System.out.println("Else");
}
---------------
The outputs are true,true.
The line 1 and 2 are the same.

In code 1 str is set to null and its storage is differ from lStr?
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


String str = null; //Line 1
String lStr = new String();

str = "";



equals() method checks for the contents of the variable. In line1 string "str" is null. Later you are assigning it to a empty string so now the string is not null. When you check with the equals() method since both the contents are same it prints true.


Regards
Nik
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Equals methods only checks for character sequence in two strings. if character sequence is same , it will return true else false.

if you want to check the references are equal or not, use == operator.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In code 1 str is set to null and its storage is differ from lStr?




If I get your question correctly:

String str=null;

A String ref variable is created and set to null; It simple means this ref
variable is not referring to object on the heap.


String str = "";

A String ref variable is created, one blank string object is created
(assume constant pool is empty) and ref variable str is set referring to
that.


Note: equals() compares the content of the object while == is used to
test the references whether they point to the same object on the heap or
not.

Thanks,
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm,

Maybe he was trying to see if the strings were Objects and meant to use instanceof ???
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic