Java Operators Examples
Example 1 : Bitwise Operators
class BitwiseOperators {
public static void main(String args[]){
//!(logical not) It negates the result of the evaluation
System.out.println(!(3<10)); // True but note that (3>7)
returns false // bitwise exclusive OR
System.out.println((0)^(0)); //if one or the other of the operands is true but
not both // bitwise exclusive OR
System.out.println((3>2)^(2>7)); //if one or the other of the operands is true but
not both //bitwise inclusive OR
System.out.println((3>2)|(2>7)); //either op1 or op2 is true, always evaluates op1 and op2
}
}
Example 2: Tenary Operators
class TenaryOperators {
public static void main(String args[]){
//Syntax variable x = (expression)?value if true : value
if false int marks;
marks = 30;
char grade= (marks >= 40) ? 'P': 'F';
System.out.println( "The grade is : " + grade );
}
}
Example 3: Working with string object
class TenaryOperators {
public static void main(String args[]){
int marks;
marks = 60;
String grd;
/ In this case we use String.valueOf() method to convert the passed argument
to a string grd= (marks >= 40) ? String.valueOf("Pass"):
String.valueOf("Fail"); System.out.println( "The grade is : " + grd );
}
}
Example 4: Generating the marks randomly
class Operators {
public static void main(String args[]){
int marks;
/ Generating the marks randomly between 20 and 95
% marks =20+(int)(Math.ceil(Math.random()*95));
String grd;
grd= (marks >= 40) ? String.valueOf("Pass"): String.valueOf("Fail");
System.out.println( "The mark generated is : " + marks + " and the grade is"+ grd);
Example 1 : Bitwise Operators
class BitwiseOperators {
public static void main(String args[]){
//!(logical not) It negates the result of the evaluation
System.out.println(!(3<10)); // True but note that (3>7)
returns false // bitwise exclusive OR
System.out.println((0)^(0)); //if one or the other of the operands is true but
not both // bitwise exclusive OR
System.out.println((3>2)^(2>7)); //if one or the other of the operands is true but
not both //bitwise inclusive OR
System.out.println((3>2)|(2>7)); //either op1 or op2 is true, always evaluates op1 and op2
}
}
Example 2: Tenary Operators
class TenaryOperators {
public static void main(String args[]){
//Syntax variable x = (expression)?value if true : value
if false int marks;
marks = 30;
char grade= (marks >= 40) ? 'P': 'F';
System.out.println( "The grade is : " + grade );
}
}
Example 3: Working with string object
class TenaryOperators {
public static void main(String args[]){
int marks;
marks = 60;
String grd;
/ In this case we use String.valueOf() method to convert the passed argument
to a string grd= (marks >= 40) ? String.valueOf("Pass"):
String.valueOf("Fail"); System.out.println( "The grade is : " + grd );
}
}
Example 4: Generating the marks randomly
class Operators {
public static void main(String args[]){
int marks;
/ Generating the marks randomly between 20 and 95
% marks =20+(int)(Math.ceil(Math.random()*95));
String grd;
grd= (marks >= 40) ? String.valueOf("Pass"): String.valueOf("Fail");
System.out.println( "The mark generated is : " + marks + " and the grade is"+ grd);