Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
josephine chen
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(hello + java)
prints hellojava
how is it diff from
System.out.println("hello" + "java")
what does .intern() method do???
 
gunjan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way the code can work. hello and java without quotes are treated as variables. And since they are not declared before Compiler flags an error.
Use of intern on a string object makes puts that it in the common pool of strings.
that is
String s1 = new String("a");
String s2 = new String("a");
In this case two objects created above are different, but if you use intern() above, then two objects share the same space and hence are same.
Regards
Gunjan
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm stumped. You compiled and executed
<CODE>System.out.println(hello + java)</CODE>
and it did what?
String.intern() makes the string part of the infamous 'string pool'.
 
josephine chen
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prints hellojava!!!.No compilation errors
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there more to the program that you forgot to mention? Did you perhaps define hello and java somewhere?
 
josephine chen
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry , they are variables which I had declared.
I am sorry for this!!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And did you perhaps give the variables values? Like
<code><pre> String hello = "hello";
String java = "java";</pre></code>
If so, then there's really no difference between
<code><pre> System.out.println(hello + java);</pre></code>
and
<code><pre> System.out.println("hello" + "java");</pre></code>
Should there be?
It would be easier to answer your questions if you provided more information about what you're talking about, and why it confuses you.
As for intern(), it's defined in String.intern().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic