• 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

Strings

 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It prints hellojava!!!.No compilation errors
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry , they are variables which I had declared.
I am sorry for this!!
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic