| Author |
No. of string instances
|
Ravinder Rana
Greenhorn
Joined: Dec 07, 2006
Posts: 19
|
|
Consider the following code: Now, suppose 10 threads are concurrently executing this method. How many instances of String "Teacher", "Prof", and "Lecturer" will get created - 3 or 10*3??
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
|
Three; it doesn't matter how many threads call this at the same time or how many times you execute the code. String literals are pooled.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Jesper Young: Three; it doesn't matter how many threads call this at the same time or how many times you execute the code. String literals are pooled.
And even if String literals weren't pooled, it would still be three, because every literal exists only once in the byte code.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Sidd Kulk
Ranch Hand
Joined: Feb 20, 2007
Posts: 152
|
|
|
And what about non-String objects? Say, for example, StringBuffer.
|
 |
 |
|
|
subject: No. of string instances
|
|
|