Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how does package affect comparison of object? 1

Status
Not open for further replies.

prosper

Programmer
Sep 4, 2001
631
HK
It is a scjp question and I cannot compile the program.
Consider following classes:
//In File Other.java
package other;
public class Other
{public static String hello = "Hello";}

//In File Test.java
package testPackage;
class Test
{
public static void main(String[] args)
{
String hello = "Hello", lo = "lo";
System.out.print((testPackage.Other.hello == hello) + " "); //line 1
System.out.print((other.Other.hello == hello) + " "); //line 2
System.out.print((hello == ("Hel"+"lo")) + " "); //line 3
System.out.print((hello == ("Hel"+lo)) + " "); //line 4
System.out.println(hello == ("Hel"+lo).intern()); //line 5
}
}
class Other
{ static String hello = "Hello";}

What will be the output of running class Test? Select 1 correct option

(1) false false true false true
(2) false true true false true
(3) true true true true true
(4) true true true false true
(5) None of the above

The answer is 4
I use winxp.
*** I cannot compile the program and type javac testPackageTest.java in dos prompt at directory n.
here is my file list
I have a directory n. Directory n contains two subdirectories testPackage and other. Subdirectory testPackage contains file Test.java. Subdirectory other contains file Other.java

*** I would like to ask why line1, line2 and line3 give true result?

Thanks for any help!
 
For a complete explanation you will have to look at the "String" class and the "intern()" method, but I'll try to explain it in a nutshell.

- Two objects are never equal using reference equality unless they are the same object.
- Thus the Strings in your example, when the (reference) comparison returns true, must be (point to) the same object.
- This is so because the String class maintains a pool of strings. Every string in the pool of strings is unique.
... etc...etc... The 'compiler / classloader / JVM' do some special stuff to guarantee this...

The reason why line 4 gives false is because ("Hel"+lo) is a "computed string" (and not a "compile-time"). The "intern()" method is used to fix this "problem" as you can see at line 5 returning true.

===================
From the Sun source
===================
/**
* Returns a canonical representation for the string object.
* <p>
* A pool of strings, initially empty, is maintained privately by the
* class <code>String</code>.
* <p>
* When the intern method is invoked, if the pool already contains a
* string equal to this <code>String</code> object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise, this <code>String</code> object is added to the
* pool and a reference to this <code>String</code> object is returned.
* <p>
* It follows that for any two strings <code>s</code> and <code>t</code>,
* <code>s.intern() == t.intern()</code> is <code>true</code>
* if and only if <code>s.equals(t)</code> is <code>true</code>.
* <p>
* All literal strings and string-valued constant expressions are
* interned. String literals are defined in §3.10.5 of the
* <a href=&quot; Language
* Specification</a>
*
* @return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/
public native String intern();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top