public class Kirving{
public static void main(String argv[])
{
String s1= new String("gan");
String s2= new String("gan");
if(s1==s2)
System.out.println("true");
else
System.out.println("false");
Integer i1 = new Integer("1");
Integer i2 = new Integer(1);
if(i1==i2)
System.out.println("true");
else
System.out.println("false");
Integer i3 = Integer.valueOf("1");
Integer i4=Integer.valueOf("1");
System.out.println((i3==i4));
Boolean b3 = new Boolean("TRue");
Boolean b4 =new Boolean("true");
System.out.println((b3==b4));
Boolean b1 = Boolean.valueOf("true");
Boolean b2 = Boolean.valueOf("true");
System.out.println((b1==b2) );
}
}
My guess was
false
false
false
false
false
but the answer when compiled and executed was
false
false
false
false
true
I think this code will give compilation error . Because of this line :Integer i1 = new Integer("1");
Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Boolean b1 = Boolean.valueOf("true");
Boolean b2 = Boolean.valueOf("true");
System.out.println((b1==b2) );
Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Mike Gershman
SCJP 1.4, SCWCD in process
Returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE. If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean), as this method is likely to yield significantly better space and time performance.
Mike Gershman
SCJP 1.4, SCWCD in process
"I'm not back." - Bill Harding, Twister
quote:
--------------------------------------------------------------------------------
Returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE. If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean), as this method is likely to yield significantly better space and time performance.
--------------------------------------------------------------------------------
public static Boolean valueOf(String s)Returns a Boolean with a value represented by the specified String. The Boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
Example: Boolean.valueOf("True") returns true.
Example: Boolean.valueOf("yes") returns false.
Parameters:
s - a string.
Returns:
the Boolean value represented by the string.
public static Boolean valueOf(String s) {
return toBoolean(s) ? TRUE : FALSE;
}
private static boolean toBoolean(String name) {
return ((name != null) && name.equalsIgnoreCase("true"));
}
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
Check what will happen if use b1= Boolean.valueOf(true)
and b2= Boolean.valueOf(true)and check for b1==b2 , i guess its false
Returns an Integer object holding the value of the specified String. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) method. The result is an Integer object that represents the integer value specified by the string.
public class Kirving{
public static void main(String argv[])
{
Integer i3 = Integer.valueOf("1");
Integer i4=Integer.valueOf("1");
System.out.println((i3==i4));
Boolean b3 = new Boolean("TRue");
Boolean b4 =new Boolean("true");
System.out.println((b3==b4));
Boolean b1 = Boolean.valueOf("true");
Boolean b2 = Boolean.valueOf("true");
System.out.println((b1==b2) );
i3=i4=b3=b4=b1=b2=null //line 1
}
}
How many objects are eligible for garabge collection and how many objects
were created before that???
"I'm not back." - Bill Harding, Twister
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |