Hi all, I am preparing for SCJP 1.4 and found this question in EXAM CRAM2 Java 2 Programmer (page 59). How many String objects are created in the following code? 1. String A, B, C; 2. A = new String( "1234" ) ; 3. B = A; 4. C = A + B; A. One B. Two C. Three D. Four My guess is C. Three. Since line 2 A = new String( "1234" ) ; would create 2 String objects, and line 4 C = A + B; created another one. But the book's answer is B. Two Could anyone please explain it. Is there a way that we can check the answer programmatically? Thanks. cn
Sefa Urgenc
Ranch Hand
Joined: Jul 25, 2002
Posts: 34
posted
0
You can try to invoke String methods on those references, and if they are objects they should not give you NullPointerException (I think I should try it, before mentioning it duhh!!). Sorry that's all I can think of.
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1865
posted
0
hi Carolyn the only thing i see is- the question asks "String Objects". the two strings that are created in A = new String("1234"); are essentially, 1. String Object reference A 2. string "literal" "1234" but out of these String Object is only one. so in place you counted 2, its only 1 "String Object" which leads to the answer B. regards maulin
so..in essence i mean- string literal wasn't considered "String Object" regards maulin
Sefa Urgenc
Ranch Hand
Joined: Jul 25, 2002
Posts: 34
posted
0
Well I did invoke string method length() on those references, and they seem to be working. I do not know how else to classify a if a String reference is an object.
You get the output of 4 4 8 There is a similar example on the K&B (if you have it) page 359.
"compiler automatically creates a new String object for every literal string it encounters, you can use a literal string to initialize a String. " and it goes on to describe that the following line of code String s = new String("Hola Mundo"); would create two Strings. "The compiler creates the first string when it encounters the literal string "Hola Mundo!", and the second one when it encounters new String. "
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
posted
0
Hi Carolyn The answer is three. But I guess that this type shouldn't be there on the exam because I don't think that asking such questins would make much sense.
Damien Howard
Ranch Hand
Joined: Apr 01, 2003
Posts: 456
posted
0
K&B's book has a similar example and they count it as three. The K in K&B is one of the exam co-authors, so if it comes up on the exam, I would go with three.
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1865
posted
0
yeah this question has always results into long threads of discussion as there seems uncertainty in definition or interpretation and what not ... to me, i would agree w/ ppl here that this sort of question should not be there in exam but if it is just go w/ what u think. none is gonna take away life from u for ONLY ONE wrong answer out of all if u know what i mean regards maulin
pradeepbill arumalla
Greenhorn
Joined: Jul 15, 2003
Posts: 19
posted
0
totally at the end of the program we have two different strings so two objects obviously,since the strings which are same are treated as a single object(excluding some special cases),we have 2 string objects.
pradeep
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.