Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Synchronization question from Exam Lab

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is question 37 from the diagnostic quiz in Exam Lab



The possible answers are :
A) compilation fails
B) Prints A-A-B-B-C-C
C) Prints A-B-C-A-B-C
D) Result is unpredictable
E) Exception at runtime.


The answer is D. The explanation give is "a new string object is created every time because of the keyword new. Therefore the lock of an instance of the given String will be owned by the first thread; and another instance of the given String will be owned by the second thread.

However, in the K&B book page 433-434 it says "If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created."

In the above example, wouldn't the string "a" be in the string pool, meaning it's the same object, and so synchronization would work and the output would be A-B-C-A-B-C

Does the new String("a") causes a new String in the string pool? So if you did the following:
new String("a");
new String("a");
new String("a);

would you end up with 3 String objects in the String pool all representing the letter a?
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Maren Fisher , it works like this:
1- if you assign a String literal to a String variable the JVM will check the String Pool for a similar String object, if it finds one it will be assigned to your variable, else the String object will be created using your String literal and will be added to the String Pool.
e.g: String s = "if not in the pool, create one. If in the pool assign to s";

2- When using the new keyword you are forcing the JVM to create your String object in the heap, anyway the JVM will chick the String Pool after creating your String object in the heap: if there is a similar String object nothing will happen, else the new created String object will be added to the String Pool.
Regards
 
Maren Fisher
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, ok. Thank you.
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Imad Aydarooos wrote:Dear Maren Fisher , it works like this:
1- if you assign a String literal to a String variable the JVM will check the String Pool for a similar String object, if it finds one it will be assigned to your variable, else the String object will be created using your String literal and will be added to the String Pool.
e.g: String s = "if not in the pool, create one. If in the pool assign to s";

2- When using the new keyword you are forcing the JVM to create your String object in the heap, anyway the JVM will chick the String Pool after creating your String object in the heap: if there is a similar String object nothing will happen, else the new created String object will be added to the String Pool.
Regards


Sorry Maren Fisher and other readers, I'm wrong in statment 2, and if you used the new keyword nothing will be added to the String Pool, objects will be created in the heap.
please read this :
Strings, Literally
 
Not so fast naughty spawn! I want you to know about
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic