Java Operators: Data Types
Java Operators: Data Types
Java Operators: Data Types
Module 4
Data Types
Java Operators
Operators are used to perform operations on variables and values.
An operator is a symbol that performs a specific kind of operation on one, two, or three
operands, and produces a result.
Operators in Java can be categorized based on two criteria:
• The number of operands they operate on
• The type of operation they perform on the operands
There are three types of operators based on the number of operands. An operator is called
a unary, binary, or ternary operator based on the number of operands.
If an operator takes one operand, it called a unary operator; if it takes two operands, it
called a binary operator; if it takes three operands, it called a ternary operator.
A unary operator can use postfix or prefix notation.
In the postfix notation, the unary operator appears after its operand.
In the postfix notation, the unary operator appears after its operand.
operand operator // Postfix notation
For example, num++; // num is an operand and ++ is a unary Java operator
In a prefix notation, the unary operator appears before its operand.
operator operand // Prefix notation
For example, ++num; // ++ is a Java unary operator and num is an operand
A binary operator uses infix notation. The operator appears in between the two operands.
operand1 operator operand2 // Infix notation
For example, 10 + 15; // 10 is operand1, + is a binary operator, and 15 is operand2
Like a binary operator, a ternary operator uses infix notation.
operand1 operator1 operand2 operator2 operand3 // Infix notation
Here, operator1 and operator2 make a ternary operator.
For example, isSunday ? holiday : noHoliday;
/* isSunday is the first operand, ? is the first part of ternary operator, holiday is
the second operand, : is the second part of ternary operator, noHoliday is the
third operand */
Java divides the operators based on the type of operation they perform on the operand into the
following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators (Relational operators)
• Logical operators
• Bitwise operators
DTTH Page 1
Java Fundamentals
System.out.println(i);
i = s; // Ok. short s is assignment compatible to int i
i = (int)big; // Ok
i = (int)f; // Ok
Arithmetic Operators
DTTH Page 3
Java Fundamentals
short s1 =100;
int i = 10;
int j =12;
float f1 =2.5f;
double d1=20.0;
byte b3 = 2;
//b1 = b2 + b3; // A compile-time error. Trying to assign 5 to b1
b1 = (byte)(b2 + b3); // Ok now
//b1 = (byte) b2 + b3; // An error again
//b1 = s1 + 2;
//an error. s1 is promoted to int and s1+2 is of the data type int.
//int to byte assignment is not permitted
//b1 = f1 + b2;
//an error. b2 is promoted to float and f1+b2 is of the data type
float.
//float to byte assignment is not permitted
f1=(float)(i+d1); //ok
f1=2.0+3.2;
//an error. 2.0 and 3.2 are of the type double. The result of
2.0+3.2 is 5.2 which is also of the type double
//dobule to float assignment is not permitted.
f1= 2.0F+3.2F;
//ok. 2.0F and 3.2F are of the type float. The result of 2.0F+3.2F,
which is 5.2F, is of the type float.
d1 = f1+ j;
//ok. j is promoted to float and f1+j is of the type of float.
//float to double assignment is permitted.
}
}
Java Fundamentals
Operation2.java
public class Operation2
{
public static void main(String[] args)
{
byte b1 = 5;
int i =100;
float f1 = 2.5f;
double d1 = 15.45;
b1 = 200-173;
//ok. 200-173 = 27 because 27 is the range -128 and 127
//b1 = i-27;
//error. i-27 is of the type int. int to byte assignment is not
allowed
b1 = (byte)(i-27); //ok
byte b2 = 5;
int i2 = 10;
float f2 = 2.5f;
double d2 = 15.45;
b2 = 20*6;
//b2 = 20 * 7;
//incompatible type: error : possible lossy conversion from int to
byte
//b2 = i2*12;
b2=(byte)(i2*12);
System.out.println(b2);
b2=(byte)(i2*15);//ok
System.out.println(b2);
f2 = i2*b2;
//f2 =d2*i2;
f2=(float)(d2*i2); //ok
int num;
num = 5/2; //assigns 2 to num
System.out.println(num);
num = 5/3; //assigns 1 to num
System.out.println(num);
DTTH Page 5
Java Fundamentals
f1 = (float)(15.0/4.0F);
}
}
Operation3.java
public class Operation3
{
public static void main(String[] args)
{
int i =2;
int j =5;
int k=0;
//i=j/k; //runtime error. Divide by zero
//i = 0/0; //runtime error. Divide by zero
float f1 = 2.5f;
double d1 = 5.6;
//f1=5.0F/0.0;
//compile_time error : incompatible types: possible lossy
conversion from double to float
f1 = (float)(5.0F/0.0);
//Float.POSITIVE_INFINITY is assigned to f1
If both operands of the modulus opearator are integers, the following rules are applied to
computer the result.
Rule-1 It is a runtime error if the right-hand operand is zero.
Rule-2 If the right-hand operand is not zero, the sign of the result is the same as the sign of
the left-hand operand.
Operation4.java
public class Operation4
{
public static void main(String[] args)
{
//Rule 1
int num;
//num = 15%0; //runtime error
//Rule 2
num=15%6; //Assigns 3 to num
System.out.println(num);
num =-15%6; //Assigns -3 to num
System.out.println(num);
num=15%-6; //Assigns 3 to num
System.out.println(num);
num=-15%-6; //Assigns -3 to num because left-hand operand is -15,
which is negative
num = 5%7; //Assigns 5 to num
DTTH Page 7
Java Fundamentals
System.out.println(num);
num=0%7; //Assigns 0 to num
System.out.println(num);
num=15/6; //2
System.out.println(num);
num=-15/6; //-2
System.out.println(num);
num=15/-6; //-2
System.out.println(num);
num=-15/-6; //2
System.out.println(num);
//Rule 3
float f1;
f1 = 15.0F % 0.0F; // Assigns Float.NaN to f1
System.out.println(f1);
double d1;
f1 = Float.NaN % 10.5F; // Assigns Float.NaN to f1
System.out.println(f1);
//Rule 4
f1 = Float.POSITIVE_INFINITY % 2.1F; // Assigns Float.NaN to f1
//Rule 5
f1 = 15.5F % 6.5F; // Assigns 2.5F to f1
System.out.println(f1);
d1 = 5.5 % 15.65; // Assigns 5.5 to d1
System.out.println(d1);
d1 = 0.0 % 3.78; // Assigns 0.0 to d1
System.out.println(d1);
d1 = 85.0 % Double.POSITIVE_INFINITY; // Assigns 85.0 to d1
System.out.println(d1);
d1 = -85.0 % Double.POSITIVE_INFINITY; // Assigns -85.0 to d1
System.out.println(d1);
d1 = 85.0 % Double.NEGATIVE_INFINITY; // Assigns 85.0 to d1
System.out.println(d1);
d1 = -85.0 % Double.NEGATIVE_INFINITY; // Assigns -85.0 to d1
System.out.println(d1);
}
}
Java Fundamentals
DTTH Page 9
Java Fundamentals
int i = 100;
i += 5.5; // Assigns 105 to i is equivalent to
i = (int)(i + 5.5); // Assigns 105 to i
There are two advantages of using the compound arithmetic assignment operators.
One of the reasons compound operators are useful is that not only they provide a
shorter way for operations, but also implicitly cast variables.
long number = 10;
int i = number;
i = i * number; // Does not compile
Java automatically promotes smaller data types to larger data ones, when they are
together in an operation, but will throw an error when trying to convert from
larger to smaller types.
This could be fixed with an explicit cast:
i = (int) i * number;
Java compound assignment operators are perfect in this case because they do
an implicit casting:
i *= number;
This statement works just fine, casting the multiplication result to int and assigning
the value to the left-hand side variable, i.
short x = 4;
x += 6.6;
Ex.1
int i = 10;
i = i++ + i; // Assigns 21 to i
i = 10;
i = ++i + i++; // Assigns 22 to i
Ex.2
int i = 15;
int j = 16;
i--;
--i;
i = 10;
i = i--; // Assigns 10 to i
i = 10; j = i-- + 10; // Assigns 20 to j and 9 to i
i = 10; j = --i + 10; // Assigns 19 to j and 9 to i
DTTH Page 11
Java Fundamentals
There are two important points to remember about the use of increment and decrement operators.
• The operand of the increment and decrement operators must be a variable. For example, the
expression 5++ is incorrect because ++ is being used with a constant.
• The result of the expression using ++ or -- operator is a value, not a variable. For example, i++
evaluates to a value, so or where a variable is required.
If a String variable contains the null reference, the concatenation operator uses a string "null". The
following examples illustrate the use of string representations of the values of primitive data types in
string concatenation:
Ex-3
boolean b1 = true;
boolean b2 = false;
int num = 365;
double d = -0.0;
char c = 'A';
String str1;
String str2 = null;
str1 = b1 + " friends"; // Assigns "true friends" to str1
str1 = b2 + " identity"; // Assigns "false identity" to str1
// Assigns "null and void" to str1. Because str2 is null, it is
replaced
// by a string "null" by the concatenation operator
str1 = str2 + " and void";
1 str1 = Double.NaN + " is absurd"; // Assigns "NaN is absurd" to
str1
str1 = c + " is a letter"; // Assigns "A is a letter" to str1
str1 = "This is " + b1; // Assigns "This is true" to str1
// Assigns "Beyond Infinity" to str1
str1 = "Beyond " + Float.POSITIVE_INFINITY
Ex-4
int num1 = 12;
int num2 = 15;
String str1 = " men";
String str2;
str2 = num1 + num2 + str1; //27 men
str2 = num1 + (num2 + str1); // Assigns "1215 men" to str2
str2 = str1 + num1 +num2;
System.out.println(str2); // men1215
str2 = "" + num1 + num2 + str1; // Assigns "1215 men" to str1
str2 = num1 + "" + num2 + str1; // Assigns "1215 men" to str2
Ex-5
int num = 15;
boolean b = false;
String str1 = “faces";
String str2 = b + num + str1; // A compile-time error
str2 = b + (num + str1); // Ok. Assigns "false15faces" to str2
str2 = "" + b + num + str1; // Ok. Assigns "false15faces" to str2
str2 = b + "" + num + str1; // Ok. Assigns "false15faces" to str2
DTTH Page 13
Java Fundamentals
You use the println() and print() methods to print a message on the standard output, as follows:
System.out.println("Prints a new line at the end of text");
System.out.print("Does not print a new line at the end of text");
Java Fundamentals
DTTH Page 15