aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections API & Strings 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 "Collections API & Strings" Watch "Collections API & Strings" New topic
Author

Collections API & Strings

Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL
I hope I am not re-posting any questions that have already been discussed before.
Firstly I have a question on Collections API, What do we need to know about them for the Exam ???
Secondly I have a question on Strings that I am unclear about.
The question is from the chapter 8 test on R.H.E.
Here is the question:
--------------------------------------------------
Which one statement is true about the code below :
String s1 = "abc" + "cde";
String s2 = new String(s1);
if(s1==s2)
System.out.println(" == Succeeds ");
if (s1.equals(s2))
System.out.println(" .equals() succeeds ");
Answer:
A) Line 4 and 6 both execute.
B) Line 4 executes, line 6 does not.
C) Line 6 executes and line 4 does not.
D) Neither line 4 nor 6 execute.
Answer: C.
Since s1 and s2 are references to 2 different objects the == test fails. However the String contained within the 2 String Objects are identical, so the equals() test passes.
--------------------------------------------------
I understand why the equals() test works but do not understand why the == test fails.
I thought that If 2 Strings are the same, Java only holds ONE copy of it in the pool of Strings. This is how Java ensures maximum efficiency on memory. Therefore I would have gone for answer A, as both s1 and s2 refer to the same String.
Does using the Constructor of the String class affect String construction in any way ???
Any contribution would be greatly Appreciated.
Thanks in advance.
Naveen Arumugam
Greenhorn

Joined: Mar 10, 2001
Posts: 23
Hi Zahid,
String s1 = "abc" + "cde";
String s2 = new String(s1);
if(s1==s2)
System.out.println(" == Succeeds ");
if (s1.equals(s2))
System.out.println(" .equals() succeeds ");
Here ".equals() succeeds" will printed.
if u change
String s2 = new String(s1);
into
String s2 = s1;
then

" == Succeeds " will be printed.
The reason is , only when You assign some reference [or "string"] java will make an efficiency. whenever u use new it creates one more object. So, both are different objects.
Hope U understood!
Rgds
ANaveenS


ANaveenS
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL
Thanks Naveen.
But does this mean that The pool of Strings CAN contain duplicate Strings ??? If so it is not very efficient is it
Is the second object put in the pool of String ???

Thanks in advance.

[This message has been edited by Zahid, Butt (edited March 19, 2001).]
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL
would we need to deal with/write code related to the Collections API in the SCJP2 Exam ???
Thanks in advance.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Yes collections are on the exam. You need to know Set, List, and Map (which does NOT implement Collection). I think you probably need to know ArrayList in particular and Vector also.
String LITERALS (which are "special" objects) are kept in a constant pool. String OBJECTS created with the "new" operator are created on the heap separate from the constant pool. So s1==s2 fails because the references point to different locations.


"JavaRanch, where the deer and the Certified play" - David O'Meara
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
Dear Zahid,
In the first case it will be taken as a String literal. This is inside the String pool. It is not an object. But when u create another String with a new Operator, it gives a String object. Now == compares two object references. You are tring to compare a String literal in the String pool to a String Object and hence it will return false.
The equals() method in String is overridden to check for the contents and hence yields true.
I hope my understanding is correct. Request gurus to correct me if iam wrong.
[This message has been edited by tvs sundaram (edited March 19, 2001).]
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL
Thanks for your explanations Cindy, tvs sundaram and everyone else. The concept is becoming more and more clear.
However, I was told by a colleague that when you create a new String object in the following manner:
String s2 = new String (s1);
that " 2 " Strings are created, how and why does this happen ??
As Cindy explained that 1 String will be put on the Heap, but where will the other reside ??
All explanations are greatly appreciated.
Thanks in advance.
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL
Could someone please clear my query regarding 2 String objects soon, as It is quite URGENT.
Thanks in advance.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well, if s1 was ALREADY a string on the heap (I am making that assumption), and you create a new String referenced in s2, that is 2.
[This message has been edited by Cindy Glass (edited March 20, 2001).]
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL
Cindy, If s1 was a String Literal (in the pool of Strings), hence NOT on the heap, then what ???

Thanks in advance.
Slightly Uber
Greenhorn

Joined: Mar 21, 2001
Posts: 7
Every time you create a NEW string it is a new OBJECT - thus it cannot be == . You are dealing with 2 seperate OBJECTS and == checks to see if the operand on the left is the same OBJECT as the operand on the right. the String .equals() checks to see if the operand on the left holds the same VALUE as the operand on the right.
I hope this clears it up a little for you =). Not trying to yell, just stressing the important parts
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Thanks for the answer.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Collections API & Strings