aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
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

Joined: Jan 28, 2000
Posts: 33
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


Regards<BR>Gunjan
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
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'.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
It prints hellojava!!!.No compilation errors
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Is there more to the program that you forgot to mention? Did you perhaps define hello and java somewhere?


"I'm not back." - Bill Harding, Twister
josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
Sorry , they are variables which I had declared.
I am sorry for this!!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings