• 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

Question from jqplus for equals()

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
can any one please explain me why answer D is right.
Which expressions will evaluate to true if preceded by the following code?
String a = "java";
char[ ] b = { 'j', 'a', 'v', 'a' };
String c = new String(b);
String d = a;
A) (a == d) B) (b==d) C)(a == "java")D) a.equals(c)
The answer is A,C & D

[This message has been edited by K2Joshi (edited September 13, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is onlu one String onject with value of "java" existed after the first sataement executed. So, all the reference variable a, c, d, and the String litler "java" are with same value, they are not only equals() but == as well.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you create an object its data is placed in the systems memory. Every memory location has it's own 'address' or reference.
It helps to think of memory as a filing cabinet. For example, when String object 'a' is created the JVM opens memory Drawer#1 and puts the characters 'java' inside. It then stores the location Drawer#1 in 'a'. When String 'c' is created, the characters 'java' are placed in Drawer#5 and the location Drawer#5 is stored in 'c'.
So if you say 'a == c' the answer is false because the object named 'a' is not in the same drawer (memory location) as the object named 'c'.
But, 'a.equals(c)' is true because the equals() method goes to the filing cabinet, looks in Drawer#1 and Drawer#5, and sees that both drawers (memory locations) hold the same characters 'java'.
Hope that helps
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jane,
Pl. correct me if I'm wrong but when String c is created, does not it not call the String.intern() method to check if the "java" object is already available in the String's pool? In that case, c will end up pointing to the same object that a is pointing (i.e. Drawer#1 according to your explanation). So, c==a will be true.
Am I right in my understanding?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
srikrish,
Whenever a String is created with the new keyword, a new String object is created at a new memory location. So c==a would be false.
Please correct me if I'm wrong
Thanks
Sandeep
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys ...
I think srikish may be right about String using the pool.
I am very sorry ... thought I understood this but obviously I don't. Please see my newest post.
Apologies for any confusion I've caused.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, we didn't think this will create so much of confusion...
String a = "java"; //1
char[ ] b = { 'j', 'a', 'v', 'a' }; //2
String c = new String(b); //3
String d = a; //4
A) (a == d)
Obviously, because of //4. and '==' checks whether both the references point to same memory location or not.
B) (b==d)
Obviously not as b is an array of chars and d is a String! (This one was for c/c== programmers.)
C)(a == "java")
Whenever you create string using just "" (ie. without using new keyword) you get an interned String. 'interned' means the JVM uses the same memory location to provide for different references which want to refer to same string (content wise).
So, this is true as both a and "java" are actually stored in the same place.
But if you do new String("java"); you are explicitly telling the compiler to create/store this string in a seperate location. so a == c is not true.
Even if :
c = new String("java"); or c = new String(a);
(Take note, Jane)
D) a.equals(c) is true.
This is a standard method which is implemented such a way that it checks whether the contents of both the references are same or not.
HTH,
Paul.
------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!

[This message has been edited by Paul A (edited September 14, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, every1,
Sorry, I made a mistake yesterday. I'm almost understand. the following code:
class Test {
public static void main(String s[]) {
String a = "java";//line3
String b = new String("java");
System.out.println(a == b);
System.out.println(a.equals(b));
System.out.println(a == "java");//line7
System.out.println(a.equals("java"));
}
}
compile and run, the output is :
false
true
true
true
BUT when i modify the line3 to :
String a = new String("java");
the line7 will output false
Is the object referenced by "a" not a "interned" string? Can anybody help me?
Thanks.
Yiqing
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic