You are on page 1of 15

Java Fundamentals

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

Assignment Operator (=)


 An assignment operator (=) is used to assign a value to a variable. It is a binary operator. It
takes two operands.
 The value of the right-hand operand is assigned to the left-hand operand. The left-hand
operand must be a variable.
For example, int num;
num = 25;
byte b = 5;
char c = 'a';
short s = -200;
int i = 10;
i = b; // Ok. byte b is assignment compatible to int
i = c; // Ok. char c is assignment compatible to int

DTTH Page 1
Java Fundamentals

System.out.println(i);
i = s; // Ok. short s is assignment compatible to int i

long big = 524L;


float f = 1.19F;
int i = 15;
i = big; // A compile-time error. long to int, assignment
incompatible

i = f; // A compile-time error. float to int, assignment


incompatible

i = (int)big; // Ok
i = (int)f; // Ok

num1 = num2 = 25;


num1 = num2 = num3 = num4 = num5 = num6 = 219;

// Declaration of variables num1, num2 and num3. Initialization of


only num1 and num3
int num1 = 10, num2, num3 = 200;

// Declaration and initialization of variables num1, num2 and num3


int num1 = 10, num2 = 20, num3 = 30;

int num1 = 100; // num1 is 100


int num2 = 200; // num2 is 200
num1 = num2; // num1 is 200. num2 is 200
num2 = 500; // num2 is 500. num1 is still 200

Object ref1 = new Object(); // An object


Object ref2 = new Object(); // An object
ref1 = ref2;

byte b =27; //the compiler automatically narrows the literal


value to a bye
The preceding code is identical to the following:
byte b=(byte)27; //Explicitly cast the int literal to a byte
byte a = 3; //No problem, 3 fits in a byte
byte b = 8; //No problem, 8 fits in a byte
byte c = a + b; //compiler error
byte c = (byte)(a+b); //ok explicitly cast type
Java Fundamentals

Arithmetic Operators

Addition Operator (+)


The addition operator (+) is used in the form operand1 + operand2

Using arithmetic operators Examples


Operation.java

public class Operation


{
public static void main(String[] args)
{
byte b1=2;
byte b2=3;

DTTH Page 3
Java Fundamentals

short s1 =100;
int i = 10;
int j =12;
float f1 =2.5f;
double d1=20.0;

b1 =15+110; //Ok. 125 is the range -128 and 127


//b1 = i + 5;
//An error, i is promoted to int and i+5 is of the data type int.
//int to byte assignment is not permitted

//b1 =(byte)i+5; // A compile-time error. Trying to assign 5 to b1


b1=(byte)(i+5);

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

//b1 = f1+ d1;


//an error. f1 is promoted to double and f1+d1 is of the data type
double

f1 = i + f1; //ok. i is promoted to float and i+f1 is of the type of


float

//f1= i + d1; an error.i is promoted to doouble and i+d1 is of the


type of double
//double to float 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

Subtraction Operator (-)


The subtraction operator (-) is used in the form operand1 - operand2
Multiplication Operator (*)
The multiplication operator (*) is used in the form operand1 * operand2
Division Operator (/)
The division operator (/) is used in the form operand1 / operand2
There are two types of division:
• Integer division
• Floating-point division

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

num = 5/4; // Assigns 1 to num


num = 5/5; // Assigns 1 to num
num = 5/6; // Assigns 0 to num
num = 5/7; // Assigns 0 to num

f1 = 15.0F/4.0F; //float is divided by float


System.out.println(f1);

f1 = 15/4.0F; //int is divided by float


System.out.println(f1);

//f1 = 15.0/4.0F; //double is divided by float

f1 = (float)(15.0/4.0F);

f1 = 15/4; //3.0 int is divided by int


System.out.println(f1);

}
}

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.0F; //Float.POSITIVE_INFINITY is assigned to f1


System.out.println(f1);
f1 = -5.0F/0.0F; //Float.NEGATIVE_INFINITY is assigned to f1
System.out.println(f1);

f1 = -5.0F/-0.0F; //Float.POSITIVE_INFINITY is assigned to f1


System.out.println(f1);
f1 = 5.0F/-0.0F; //Float.NEGATIVE_INFINITY is assigned to f1
System.out.println(f1);

d1 = 5.0/0.0; //Double.POSITIVE_INFINITY is assigned to d1


System.out.println(d1);
d1 = -5.0/0.0; //Double.NEGATIVE_INFINITY is assigned to d1
System.out.println(d1);
d1 = -5.0/-0.0; //Double.POSITIVE_INFINITY is assigned to d1
System.out.println(d1);
d1 = 5.0/-0.0; //Double.NEGATIVE_INFINITY is assigned to d1
System.out.println(d1);

f1 = 5.0F/0; //Float.POSITIVE_INFINITY is assigned to f1


Java Fundamentals

//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

f1 = 0.0F/0.0F; //Assigns Float.NaN to f1 //Not A Number


System.out.println(f1);
d1 = 0.0/0.0; //Assigns Double.NaN to d1
System.out.println(d1);
d1 = -0.0/0.0; //Assigns Double.NaN to d1
System.out.println(d1);
}
}

Modulus Operator (%)


The modulus operator (%) is used in the form operand1 % operand2

 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.

 If either operand of the modulus operator is a floating-point number,


Rule-1 The operation never results in an error even if the right-hand operand is a floating-
point zero.
Rule-2 The result is NaN if either operand is NaN.
Rule-3 If the right-hand operand is zero, the result is NaN.
Rule-4 If the left-hand operand is infinity, the result is NaN.
Rule-5 If none of the above rules apply, the modulus operator returns the remainder of the
division of the left-hand operand and the right-hand operand. 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);

f1 = 20.0F % Float.NaN; // Assigns Float.NaN to f1


System.out.println(f1);

f1 = Float.NaN % Float.NaN; // Assigns Float.NaN to f1


System.out.println(f1);

// A compile-time error. The expression is of the type double.


// double to float assignment is not allowed
//f1 = Float.NaN % Double.NaN;

d1 = Float.NaN % Double.NaN; // Assigns Double.NaN to d1


System.out.println(d1);

//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

Unary Plus Operator (+)


•The unary plus operator (+) is used in the form +operand
•If the operand is of the byte, short, or char type, the unary plus operator promotes it to int
type.
• Otherwise, there is no effect of using this operator.
byte b1 = 10;
byte b2 = +5;
b1 = b2; // Ok. byte to byte assignment
// A compile-time error. b2 is of the type byte. But, use of the
unary plus operator on
// b2 promoted its type to int. Therefore, +b2 is of the type int.
// int (+b2) to byte (b1) assignment is not allowed.
b1 = +b2;
b1 = (byte) +b2; // Ok

Unary Minus Operator (-)


The unary minus operator (-) is used in the form –operand
byte b1 = 10;
byte b2 = -5; b1 = b2; // Ok. byte to byte assignment
// A compile-time error. b2 is of the type byte. But, use of unary
minus operator (-) on
// b2 promoted its type to int. Therefore, -b2 is of type int.
// int (-b2) to byte (b1) assignment is not allowed.
b1 = -b2;
b1 = (byte) -b2; // Ok

Compound Arithmetic Assignment Operators(+=, -=, *=, /=, %=)


int a =10;
a = a+5; //assigns 15 to a can be written as
a+= 5;

String str1 = "Hello";


str1 = str1 + 100; // Assigns "Hello100" to str1 can be rewritten as
str1 += 100; // Assigns "Hello100" to str1
//Of the compound operators, only the += operator can be used with a
String left-hand operand.
int i = 110;
float f = 120.2F;
byte b = 5;
String str = "Hello";
boolean b1 = true;
i += 10; // Assigns 120 to i
// A compile-time error. boolean type cannot be used with += unless
left-hand operand (here i) is a String variable
i += b1;
i -= 15; // Assigns 95 to i. Assuming i was 110
i *= 2; // Assigns 220 to i. Assuming i was 110
i /= 2; // Assigns 55 to i. Assuming i was 110
i /= 0; // A runtime error . Division by zero error
f /= 0.0; // Assigns Float.POSITIVE_INFINITY to f
i %= 3; // Assigns 2 to i. Assuming i is 110
str += " How are you?"; // Assigns "Hello How are you?" to str
str += f; // Assigns "Hello120.2" to str. Assuming str was "Hello"
b += f; // Assigns 125 to b. Assuming b was 5, f was 120.2

DTTH Page 9
Java Fundamentals

str += b1; // Assigns "Hellotrue" to str. Assuming str was "Hello"

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;

and results in x having the value 7 because it is equivalent to:


short x = 4;
x = (short)(x + 6.6); //10

Because here 6.6 which is double is automatically converted to short type


without explicit type-casting.
float f = 1270.00f;
f += 127.00;
System.out.println(f);

Increment (++) and Decrement (--) Operators


The increment operator (++) is used with a variable of numeric data type to increment its value by 1,
whereas the decrement operator (--) is used to decrement the value by 1.
int i = 100;
To increment the value of i by 1, you can use one of the four following expressions:
i = i + 1; // Assigns 101 to i
OR
i += 1; // Assigns 101 to i
OR
i++; // Assigns 101 to i
OR
++i;
Java Fundamentals

The increment operator ++ can also be used in a more complex expression as


int i = 100;
int j = 50;
j = i++ + 15; // Assigns 115 to j and i becomes 101
The expression i++ + 15 is evaluated as follows:
• The value of i is evaluated and the right-hand expression becomes 100 + 15.
• The value of i in memory is incremented by 1.
So, at this stage the value of the variable i in memory is 101.
• The expression 100 + 15 is evaluated and the result 115 is assigned to j.

There are two kinds of increment operators:


• Post-fix increment operator, for example, i++
• Pre-fix increment operator, for example, ++i
The result would be different if the above expression is rewritten using a pre-fix increment operator.
int i = 100;
int j = 50;
j = ++i + 15; // i becomes 101 and assigns 116 to j
In this case, the expression ++i + 15 is evaluated as follows:
• Because ++i uses a pre-fix increment operator, first the value of i is incremented in memory by 1.
Therefore, the value of i is 101.
• The current value of i, which is 101, is used in the expression and the expression becomes 101 + 15.
• The expression 101 + 15 is evaluated and the result 116 is assigned to j.
Pre-Fix Example
int i = 15;
i = ++i; // What will be the value of i after this assignment
Post-Fix Example
int i = 15;
i = i++; // What will be the value of i after this assignment

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.

String Concatenation Operator (+)


 The + operator is overloaded.
 use as an arithmetic addition operator to add two numbers
 used to concatenate two strings
 used to concatenate two strings
Two strings, such as "abc" and "xyz", can be concatenated using the + operator as "abc" + "xyz" to
produce new string "abcxyz".
The following is another example of a string concatenation:
String str1 = "Hello,";
String str2 = " FE Java Class";
String str3 = str1 + str2; // Assigns "Hello, FE Java Class" to str3
The string concatenation operator is also used to concatenate a primitive and a reference data type
value to a string.
 When either operand of the + operator is a string, it performs string concatenation.
 When both operands of + are numeric, it performs number addition.
Consider the following snippet of code:
int num = 26;
String str1 = "Alphabets";
String str2 = num + str1; // Assigns "26Alphabets" to str2
Before num and str1 are concatenated, num is replaced by its string representation, which is "26".
Now, the expression becomes "26" + str1, which results in "26Alphabets".
Java Fundamentals

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

You might also like