Course
Basic Operators
Java Tutorial
This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.
Basic Operators
Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
- Misc Operators
The Arithmetic Operators
Arithmetic 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
The Relational Operators
There are following relational operators supported by Java language.
Assume variable A holds 10 and variable B holds 20, then
The Bitwise Operators
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 1100b = 0000 1101a&b = 0000 1100a|b = 0011 1101a^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
The Logical Operators
The following table lists the logical operators
Assume Boolean variables A holds true and variable B holds false, then
The Assignment Operators
Following are the assignment operators supported by Java language
Miscellaneous Operators
There 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
Following is an example
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 : 30Value of b is : 20
instanceof Operator
This 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
Example
In 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
Example
In 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
Precedence of Java Operators
Operator 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.
What is Next?
The next chapter will explain about loop control in Java programming. The chapter will describe various types of loops and how these loops can be used in Java program development and for what purposes they are being used.