• 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 question .explanation pls

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
--------------------------------------------------
class Test {
public static void main(String args[]) {
String ab="AB"; // line1
String a = "A", b = "B", c = a+b, d = a+b;
System.out.print(((a+b)==(a+b)) + ",");
System.out.print((c==d) + ",");
System.out.print(c.equals(d));
}
}
----------------------------------------------------
The above program outputs result as "false,false,true".
on line1 we declare variable ab which will put "AB" string pool. So when we do a+b then it should return the same string reference "AB" as it is already in the string pool.
Based on above logic c==d should return true, but it returned false in the above code.
Pls explain with details.
Raju
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLS, §15.18.1 String Concatenation Operator +:


The result is a reference to a newly created String object that is the concatenation of the two operand strings.


From me:


The String literal pool is not on the SCJP exam.


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

Originally posted by Corey McGlone:
From the JLS, §15.18.1 String Concatenation Operator +:
Corey



results in "Not Equal".
Even though I thought, "String" in the string pool, would give true for the '==' test.
Is this really testing a String Pool concept?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by V Bose:

Is this really testing a String Pool concept?


Yes, that is a String literal pool question. Such questions will not appear on the exam. The key is that, when you invoke the trim() method, the method will return either the original String (if no trimming was required) or a new String. It will not return a reference to a Sting in the literal pool. If you'd like to get one, though, you can use the intern() method.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since String is an OBJECT and not a premitive data type, you cannot use the "==" operator with it.
The way to compare two Strings is string1.equals(string2)
regards
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shantanu Wagh:
Since String is an OBJECT and not a premitive data type, you cannot use the "==" operator with it.
The way to compare two Strings is string1.equals(string2)
regards


You can use the reference equality operator (==) on any two reference variables. However, rather than determining if the two objects are equal (via the equals method), it will determine if the two reference variables refer to the same object or not. Quite often, that isn't the behavior you're after, but there are some cases in which it is useful.
Corey
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi raju
When you are doing c=a+b as a and b are variables they are not constant so their value is not determined at compile time so a new variable c is created having value "AB" and same is the case when you are creating d=a+b then again a and b are variable whose value cannot be determined at compile time so new variable is created although the variable c having the same content is already present in string pool. So that's why two different variables are created in string pool having the same content and that's why it is returning false when you are doing c==d.
Regards
Rashi
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These questions can cause some confusion in the exam. Anyway, here is something pertinent from the JLS that might help to remember a few things about Strings.



and the compilation unit:

produces the output:
true true true true false true
This example illustrates six points:

  • Literal strings within the same class (�8) in the same package (�7) represent references to the same String object (�4.3.1).
  • Literal strings within different classes in the same package represent references to the same String object.
  • Literal strings within different classes in different packages likewise represent references to the same String object.
  • Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
  • [B}Strings computed at run time are newly created and therefore distinct. [/B]
  • The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.



  • [ May 05, 2004: Message (format) edited by: Anil Hulikal ]
    [ May 05, 2004: Message edited by: Anil Hulikal ]
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic