Operators Flashcards
Are there any overloaded operators in Java?
Yes.
- The + operator can be used to add two numeric primitives together or to perform a concatenation operation if either operand is a String.
- The &, |, and ^ operators can all be used in two different ways.
What is the order of evaluation for this expression?
x *= 2 + 5;
x = x * (2 + 5);
Because the right side of = is always evaluated first.
What are the comparison relational operators in Java?
> Greater than >= Greater than or equal to < Less than <= Less than or equal to Comparison can also be between different types e.g. it's also legal to compare a character primitive with any number. When comparing a character with a character or a character with a number, Java will use the Unicode value of the character as the numerical value, for comparison.
What are the equality relational operators in Java?
== Equal != Not equal You can't compare incompatible types. There are four different types of things that can be tested: * Numbers * Characters * Boolean primitives * Object reference variables (What is left???)
What is the result?
5.0 == 5L?
If a floating-point number is compared with an integer and the values are the same, the == operator usually returns true as expected
What is the result?
boolean b = false;
if (b = true) { System.out.println(“b is true”);
} else { System.out.println(“b is false”); }
b is true
The result of any assignment expression is the value of the variable following the assignment.
What does == look for?
Remember, the == operator is looking at the bits in the variable, so for reference variables, this means that if the bits in both reference variables are identical, then both refer to the same object.
How does equal method in Object class works?
The equals() method in class Object works the same way that the == operator works.
How does equal method in String class works?
When the equals() method is used to compare two
strings, it will return true if the strings have the same value, and it will return false if the strings have different values. (??they should be really the same object in string pool - check this)
How does equals or == work for enums?
It compares if the two operands refer to same enum constant.
What is the result? public class EnumEqual { enum Color {RED, BLUE} // ; is optional public static void main(String[] args) { Color c1 = Color.RED; Color c2 = Color.RED; if(c1 == c2) { System.out.println("== works"); } if(c1.equals(c2)) { System.out.println("equals works"); } } }
== works
equals works
What is instanceof comparison used for?
To check whether an object is of a particular class or interface.
What is the result?
class A { } class B extends A { public static void main (String [] args) { A myA = new B(); m2(myA); } public static void m2(A a) { if (a instanceof B) ((B)a).doBstuff(); // downcasting an A reference // to a B reference } public static void doBstuff() { System.out.println("'a' refers to a B"); } }
‘a’ refers to a B
In examples like this, the use of the instanceof operator protects the program from attempting an illegal downcast.
Given: interface Foo { } class A implements Foo { } class B extends A { } ... A a = new A(); B b = new B();
which are true?
a instanceof Foo
b instanceof A
b instanceof Foo
All. last one is implemented indirectly. An object is said to be of a particular interface type (meaning it will pass the instanceof test) if any of the object’s superclasses implement the interface.
What happans when null is compared using instanceof?
It is legal to test whether the null reference is an instance of a class. This will always result in false, of course. This example, class InstanceTest { public static void main(String [] args) { String a = null; boolean b = null instanceof String; boolean c = a instanceof String; System.out.println(b + " " + c); } } prints this: false false