GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java Operators
Selected Reading
Java OperatorsJava operators are the symbols that are used to perform various operations on variables and values. By using these operators, we can perform operations like addition, subtraction, checking less than or greater than, etc. There are different types of operators in Java, we have listed them below −
Java Arithmetic OperatorsArithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators − Assume integer variable A holds 10 and variable B holds 20, then −
ExampleThe following example demonstrates the usage of arithmetic operators in Java: public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 5;
// Addition
System.out.println("a + b = " + (a + b)); // 15
// Subtraction
System.out.println("a - b = " + (a - b)); // 5
// Multiplication
System.out.println("a * b = " + (a * b)); // 50
// Division
System.out.println("a / b = " + (a / b)); // 2
// Modulus
System.out.println("a % b = " + (a % b)); // 0
}
}
When the above code is compiled and executed, it produces the following result − a + b = 15 a - b = 5 a * b = 50 a / b = 2 a % b = 0 Java Assignment OperatorsAssignment Operators are used to assign values to variables. These operators modify the value of a variable based on the operation performed. The most commonly used assignment operator is =, but Java provides multiple compound assignment operators for shorthand operations. Following are the assignment operators supported by Java language −
ExampleThe following example demonstrates the usage of assignment operators in Java: public class AssignmentExample {
public static void main(String[] args) {
int a = 10;
// Assign and add
a += 5;
System.out.println("a += 5: " + a); // 15
// Assign and subtract
a -= 3;
System.out.println("a -= 3: " + a); // 12
// Assign and multiply
a *= 2;
System.out.println("a *= 2: " + a); // 24
// Assign and divide
a /= 4;
System.out.println("a /= 4: " + a); // 6
// Assign and modulus
a %= 5;
System.out.println("a %= 5: " + a); // 1
}
}
When the above code is compiled and executed, it produces the following result − a += 5: 15 a -= 3: 12 a *= 2: 24 a /= 4: 6 a %= 5: 1 Java Relational OperatorsRelational operators are used to compare two values. These operators return a boolean result: true if the condition is met and false otherwise. Relational operators are commonly used in decision-making statements like if conditions and loops. There are following relational operators supported by Java language. Assume variable A holds 10 and variable B holds 20, then −
ExampleThe following example demonstrates the usage of relational operators in Java: public class RelationalExample {
public static void main(String[] args) {
int A = 10, B = 5;
System.out.println("A == B: " + (A == B)); // false
System.out.println("A != B: " + (A != B)); // true
System.out.println("A > B: " + (A > B)); // true
System.out.println("A < B: " + (A < B)); // false
System.out.println("A >= B: " + (A >= B)); // true
System.out.println("A <= B: " + (A <= B)); // false
}
}
When the above code is compiled and executed, it produces the following result − A == B: false A != B: true A > B: true A < B: false A >= B: true A <= B: false Java Logical OperatorsLogical operators are used to perform logical operations on boolean values. These operators are commonly used in decision-making statements such as if conditions and loops to control program flow. The following table lists the logical operators − Assume Boolean variables A holds true and variable B holds false, then −
ExampleThe following example demonstrates the usage of logical operators in Java: public class LogicalExample {
public static void main(String[] args) {
boolean A = true, B = false;
System.out.println("A && B: " + (A && B)); // false
System.out.println("A || B: " + (A || B)); // true
System.out.println("!A: " + (!A)); // false
System.out.println("!B: " + (!B)); // true
}
}
When the above code is compiled and executed, it produces the following result − A && B: false A || B: true !A: false !B: true Java Bitwise OperatorsBitwise operators are used to perform operations at the binary (bit) level. These operators work on individual bits of numbers. They are commonly used in low-level programming, encryption, and performance optimization. Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte. Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and b = 13; now in binary format they will be as follows − a = 0011 1100 b = 0000 1101 a&b = 0000 1100 a|b = 0011 1101 a^b = 0011 0001 ~a = 1100 0011 The following table lists the bitwise operators − Assume integer variable A holds 60 and variable B holds 13 then −
ExampleThe following example demonstrates the usage of bitwise operators in Java: public class BitwiseExample {
public static void main(String[] args) {
int A = 60; // 0011 1100
int B = 13; // 0000 1101
System.out.println("A & B: " + (A & B)); // 12 (0000 1100)
System.out.println("A | B: " + (A | B)); // 61 (0011 1101)
System.out.println("A ^ B: " + (A ^ B)); // 49 (0011 0001)
System.out.println("~A: " + (~A)); // -61 (1100 0011 in 2's complement)
System.out.println("A << 2: " + (A << 2)); // 240 (1111 0000)
System.out.println("A >> 2: " + (A >> 2)); // 15 (0000 1111)
System.out.println("A >>> 2: " + (A >>> 2)); // 15 (0000 1111)
}
}
When the above code is compiled and executed, it produces the following result − A & B: 12 A | B: 61 A ^ B: 49 ~A: -61 A << 2: 240 A >> 2: 15 A >>> 2: 15 Java Miscellaneous OperatorsThere are few other operators supported by Java Language. Conditional Operator ( ? : )Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as − variable x = (expression) ? value if true : value if false ExampleFollowing is an example − In this example, we're creating two variables a and b and using ternary operator we've decided the values of b and printed it. public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
Output Value of b is : 30 Value of b is : 20 instanceof OperatorThis operator is used only for object reference variables. The operator checks whether the object is of a particular type (class type or interface type). instanceof operator is written as − ( Object reference variable ) instanceof (class/interface type) If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true. Following is an example − ExampleIn this example, we're creating a String variable name and then using instanceof operator we've checking the name is of String or not. public class Test {
public static void main(String args[]) {
String name = "James";
// following will return true since name is type of String
boolean result = name instanceof String;
System.out.println( result );
}
}
Output true This operator will still return true, if the object being compared is the assignment compatible with the type on the right. Following is one more example − ExampleIn this example, we're creating a variable a of class Vehicle and then using instanceof operator we've checking the name is of type Car or not. class Vehicle {}
public class Car extends Vehicle {
public static void main(String args[]) {
Vehicle a = new Car();
boolean result = a instanceof Car;
System.out.println( result );
}
}
Output true Java Operators Precedence & AssociativityOperator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator − For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3 * 2 and then adds into 7. Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
ExampleThe following example demonstrates how operator precedence and associativity affect expression evaluation in Java: public class OperatorPrecedenceExample {
public static void main(String[] args) {
int result1 = 10 + 5 * 2; // Multiplication (*) has higher precedence than addition (+)
int result2 = (10 + 5) * 2; // Parentheses () change precedence
int result3 = 20 / 4 * 2; // Left-to-right associativity
int result4 = 10 - 3 + 2; // Left-to-right associativity
System.out.println("10 + 5 * 2 = " + result1);
System.out.println("(10 + 5) * 2 = " + result2);
System.out.println("20 / 4 * 2 = " + result3);
System.out.println("10 - 3 + 2 = " + result4);
}
}
When the above code is compiled and executed, it produces the following result − 10 + 5 * 2 = 20 (10 + 5) * 2 = 30 20 / 4 * 2 = 10 10 - 3 + 2 = 9 Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |