• 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

Passing by Value?

 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
thanks
Davy
p.s. the above code is taken from javaprepare sample test 1, question 19.
trying mocks to pass my exam
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Hence,

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
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 think I'll just lie down here for a second. And ponder this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic