Relational Operator
Java covers six relational operators (<, <=, >, >=, ==, and !=). Relational
operators always result in a boolean value. This boolean value is
most often used in an if test, as follows,
int x = 6;
if (x < b =" 10"> 99;
System.out.println("The value of b is " + b);
}
}
Java has four relational operators that can be used to compare any combination of
integers, floating-point numbers, or characters:
1. > greater than
2. >= greater than or equal to
3. < style="color: rgb(0, 0, 153);">"Equality" Operators
Java also has two equality operators that
compare two similar "things" and return a boolean the represents what's true about
the two "things" being equal. These operators are
1.== equals (also known as "equal to")
2.!= not equals (also known as "not equal to")
Each individual comparison can involve two numbers (including char), two
boolean values, or two object reference variables. You can't compare incompatible
types, however. What would it mean to ask if a boolean is equal to a char? Or if a
Button is equal to a String array?There are four different types of things that can be tested:
numbers
characters
boolean primitives
Object reference variables
The following code shows some equality tests on primitive variables:
class Compare {
public static void main(String[] args) {
System.out.println("char 'a' == 'a'? " + ('a' == 'a'));
System.out.println("char 'a' == 'b'? " + ('a' == 'b'));
System.out.println("5 != 6? " + (5 != 6));
System.out.println("5.0 == 5L? " + (5.0 == 5L));
System.out.println("true == false? " + (true == false));
}
}
This program produces the following output:
character 'a' == 'a'? true
character 'a' == 'b'? false
5 != 6? true
5.0 == 5L? true
true == false? false
Don't mistake = for == in a boolean expression. The following code is legal:
boolean b = false;
if (b = true) { System.out.println("b is true");
} else { System.out.println("b is false"); }
You might be tempted to think the output is b is false
but look at the boolean test in line 2. The boolean variable b is not being compared to
true, it's set to true, so the println executes and we get b is true .
Equality for Reference Variables
Two reference variables can refer to the same object, as the
following code demonstrates:
JButton a = new JButton("Enter");
JButton b = a;
After running this code, both variable a and variable b will refer to the same object
.Reference variables can be tested to see if they
refer to the same object by using the == operator. 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.
Look at the following code:
import javax.swing.JButton;
class CompareRef {
public static void main(String[] args) {
JButton a = new JButton("Enter");
JButton b = new JButton("Exit");
JButton c = a;
System.out.println("Is reference a == b? " + (a == b));
System.out.println("Is reference a == c? " + (a == c));
}
}
This code creates three reference variables. The first two, a and b, are separate
JButton objects that happen to have the same label. The third reference variable, c,
is initialized to refer to the same object that a refers to. When this program runs, the
following output is produced:
Is reference a == b? false
Is reference a == c? true
This shows us that a and c reference the same instance of a JButton.
Equality for Enums
Once you've declared an enum, it's not expandable.Of course, you can have
as many variables as you'd
like refer to a given enum constant, so it is important to be able to compare two
enum reference variables to see if they're "equal,". You can use either the == operator or the equals() method to determine if two variables are referring to the same enum constant:
class Enum{
enum Color {RED, BLUE} ;
public static void main(String[] args) {
Color c1 = Color.RED; Color c2 = Color.RED;
if(c1 == c2) { System.out.println("=="); }
if(c1.equals(c2)) { System.out.println("dot equals"); }
} }
output:-
==
dot equals
instanceof Comparison
The instanceof operator is used for object reference variables only, and you can
use it to check whether an object is of a particular type. By type, we mean class or
interface type.The following simple example
public static void main(String[] args) {
String s = new String("fun");
if (s instanceof String) {
System.out.print("s is a String");
}
}
prints this: s is a String
Even if the object being tested is not an actual instantiation of the class type on
the right side of the operator, instanceof will still return true if the object being
compared is assignment compatible with the type on the right.
The following example demonstrates a common use for instanceof: testing an
object to see if it's an instance of one of its subtypes, before attempting a "downcast":
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");
}
}
instanceof Comparison
The preceding code compiles and produces the output:
'a' refers to a B
You can test an object reference against its own class type, or any of its
superclasses. This means that any object reference will evaluate to true if you use
the instanceof operator against type Object, as follows,
B b = new B();
if (b instanceof Object) {
System.out.print("b is definitely an Object");
}
which prints this: b is definitely an Object
Look for instanceof questions that test whether an object is an instance
of an interface, when the object's class implements the interface indirectly. An indirect
implementation occurs when one of an object's superclasses implements an interface,
but the actual class of the instance does not—for example
interface Foo { }
class A implements Foo { }
class B extends A { }
...
A a = new A();
B b = new B();
the following are true:
a instanceof Foo
b instanceof A
b instanceof Foo // implemented indirectly
Remember:-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
In addition, it is legal to test whether the null reference is an instance of a class.
This will always result in false, of course. For example:
class Instance {
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
Remember that arrays are objects, even if the array is an array of
primitives. Watch for questions that look something like this:
int [] nums = new int[3];
if (nums instanceof Object) { } // result is true
An array is always an instance of Object. Any array
Useful Links :-
Assignment Operators
Arithmetic Operators
Logical operators
Conditional Operators
Bitwise Operator Intro
Bitwise Logical operator
Bitwise Left shift Operator
Bitwise Right shift Operator
Sponsored Links :-
Get Your SCJP preparation Kit free!!!!!!!
Get Your SCWCD preparation Kit free!!!
Are you Intelligent check here


















0 comments:
Post a Comment
Post a Comment