• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String problem

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 String s1 = "Amit";
2 String s2 = "Amit";
3 String s3 = new String("abcd");
4 String s4 = new String("abcd");
5 System.out.println(s1.equals(s2));
6 System.out.println((s1==s2));
7 System.out.println(s3.equals(s4));
8 System.out.println((s3==s4));

Output of above code is:
true
true
true
false

Output from 6th line is true but 8th line false why?? Please explain..
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Soni,
If u are not costrcting the string using new, it will refer it from the string pool. so the first 2 statements, refering to the same place, but if u create a string with new it will allocate memory for it, so the next 2 statements are refering to different locations.
== compares the references, and equals() compares the contents, so u got the result like that.
i think u got my point.
sri.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Soni,

In String == checks, whether the object's are same.


1 String s1 = "Amit";
2 String s2 = "Amit";
3 String s3 = new String("abcd");
4 String s4 = new String("abcd");
5 System.out.println(s1.equals(s2));
6 System.out.println((s1==s2));
7 System.out.println(s3.equals(s4));
8 System.out.println((s3==s4));



Please note in String that there is a difference in creating a String with new String() and simply Initializing for the same value.

String str = "a";
String str1 = new String("a");

Here str and str1 will point to different address, means they are different objects thou having same value.


In above quote output of 8 is "False" since Object s3 and s4 are different.
The reason for 6 is true, is
If you initialize any number of String's with same value eg:- "Amit", all will point to same address where an Object is created for "Amit".
ie: s1 and s2 are pointing to the same address, were in s3 and s4 are pointing to different address.

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

Hi,
Soni Firstly
== Checks the Object reference equality,while
equals Checks the Object value equality,Secondly
When you code:
String S="JavaRanch";
String S1="JavaRanch";
these are just reference variables not Object
until we don't use new operator and when we
use new it refers to two different new objects
while if we are not using new then it refers to
String Pool.

 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means

String a1 = "soni";
String a2 = "soni";
String a3 = "soni";

a1,a2,a3 all are refering to the same string object in the pool but

String a4 = new String("soni");
String a5 = new String("soni");
String a6 = new String("soni");

a4,a5,a6 are refering to the three different string object?
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So in the above case a1, a2, a3 are not objects they are just references but refering to what?? (some string in the pool but is that string not an object??).
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
soni,

at the time of declaring the variables the following process will be done..

str1 = "somu";
//Compiler create new reference for this String.
str2 = "somu";
//Compiler check the String Pool,if it found the same content, compiler will assign to that exist reference..other wise it will create new one.

"==" compares the references so it shown true
.equals() compares the content so it is also shown true


but in the following case, it will craete always new references because we r using the new operator

String str1 = new String("somu");
String str2 = new String("somu");
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
a.equals(b)//compares the content of the two reference variable
a==b//compares the address of the reference variable

can i say that the above conditions are true whether the equals() method is overwritten or not.
or

is there is any impact on the above condition when the equals method is overridden.

what the significance in overridding the equals method.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Soni,

All strings in Java are represented by objects. I think the confusion comes from the fact that there are really two times that Java is dealing with your code.

The first is when your code is compiled. At that point, any string literals (that is, any strings that are typed directly into your code) are set up as string objects. You can think of the literal itself as being a reference to this string object.

However, to save memory, Java is smart enough to keep these objects in the "string pool". If you have two literals with the same text, Java will make those two literals point to the same string object. Java can get away with this sort of reuse because the text of string objects cannot ever be changed. And it can afford to take the time to do all this matching because it's compiling, and time is not really a big factor.

The second time Java deals with your code is during run-time. Well okay, it's really dealing with a bytecode representation of your code, if you want to get picky. Anyway, at that point, when your program is running, it will also be creating string objects. Often, this creation will be because you've used "new", but it also happens behind the scene as a result of calling various string functions (like trim()). Java can't predict what will happen here ahead of time, when you compile, because it all depends on how your code executes. It has to run the code first to find out what strings objects to instantiate.

At this point--when strings are created at run time rather than at compile time--Java does not normally look at the string pool and try to "match" up your new string with an already existing one. It doesn't have time! Instead, it creates a brand-new object, even if it does duplicate a string object already in the string pool.

(You can force Java to use the string pool at run-time by using the intern() method on a string)

Hope that helps!

- Jeff
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how much objects will be created from
1) String s=”amit” 2) String s=”amit” 3) String s=new string ”amit"
What is the answer
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R Pinjarkar wrote:how much objects will be created from
1) String s=”amit” 2) String s=”amit” 3) String s=new string ”amit"
What is the answer



Howdy, welcome to Coderanch.

Let me try to fix your code slightly so that it would compile, then I would try to answer it

There will be 2 String objects created here. s1 and s2 will point to the same String object (coming from the String constant pool), while s3 will point to a separate String object as you are specifically calling the constructor.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic