This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing by Value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing by Value?" Watch "Passing by Value?" New topic

Passing by Value?

Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Hello, as you can see I am quite new to this site.
I would like to know, about passing by value but here is some code to help you to understand my problem:

I thought that because s1 and s2 relate to different objects, not s2 = s1; that the first statement would be false, as I am not checking for reference here!?
I know that s1.equals(s2) is true, because the value in s1 is equal to the value in s2
but the answer i got was 1 and 3.
Why is the first if statement equaling true?
p.s. the above code is taken from javaprepare sample test 1, question 19.
trying mocks to pass my exam

How simple does it have to be???
Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
Hi Davy,
good question I made a mistake on this one my first time too. The concept here is to understand how Strings are handled in memory. There is an area in memory called "String constant pool" which java maintains for memory efficiency. When a compiler encounters a String literal, it checks the pool if an identical String already exists in the memory. If yes the new reference is added to the existing String and no new object is created. If no string is found a new string is created, quite obviously. All this is done when you initialize Strings in this format:
String s1 = "abc";
String s2 = "abc";
here s1 and s2 point to the SAME object in the String constant pool.
However if we do
String s3=new String("abc");
because we used the new keyword, Java will create a new String object in normal(non-pool) memory and s3 will refer to it and in addition the literal "abc" is placed in the pool.
All this is from page 359 of Kathy&Bates. Its a MUST read.
David Hadiprijanto
Ranch Hand

Joined: Sep 14, 2003
Posts: 52
Hello Davy,
string literal (like the "abc" in your code) are allocated in the "string constant pool". During execution of the program, if JVM encounters string literal that already existed in the string constant pool, it simply returns the reference to that String object.

prints 1, because s1 == s2 (both are reference to the same String object, that has been allocated by JVM).
For more details see JLS, especially section 3.10.5 String Literals :

Literal strings within the same class (�8) in the same package (�7) represent references to the same String object (�4.3.1).
Literal strings within different classes in the same package represent references to the same String object.
Literal strings within different classes in different packages likewise represent references to the same String object.

Hope this helps.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
thanks guys,
So if I make:

I would end up with the same object so therefore:

Because what you are saying is that if a String on the heap has the same data (regardless if made by new), then all identifiers, relating to the data is pointing to the one copy?
If this is right, does this make Strings implicitly static?
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Sorry guys,
Up late last night, up early this morning,
I forgot, the new keyword makes a new reference, and when I do the s1 == s2, this will be differnet as I am testing for reference not the object.
in the above code I meant s1 == s2 and so on, i did not mean to assign the variables to different objects.
Davk :roll:
Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
hi dav,
yes all of them return false because they have 3 different "abc" objects created in the heap and you are checking if both the references point to the same object.
I agree. Here's the link:
subject: Passing by Value?
It's not a secret anymore!