05/07/2016
Try these programs that i found some miner's difference that some times ignored and that the cause of fails in interviews.
Please try these programs:
package garun.mishra;
public class Test {
public static void main(String[] args)
{
/*
Program 1:
String x = new String("xyz");
String y = "abc";
x = x + y;
System.out.println("String = "+x);
// Four object will create for String class
output: Sstring = abcxyz
*/
/*
Program 2
int result = 0;
short s = 42;
Long x = new Long("42");
System.out.println("result = " + x);
Long y = new Long(42);
System.out.println("result = " + y);
Short z = new Short("42");
Short x2 = new Short(s);
Integer y2 = new Integer("42");
Integer z2 = new Integer(42);
if (x == y)
result = 1;
if (x.equals(y) )
result = result + 10;
if (x.equals(z) )
result = result + 100;
if (x.equals(x2) )
result = result + 1000;
if (x.equals(z2) )
result = result + 10000;
System.out.println("result = " + result);
Output: 10
*/
/*
Program 3:
int result = 0;
Boolean b1 = new Boolean("TRUE");
Boolean b2 = new Boolean("true");
Boolean b3 = new Boolean("tRuE");
Boolean b4 = new Boolean("false");
if (b1 == b2) // false because it checks by reference
result = 1;
if (b1.equals(b2) ) // True So result = 0+10 = 10
result = result + 10;
if (b2 == b4) // False because of reference are different
result = result + 100;
if (b2.equals(b4) ) // False because of values are different
result = result + 1000;
if (b2.equals(b3) )
// True Because of contents are same. No matter they are in captal letter
//or small letter because of String constructors are case insensitive. So 10000+10 = 10010
result = result + 10000;
System.out.println("result = " + result);
Output: 10010
*/
/*
Program 4:
int result = 0;
Test oc = new Test();
Object o = oc;
if (o == oc)
result = 1;
if (o != oc)
result = result + 10;
if (o.equals(oc) )
result = result + 100;
if (oc.equals(o) )
result = result + 1000;
System.out.println("result = " + result); // 1101
Explaination: Even though o and oc are reference variables of different types, they are both referring to the same object.
This means that == will resolve to true and that the default equals() method will also resolve to true.
*/
/* Program 5 :
String x = "xyz";
x.toUpperCase(); // Line 2
String y = x.replace('Y', 'y');
y = y + "abc";
System.out.println(y);
Output: xyzabc
Explaination:
Line 2 creates a new String object with the value "XYZ",
but this new object is immediately lost because there is no reference to it.
Line 3 creates a new String object referenced by y. This new String object has the value "xyz"
because there was no "Y" in the String object referred to by x.
Line 4 creates a new String object, appends "abc" to the value "xyz", and refers y to the result
*/
/*
Program 6:
double value = -9.0;
System.out.println( Math.sqrt(value));
Output: NaN(Not a Number)
Eplaination: The sqrt() method returns NaN (not a number) when it's argument is less than zero.
*/
/*
Program 7:
String a = "ABCD";
String b = a.toLowerCase();
b.replace('a','d');
b.replace('b','c');
System.out.println(b);
// Output: abcd
Explaination :
String objects are immutable, they cannot be changed, in this case we are talking about the replace method which
returns a new String object resulting from replacing all occurrences of oldChar in this string with newChar.
b.replace(char oldChar, char newChar);
But since this is only a temporary String it must either be put to use straight away i.e.System.out.println(b.replace('a','d'));
Or a new variable must be assigned its value i.e. String c = b.replace('a','d');
*/
}
}
: Happy to Help you (Garun Mishra)