• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string equal(); question

 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Soni Prasad
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
didn't get your answer ??

pls clarify more
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANX

MORE REPLY PLS TO GET MORE INFO ABOUT THIS
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok then the final conclusion is that

code like

"string".toUpperCase();

will create new string and put in HEAP ?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic