Studydroid is shutting down on January 1st, 2019



keywords:
Bookmark and Share



Front Back
Where does java get it's numeric value for a char?
From it's UNICODE value (not it's ASCII value).
5.0 == 5L True or False?
True.
Animal a = new Animal(); Dog d = new Dog(); Cat c = new Cat(); if (d == c) { System.out.println(""meowoof""); } else { System.out.println(""Huh?""); } What prints?
Compile error. Can't compare uncomparable types.
What does the == operator compare?
The bits in the operand variables. That means that if 2 reference variables point to the same object == evaluates to true otherwise if they point to different objects they evaluate to false. NOTE: Some objects (String objects and wrappers objects in particular) sometimes SEEM like different objects but are the same. String a = ""hi""; String b = ""hi""; a == b [------------ true! String c = new String(""hi""); String d = new String(""hi""); c == d [------------ false! That's because in the latter c/d example while the String literal (""hi"") is shared the new objects created are different objects and thus == evaluates false.
If equals() method is not overridden how does it function?
Exactly like ==
Where does java get it's numeric value for a char?
From it's UNICODE value (not it's ASCII value).
Integer i1 = 128; Integer i2 = 128; if (i1 == i2) { System.out.println(""Same""); } else { System.out.println(""Different""); } What prints?
Different. If the Integers had both been set to 127 it would have printed ""Same"".
Integer i1 = 127; Integer i2 = 127; if (i1 == i2) { System.out.println(""Same""); } else { System.out.println(""Different""); } What prints?
Same. Wrapper integers 127 and below are optimized so that all references share a common object even if to the programmer it appears that multiple objects are being created.
enum Cat {MEOW PURR}; Cat e1 = Cat.MEOW; Cat e2 = Cat.MEOW; if (e1 == e2) {System.out.println(""=="");} if (e1.equals(e2)) { System.out.println(""equals()"");} What prints?
== equals() Both == and equals() evaluate true when different references point to the same enum constant.
Where classes A and Z both descend from Object... A a = new A(); Z z = new Z(); if (a instanceof Z) {System.out.println(""IS"");} else {System.out.println(""IS NOT"");} What prints?
Compiler error. Classes of A and Z are not assignment compatible. Forget what object is actually being pointed at. At compile time we're only concerned with what the reference MIGHT point at. If the TYPE of the reference is unable to hold an object that IS-A kind of Class being compared. that's a compiler error. The simple rule for the compiler safety: If the reference type descends from the class or the class descends from the reference type or the type and class are equal -- then it can be compared (no compiler error) BEWARE OF THIS: Cat c; if (c instanceof Dog) {} Even though c & d both descend from Animal (assumed) the reference type does not descend from the class nor does the class (Dog) descend from the reference type(Cat) nor does the type equal the class... so COMPILER ERROR
A a = new A(); Z z = new Z(); Object o = new Object(); if (o instanceof A) {System.out.println(""IS"");} else {System.out.println(""IS NOT"");}
IS NOT.
Foo[] f = null; f instanceof Foo What happens?
Compiler error. Reference type (Foo[]) and class (Foo) are not equal nor do they descend from each other. If f's reference type was simply Foo then compiler wouldn't complain and the instance of expression would evaluate to false. Null compared to any class evaluates to false.
In a boolean context what is the difference between & and &&
&& short circuits & does not. Same goes for || and |.
int k = 4 / 0; Compiler error?
No. It compiles just fine. At runtime ArithmeticError thrown.
Integer a = new Integer(5); Integer b = new Integer(5); System.out.println(a==b); Integer c = 10; Integer d = 10; System.out.println(c==d); What prints?
false true NOTE: == compares values of references to objects. a and b point to different objects. For c and d autoboxing occurs WITH AN OPTIMIZATION. For values between -128 and 127 autoboxing will use the same object for all instances of a value. In our case the value 10 is not autoboxed into 2 different values but rather 1 object which both c and d are then made to point at.
char c = '\u0000'; int i = 0; boolean b = i == c; System.out.println(b); What prints?
true Compiles and runs fine.
x of y cards