File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question to Kathy - from your book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question to Kathy - from your book" Watch "Question to Kathy - from your book" New topic
Author

Question to Kathy - from your book

Karthik Balasubramanian
Ranch Hand

Joined: Dec 13, 2002
Posts: 43
Hi Kathy,
In your book, in Ch.6 (Strings)..you say that when you create a new string like..
String s = new String("Hai");
two objects are created. One referred by s and another one is "Hai" which was lost.
what happens when you create a string like this
String s = "Hai";
How many objects are created now?
Can you please explain me how?
Thanks,
Karthik
Shiva Mantri
Greenhorn

Joined: Dec 30, 2002
Posts: 19
I didnt read Kathy's book, but does it really create two objects when you use String ("abc")?
If this is true, why is one object lost? I think it would be in String pool, not lost. If it is really lost, then its really stupid.
I know that when you create Strings using string literals, like String s = "abc", it would be added to the String pool, if doesnt exist in the pool. If it already exists, then its reference is returned.

thats why s1 == s2 is true.
[ January 15, 2003: Message edited by: Shiva Mantri ]
[ January 15, 2003: Message edited by: Shiva Mantri ]
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Karthik
I don't believe that Kathy's book says what you think it says. Here is a direct quote from the chapter on Strings, which answers your question.

1 � String s = "abc"; // creates one String object and one reference
// variable
In this simple case, �abc� will go in the pool and s will refer to it.
2 � String s = new String("abc"); // creates two objects, and one
// reference variable
In this case, because we used the new keyword, Java will create a new String
object in normal (nonpool) memory, and s will refer to it. In addition, the literal
�abc� will be placed in the pool.


SCJP 1.4
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Here's a way to create a String, and then "lose" the reference to it:
String s = "java";
s.toUpperCase();
The 2nd line creates a new String object containing "JAVA", but that String is not assigned to a reference variable, so it is immediately available for garbage collection.
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
John,

2 � String s = new String("abc"); // creates two objects, and one
// reference variable
In this case, because we used the new keyword, Java will create a new String
object in normal (nonpool) memory, and s will refer to it. In addition, the literal
�abc� will be placed in the pool.

Correct me if i am wrong....
arent string objects created via newkeyword be operated with intern() to be placed into string literal pool that we are talking about?
Thanks
Sri
Shiva Mantri
Greenhorn

Joined: Dec 30, 2002
Posts: 19
I DIDNT find anywhere that using new would create two String Objects (one in normal memory and other in pool).
What I read was that, using new would create a String object outside the pool and you use to .intern() to add it to pool and it would return the reference of this added String. If the string already exists in the pool, this reference is returned.
[ January 15, 2003: Message edited by: Shiva Mantri ]
[ January 15, 2003: Message edited by: Shiva Mantri ]
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Originally posted by Shiva Mantri:
I DIDNT find anywhere that using new would create two String Objects (one in normal memory and other in pool).

The new operator only creates 1 String object.
When a class is loaded, all the String literals used by the class are loaded as Strings into the String pool, unless the String pool already contained those Strings.
String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1 == s2);
When new String("abc") is executed, Java creates a copy of the "abc" String that is in the literal pool. If that were not so, the preceding code would print true. Run it and you will see that it prints false. False means that s1 and s2 refer to 2 different objects, so there must exist two String objects containing "abc".
[ January 15, 2003: Message edited by: John Paverd ]
Shiva Mantri
Greenhorn

Joined: Dec 30, 2002
Posts: 19
snippet from Kathy's book
2 � String s = new String("abc"); // creates two objects, and one
// reference variable
In this case, because we used the new keyword, Java will create a new String
object in normal (nonpool) memory, and s will refer to it. In addition, the literal
�abc� will be placed in the pool.
The question is "using new alone, would JVM place the literal in the pool"? Does new create two String objects?

John Paverd response:
The new operator only creates 1 String object.
This is what I have been assuming, till I encountered this original post. I think this is true and there is errata in the book. If not, we need to be enlightned, with new operator creating two objects.

John Paverd response:
String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1 == s2);
When new String("abc") is executed, Java creates a copy of the "abc" String that is in the literal pool. If that were not so, the preceding code would print true. Run it and you will see that it prints false. False means that s1 and s2 refer to 2 different objects, so there must exist two String objects containing "abc".
I dont disagree with this at all.
[ January 15, 2003: Message edited by: Shiva Mantri ]
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
If you were to read the entire Strings chapter of Kathy's book, the section I posted would make sense. It was my mistake to post a small portion, which, when taken out of context, led to confusion.
I have found the book very useful in studying for the exam, as have others on this forum. I thought that it explained things very clearly.
So, once again, I apologize for causing confusion. The fault is mine, not the book's.
Shiva Mantri
Greenhorn

Joined: Dec 30, 2002
Posts: 19
John
I wanted to mention that you may have not pasted code properly, in my previous post. but ignored it.
So, no errata in the book. cooll.. I have RHE book, but want to take a look at Kathy/Bert book. Everybody in this forum are really appreciating it. will check it out. PEACE
Good Luck
-Shiva
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy! Errata is coming, by the way, SOON. And thanks to everyone who is helping put that together, but most especially Jessica and the, "doesn't miss ANYTHING and appears to have memorized the spec" John Paverd
For the exam, you need to know that IF you use 'new' to create a new String that is not *already* in the pool, TWO String objects are created. But there is no need to ever have duplicates in the String pool, so if the String is already in the pool at the time you use 'new', there will be only ONE created.
I will say that there is very little on the exam that deals with String pool issues. One issue that you *might* need to keep in mind, however, is that code like the following:
String s = "foo";
String g = s + "bar";
will imply that there are THREE rather than TWO String objects in the pool: "foo" (the first one); "bar" (the literal in the second statement), and "foobar" (the result of concatenating s + "bar").
What you will NOT see is the following variation:
String s = "foo";
String g = "bar";
String z = s + g + "baz";
Because NOW how many String objects are there? We don't know for sure whether there are really four or five. We DO know that there will be the following:
1) "foo"
2) "bar"
3) "baz"
4) "foobarbaz"
What we do NOT know is whether there will be an intermediate, "foobar", created by the concatenation of s+g. According to the spec, Java has the option of optimizing that away and not making/keeping that intermediate concatenation as a separate object.
But... how do we KNOW that those Strings aren't already in the pool because of some OTHER code?
On the exam, assume that the code you see is the ONLY code that matters. In other words, you do NOT have to speculate, "Hmmmmm... but what if some other code somewhere has already caused those String literals to be created?"
If you do not see it happening in the code example in the question, assume that it did not happen.
Which brings me to another point about the exam overall -- you ARE to assume that when a question is asked, everything that you do not see is working normally. In other words, if you see a question about assertion code, ASSUME that assertions are enabled properly. If you see a question that does not show a main method calling the method shown in the question, ASSUME that the method you see is called correctly... that somewhere the question is invoked properly.
We do this because you should never be expected to imagine all the things that could go wrong, unless the things that can go wrong are part of the question. For example, it really would be ridiculous if you had to ask for each question, "OK, but can we assume the programmer has the path variable set correctly?" or even more absurd, "Are we to assume that the programmer has installed Java?" (otherwise, of course, NOTHING would work
So I guess my advice is this:
* Assume that everything is installed and working correctly, and that anything else related to the question works.
* Assume that if you are shown a method and asked for the result, that other code you do not see has correctly invoked that method.
* Assume that if a particular thing is important for the question, it will be part of the question or answer -- there are NO 'ambiguous' questions if you really understand what is being asked, and if you make the two assumptions above.
Of course there ARE times when you cannot make assumptions -- especially with garbage collection. For example, if you see a question that asks at what point an object becomes eligible, and yet the question shows that a reference to the object is passed into a method (a method that you do NOT see), you simply cannot answer that question with certainty, because you do not KNOW what the method is doing with that parameter (the reference it just received).
So not to give anything away here... but if *I* saw a question like that, hypothetically of course, I would probably not select any of the answers that say with CERTAINTY exactly what would happen.
My advice also is to become familiar with the word "indeterminate" as in, "impossible to determine from the information given".
(you'll see issues like this for both thread and gc questions in particular)
And since I'm here, I might as well mention something that I've been getting a lot of questions about from confused readers -- the questions that say,
"Which two lines, inserted independently at line 16, would allow the code to compile?"
When you see a question like that, it means that "If you could pick only ONE of these lines that -- inserted at line 16 -- would allow the code to compile, which would you pick?"
And then answer it twice.
(gee Kathy, that just cleared it RIGHT up
OK, let me try again
It means that you cannot put any TWO of the answers together. You have to treat EACH individual answer like a true/false question, all by itself, without considering any of the other answers. Because sometimes, you might see a question like that and realize that if you inserted both, say, B AND C together, the code would compile. But that is not how you are allowed to answer those "which two inserted independently..." questions.
You must look at -- and evaluate -- each answer separately (i.e. independently).
(personally, I argued that we should not use this wording on the real exam, but I am not -- as we say here -- high enough on the totem pole to have won that battle
cheers,
Kathy

Errata coming soon... we promise!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question to Kathy - from your book