• 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

equals / ==

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)System.out.println("SCJP" == "scjp".toUpperCase() ) will print false why?
5)String str = "SCJP";String str2 = "";
System.out.println(str == (str + str2).intern()); will print true why?;
6)String str = "SCJP";String str2 = ""; // There is no space between double quotes.
System.out.println(str == str + str2); print false why?
7)String str = "SCJP"; String str2 = str.concat(""); // There is no space between double quotes.
System.out.println(str == str2); will print true why?
8)int arr1[] = {1,2,3,4}; int arr2[] = {1,2,3,4};
System.out.println(arr1.equals(arr2)); will print false why?
9)String str1="Java"; System.out.println(str1 == "Ja"+"va");
will print true why?
10)String str1= "Ja"; String str2= "va"; String str3= "Java"; System.out.println(str3 == str1 + str2); will print false why?
11)Object strarr [] = {"Welcome",null};
Object strarr2 [] = {"Welcome",null};
System.out.println(strarr[1] == strarr2[1] );wiill print true and System.out.println(strarr[1].equals(strarr2[1]) ); will print flase why?

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"==" asks if we are pointing to the same spot in memory. the equals() method let somebody decide what is "equal enough", and then they write a method to compare stuff.

so...

1) "SCJP" is a literal on the heap somewhere. when you call "scjp".toUpperCase(), it creates a NEW string on the heap. since the new string cannot occupy the same space as the old one, we are referring to two different places in memory. therefore, false.

5) from the api, "When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned." so, when you add str and str2, you get "SCJP". this is in the string pool, so you get a reference to that. therefore, the two references DO point to the same place in memory.

6) this time, you add the two strings together, you get a brand new string on the heap. diferent place in memory, not equal.

7) again, from the api "If the length of the argument string is 0, then this String object is returned." your argument has a length of 0, so we return a reference to the original string.

8) i stopped looking things up, but what i think is happening here is that there is no method defined for comparing arrays like this. So you get the equals() method inherited from Object, which, guess what???, compares memory location.

9) the compiler is smart enough to optimize your literals.

10) the compiler is not smart enough to optimize this away.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this out, as well.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont understand why 9 prints True and 10 prints False

If the + operator has precedence over the == operator in the case of str=="Ja"+"va"

then why does it not happen in str = str1+str2
the only difference i see here is tht the strings are stored in variables.

Please explain !!

Also if sum1 could explain Q 11 thn it would be gr8 !!
Thanks
[ August 10, 2004: Message edited by: Murtuza Akhtari ]
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In #9, the compiler reads the line. str1 points to the string literal "Java" in the string pool.

in the condition, the compiler can read "Ja" + "va". it KNOWS that this will NEVER CHANGE. this can be simplified, without danger, to the string "Java", which it KNOWS is already in the string pool. so "Ja" + "va" is optimized to a reference to the same string pool object that str1 refers to.

in #10, you are adding together two variables. the compiler doesn't know that they won't change. so, this can't be optimized. when the code is executed, str1 + str2 is evaluated. since it's being done at run-time, it is NOT in the string pool, but in a brand new place on the heap.

therefore, they do NOT refer to the same place in memory.
 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Fred !!
 
reply
    Bookmark Topic Watch Topic
  • New Topic