Forums Register Login

String handling.

+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
@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 .
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
@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 .
1
+Pie Number of slices to send: Send
 

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!



1
+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
@ 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?
+Pie Number of slices to send: Send
@ 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 ..
1
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
 

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 !!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Ok . I havent seen the cd yet ! Thanks for helping .
+Pie Number of slices to send: Send
C Halbe @

Great Explanation!!!I wonder if this could be explained in any better way!!!Thanks a lot!!..
+Pie Number of slices to send: Send
Precedence of == is much less than +

Link
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2049 times.
Similar Threads
doubt in String
comparing Strings
Simple Q on Strings
Why would this print true?
equals() and ==, doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:02:06.