aspose file tools*
The moose likes Beginning Java and the fly likes Just two literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Just two literals" Watch "Just two literals" New topic
Author

Just two literals

Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Hi

This code


shows that two literals are... actually only one literal. Is it guaranteed by JLS and will be correct on any JVM?

Thanks.


SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
Steve Simon Joseph Fernandez
Ranch Hand

Joined: Jul 17, 2005
Posts: 35
Hi,

It should be guaranteed if the code that you wrote should provide the same output across JVM implementations

Anyways, given the immutable property of literals, it would be foolish to create two objects for the same literal.

_steve.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Originally posted by George Bolyuba:
Is it guaranteed by JLS and will be correct on any JVM?


You will need to dig into JLS for that.
But this can be explained by 'String pooling'. See
intern method in String class


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Originally posted by Sonny Gill:

But this can be explained by 'String pooling'.


Yeah, I know about 'String pooling' and I explain behavior of my code just like you suggest.

Originally posted by Sonny Gill:

You will need to dig into JLS for that.

I hoped that someone did it for me . The background of my question was: "is the 'String pooling' mandatory for all JVM?". So, I believe I have to dig into JVM specification...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

The behavior of your program is guaranteed.


[Jess in Action][AskingGoodQuestions]
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Originally posted by Ernest Friedman-Hill:
The behavior of your program is guaranteed.


Ernest, your word is enough for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Just two literals
 
Similar Threads
Accessing a Static Member
How can I get code examples to look right
Pattern & Matcher - how to get any combination?
Regular Expressions in String.split()
Empty space caused by rendered attribute