9, actually!Paul Clapham wrote:As for JSP scriptlets, they have been superseded for 7 or 8 years now.
Kim Ming Yap wrote:Thanks for your reply.
The companyID is from here:
companyID = (String) session.getAttribute("UserLDAPOrg");
This is the only place it get is value from.
The reason i am not using .equals is i don't need to.
A string literal like "CGI" is a reference to a string object.
I expect this reference to be the same as companyID.
Why is it not using the intern object?
So why returning false?
However as i mention earlier if you code the simple one from my earlier email .. it always return true!
Steve
Steve Luke wrote:Test this:
What do you get (assuming that is compilable... sorry didn't run it)?
SCJP 6, next stop - OCPJWCD!
When you try to compare, (experts, please correct me if I am wrong here), the companyID == "CGI" part creates a new String object, not referenced by any object; because a new reference variable wasn't created pointing to that object. At this point, you have TWO String objects both with value "CGI". One is being pointed to by ref variable companyID. The second "CGI" object is shown in figure not pointed to by any reference variable.
Ralph Cook wrote:It is not true that a literal in an equals expression necessarily causes creation of an additional, unreferenced object.
SCJP 6, next stop - OCPJWCD!
Ashutosh M Kulkarni wrote:
When intern() is not used, by default, String objects pool more or less work as explained in my post. Though, not exactly.
the companyID == "CGI" part creates a new String object, not referenced by any object...
Your comparison will always return false...
Ralph Cook wrote:
Ashutosh M Kulkarni wrote:
When intern() is not used, by default, String objects pool more or less work as explained in my post. Though, not exactly.
No, "String objects pool" does not work anything like you explained in your post.
You gave two lines of code:
and then said
the companyID == "CGI" part creates a new String object, not referenced by any object...
and
Your comparison will always return false...
This is just wrong, according to the definition of the Java language which I quoted above. This particular fact is not altered by the definition or use of intern(), and I don't think is covered by "Though, not exactly".
None of this has much to do with immutability, either.
rc
SCJP 6, next stop - OCPJWCD!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |