+ 2

Why?

Can someone explain me, please? I failed this question because I chose a, b, d. WHY? String s1 = "Hello world"; String s2 = s1; What is true about the statement above? Question options: a)The s1 variable holds the string "Hello world". b)The s2 variable holds the string "Hello world". c)The s1 variable holds a reference to the string "Hello world". d)The s2 variable holds a reference to the string "Hello world".

22nd Jan 2018, 3:54 AM
DIY Mods
DIY Mods - avatar
8 ответов
+ 6
If the 2nd line was written as: String s2 = new String("Hello world"); Then, s2 would definitely match (option D). The reason, according to the link I listed below, is "a String object is created out of the String literal pool, even if an equal string already exists in the pool." If s2 was matched with Option D, then I would assume s1 would better match option A. Again, I believe these options, as well as, the intent of the question are ambiguous and could be better written. http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3
22nd Jan 2018, 5:59 AM
David Carroll
David Carroll - avatar
+ 6
@DIY Mods: Can you confirm if the correct options are (A and B) or (C and D)? It seems like the question is attempting to differentiate between string literals (s1) and string objects (s2). However, the wording of the options are a bit ambiguous. On one hand, string literals are interned at compile time, which is a method of storing only a single, distinct copy in a pool of string values. My understanding is s1 holds a reference to an object in the string literal pool. However, it's not clear whether or not option A or option C best describes this. I'll assume that (option C) is intended to describe this reference for s1. If so, then, s2 = s1 would essentially point s2 to the same object reference as s1's reference to the string object in the string pool (option D).
22nd Jan 2018, 5:44 AM
David Carroll
David Carroll - avatar
+ 5
@Tim... How does s2 become s1 and how does s2 no longer exist?
25th Jan 2018, 6:55 AM
David Carroll
David Carroll - avatar
+ 4
That's unfortunate. Sounds like the class is still in the process of being refined. Questions like these are a bit too "clever" for their own good. Hang in there and good luck. 😉
22nd Jan 2018, 6:19 AM
David Carroll
David Carroll - avatar
+ 4
the answer is A. s2 became s1 therefore s2 no longer exists s1 holds the value Hello World
25th Jan 2018, 6:37 AM
Tim Millar
Tim Millar - avatar
+ 1
I'm dealing with this kind of questions entire class. My scores for quizzes never been so bad. Ever. Sometimes question asks for results of the code, but there is no semicolon as in statement right next to it, so it shouldn't compile at all, but it still tells me that I'm wrong :c
22nd Jan 2018, 6:08 AM
DIY Mods
DIY Mods - avatar
+ 1
@Tim this question has many answers
25th Jan 2018, 9:24 PM
DIY Mods
DIY Mods - avatar
0
i think answer a and b are true and d is wrong because by String s2 = s1; you are assigning the the value of s1 to s2. if after that, you change s1 = "bye world"; s2 will still be "Hello world". if s2 references to s1 it would equal the value of s1 even in case s1 changes.
22nd Jan 2018, 4:51 AM
seamiki
seamiki - avatar