Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String handling. 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 "String handling." Watch "String handling." New topic
Author

String handling.

ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
How many objects are created if i write the following codes in java 1.6 :

String str1="abc";// case 1

String str2=new String("abc");//case 2

Does str1 will also be stored in the some string literal pool or only str2 would be stored in the pool ?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

ayush raj wrote:How many objects are created if i write the following codes

Well, the easiest method to check if two objects are getting created is via reference equality: instead of

Secondly, hard-coded String literals go to String pool. In above code, str1 will go to String pool, whereas str2 won't.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
@Anayonkar Shivalkar : str1==str2 would compare the base addresses of the two objects from heap , while the equals method would check for the contents of the two strings created . Am i right ? But then how could i know if two objects have been created or only one object is created ? Please clarify .
C Halbe
Greenhorn

Joined: Jul 18, 2008
Posts: 20
ayush raj wrote:How many objects are created if i write the following codes in java 1.6 :

String str1="abc";// case 1

String str2=new String("abc");//case 2

Does str1 will also be stored in the some string literal pool or only str2 would be stored in the pool ?


If the 2 cases are taken separately, then

case 1: only one object will be created in the string constant pool and str1 will refer to it

case 2: two objects will be created as we use the 'new' keyword, one on the heap(non-pool memory) and str2 will refer to it and other object in string constant pool - assuming that "abc" is not already present in the pool

ayush raj wrote:But then how could i know if two objects have been created or only one object is created ?


== will tell you if the two references refer to the same object on the heap. In above case (str1==str2), the result will be false.


Correct me if I am wrong.

ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
@C Halbe : And what about equals method? How does it manipulate the strings equality? For the case 1 : does any object exist on the heap ? Totally confused relating to String constant pool and the heap .
C Halbe
Greenhorn

Joined: Jul 18, 2008
Posts: 20
ayush raj wrote:@C Halbe : And what about equals method? How does it manipulate the strings equality? For the case 1 : does any object exist on the heap ?


equals() does not manipulate the strings. It will only compare the contents of the the two string objects and tell you if they are meaningfully equal.

str1.equals(str2) will be true because in literal sense it doing something like "abc".equals("abc");

ayush raj wrote:Totally confused relating to String constant pool and the heap .


String constant pool is a part of the heap, but separately allocated only for String because of the way strings behave in Java. So when you say "in the constant pool" , it is on the heap but inside the string constant pool.

Whenever we create a string, eg.
String s = "abc";
JVM will check the constant pool if "abc" is already there in the pool, if not, it will create "abc", but if it already exists then the existing "abc" object will be allocated to s reference variable.

In case of
String s2 = new String("xyz");
JVM will create a String object in the heap (outside of the pool) because of the 'new' keyword. It will also put "xyz" in the constant pool in the manner I stated above. That is how the JVM works.

To clear your confusing try running this simple code below and checkout the results!



Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Thanks for all of your answer.
Quiz time:

public class StringTest {
public static void main(String[] args) {
String str1 = "abc";
String str2 = new String("abc");
System.out.println(" str1 == str2"+ str1 == str2);
}
}

What is the output?
A. true
B. false
C. str1==str2 true

The answer is .......B !!!
Why ? The first string in the print statement is "str1 == str2 abc " and str2 = "abc". Therefore the first string is not equals to the second one.

Have fun ?

I think Miss Kathy Sierra and Mr. Bert Bates will put this on the real exam !
Want to laugh?

C Halbe
Greenhorn

Joined: Jul 18, 2008
Posts: 20
And you have 100% marks!! false is the answer...oh helen, there is no greater java scholar on this planet than you!!

Anyways, i just wanted Mr. ayush raj to have a clear understanding of where the two "abc" objects will be created. And I think I have proved the point!
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I made a lot of mistakes in the practice exams .... I am nervous because I will take the exam in 2 weeks. That will be my first attempt to take it.

Besides K&B's book, I also read Mughal & Rasmussen's SCJP study guide, the third edition.
This is also a good book with a lot of details.

My tip about taking the exam is to modify the questions in the practice exam questions, just like what I did with your test string example and test myself.
But I really enjoy to type up my thought and logic on the Java Ranch to enhance my study.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I hope you guys don't take my previous post about the System.out.println("str1 == str2 + str1 == str2 ) personal.

I think Miss Sierra and Mr Bates are very smart to write tricky exams so that we will all work hard.

But I have a feeling that that is a very possible exam question.
ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
@ Helen Ma : I could not understand the output of your code . I mean how come

System.out.println("str1==str2"+str1==str2); // this line of code displays only false as the output!! According to me , it should have at least displayed : str1==str2false

How does does str1==str2 is ignored while displaying the output?
ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
@ C Halbe : Thanks for your help in clearing my String concepts . A good explanation was provided by you in this regard . Thanks for the same ..
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi,
the print statement is interpreted in this way : the first string is "str1 == str2 "+ str1 which is "str1 == str2 123". The second string is str2 which is 123.
str1 == str2 123 is not equal to 123 . So, the answer is false.

In one of the mock exam, the writer of the exam tests how we understand the exam:

Suppose we have an int array like this : int array[] = {1,2,3}
case 1 :System.out.println(array[0]+ array[1]+ array[2]) ; //The output is 6 , which is printing the sum

case 2: System.out.println("a"+ array[0]+ array[1]+ array[2]) ; // The output is a123

case 3: System.out.println(array[0]+ array[1]+ array[2]+ "a") ; //The output is 6a instead of 123a.

The reason is that in the println method, when three numerical values comes first , the compiler adds all those numerical values and print it out as a string output. Therefore, the output in case 1 and 3 are 6 and 6a.
In case 2, the compiler sees a string "a" and then treats the rest of the numerical values as string. It concatenate all the strings.

I have a feeling that this type of question will appear in the exam.

ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
Helen Ma :the print statement is interpreted in this way : the first string is "str1 == str2 "+ str1 which is "str1 == str2 123". The second string is str2 which is 123.
str1 == str2 123 is not equal to 123 . So, the answer is false.


A very good explanation indeed . The example of array you gave was clear to me . Where from you think such type of questions ? I mean they are pretty impressive for the exam point of view !!
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
That print statement questions come from the CD that comes with the book.
But in KB's book, they specify that printing is important for the exam. The example is one of them.
ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
Ok . I havent seen the cd yet ! Thanks for helping .
Dilip Mehra
Greenhorn

Joined: Apr 02, 2012
Posts: 8
C Halbe @

Great Explanation!!!I wonder if this could be explained in any better way!!!Thanks a lot!!..
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Precedence of == is much less than +

Link


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
 
jQuery in Action, 2nd edition
 
subject: String handling.
 
Similar Threads
Why would this print true?
equals() and ==, doubt
Simple Q on Strings
comparing Strings
doubt in String