I'm wondering what is the different between
setAttribute("key", new X()); //print "B"
setAttribute("key", "x") // print "UB";
Question 55.
15. req.getSession().setAttribute("key", new X());
16. req.getSession().setAttribute("key", new X());
17. req.getSession().setAttribute("key", "x");
18. req.getSession().removeAttribute("key");
}
Answer: B. BBUBUB
The B/UB are printed only for elements of class X. "x" is a
String... So:
15. new X() is created, prints "B"
16. another X() object is created and bound to session, so "B" is printed. But previous object created in line 15 is removed, so "UB" is printed too.
17. it removes object put in line 16 (so prints "UB") and inserts String object, so nothing more is printed.
So, it prints BBUBUB.
BTW: Personally for me it would be more logical that it prints BUBBUB (eg line 16 should first unbound previous object and then bound new object). But BBUBUB was the only answer that could fit for me while answering this question. And no - I didn't check it
Adam
SCJA, SCJP 5.0, SCWCD 1.4, SCBCD 5, SCEA 5