aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string equal(); question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string equal(); question" Watch "string equal(); question" New topic
Author

string equal(); question

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
What will be the result of executing the following code
public class MyClass
{
public static void main(String args[])
{
System.out.println("SCJP" == "scjp".toUpperCase() )
}
}
why it will print flase ??/

is operation on string create new object on heap not on string pool ??

and if it create object on string pool then it must be true....

pls comment...


Thanks and Regards, Amit Taneja
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
check the implementation of toUpperCase, at the end of the function it is

return new String(0, result.length, result);

so a new object on heap not in string pool.

soni.


SCJP 1.4, SCBCD 1.3
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
didn't get your answer ??

pls clarify more
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

Pretty much only string literals are placed in the string pool. any time you call a method that makes a new string, it will be placed somewhere else. the literals can be determined at compile time, but the others have to be made a run-time

The compiler can't figure out what or where the string created by "scjp".toUpperCase() is or will be - it's not the compiler's job to execute funtion calls.

so, the string returned by this is most definatly NOT in the string pool.
[ June 01, 2005: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
THANX

MORE REPLY PLS TO GET MORE INFO ABOUT THIS
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
There are two ways to place strings into the pool. One by defining the string constant at compile time and other being calling the intern method of the String class.

So in your example, if you execute intern() method on the string returned by the toUpperCase(), you should get the result as true.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
ok then the final conclusion is that

code like

"string".toUpperCase();

will create new string and put in HEAP ?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

all strings are in the heap. all references to them are in the stack. literals are placed in what's called the 'string pool' - these are the literals the compiler can figure out.

anything created by a call, such as toUpper(), new String(), or even string1 + string2 can only be figured out at run-time. these strings will NOT be in the string pool, unless you then call the intern() method on it.
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
Originally posted by fred rosenberger:
all strings are in the heap. all references to them are in the stack. literals are placed in what's called the 'string pool' - these are the literals the compiler can figure out.

anything created by a call, such as toUpper(), new String(), or even string1 + string2 can only be figured out at run-time. these strings will NOT be in the string pool, unless you then call the intern() method on it.


fred, check the output of the foll code :
compiler evaluates String+String



Thnx
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

compiler evaluates String+String


sigh... yes. i guess i should have been clearer...

<string VARIABLE> + <string VARIABLE> cannot be evaluated by the compiler (i'm pretty sure that is correct). string LITERALS can be.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string equal(); question
 
Similar Threads
nodding my head for these (scjp)
Clarification on String Objects for SCJP
String Pool
SCJP Brainteaser (10)
Kathy Sierra - SCJP 1.6 - Chapter 3 - Self Test - question 1 - doubt