@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 .
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.
@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 .
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!
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?
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!
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 : 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?
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.
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 !!
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.