3.JavaIdentifiers, Data Types, Operators
3.JavaIdentifiers, Data Types, Operators
• Data are used to specify what kind of value can be stored in a variable.
• The memory size and type of the value of a variable are determined by
the variable data type..
• Data types are classified into two types:
• Primitive Data Types
• Non-primitive Data Types
c
• The Range of data, that can be stored in a variable is based on the
memory size of the data type.
• For example: byte size is 1 byte(8 bits) and range is -128 to 127.
• byte c;
Example5: Java Data types – Default values
public class PrimitiveDataTypes
{
byte i;
short j;
int k;
long l;
float m;
double n;
char ch;
boolean p;df
public static void main(String[] args)
{
PrimitiveDataTypes obj = new PrimitiveDataTypes();
System.out.println("i = " + obj.i + ", j = " + obj.j + ", k = " + obj.k + ", l = " + obj.l);
System.out.println("m = " + obj.m + ", n = " + obj.n);
System.out.println("ch = " + obj.ch);
System.out.println("p = " + obj.p);
}
}
Type Casting
• The process of converting the value of one data type (int, float, double,
etc.) to another data type is known as typecasting.
• The automatic conversion is done by the compiler and manual
conversion performed by the programmer.
o There are two types of primitive type casting:
– Widening Type Casting(automatic)
– Narrowing Type Casting(manual)
Widening Type Casting(automatic)
• The lower data type (having smaller size) is converted into the higher
data type (having larger size). Hence there is no loss in data. This is
why this type of conversion happens automatically.
• Note: This is also known as Implicit Type Casting.
Narrowing Type Casting(manual)
• Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
• Narrowing casting must be done manually by placing the type in parentheses in front of the
value
• If we don’t type cast manually the compiler will show an error
public class Main
{
public static void main(String[] args)
{
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println(myDouble); // Outputs 9.78
System.out.println(myInt); // Outputs 9
}
}
Narrowing Type Casting(manual)
• The higher data types (having larger size) are converted into lower
data types (having smaller size). Hence there is the loss of data. This is
why this type of conversion does not happen automatically.
• Note: This is also known as Explicit Type Casting.
Java Operators
Operators
• The modulus (remainder of the division) operator is used with integer data type only.
result = a + b;
System.out.println("Addition : " + a + " + " + b + " = " + result);
result = a - b;
System.out.println("Subtraction : " + a + " - " + b + " = " + result);
result = a * b;
System.out.println("Multiplucation : " + a + " * " + b + " = " + result);
result = b / a;
System.out.println("Division : " + b + " / " + a + " = " + result);
result = b % a;
System.out.println("Modulus : " + b + " % " + a + " = " + result);
result = ++a;
System.out.println("Pre-increment : ++a = " + result);
result = b--;
System.out.println("Post-decrement : b-- = " + result);
}
}
Relational Operators
• The relational operators are the symbols that are used to compare two
values. That means the relational operators are used to check the
relationship between two values.
• Relational operator has two posible results either TRUE or FALSE.
• The relational operators are used to define conditions in a program.
Relational Operators
Example7- Relational Operators
public class RelationalOperators {
boolean a;
a = 10<5;
System.out.println("10 < 5 is " + a);
a = 10>5;
System.out.println("10 > 5 is " + a);
a = 10<=5;
System.out.println("10 <= 5 is " + a);
a = 10>=5;
System.out.println("10 >= 5 is " + a);
a = 10==5;
System.out.println("10 == 5 is " + a);
a = 10!=5;
System.out.println("10 != 5 is " + a);
}
}
Logical Operators
• The logical operators are the symbols that are used to combine
multiple conditions into one condition.
Logical Operators
• The operators &, |, and ^ can be used with both boolean and integer
data type values. When they are used with integers, performs bitwise
operations and with boolean, performs logical operations.
}
Assignment Operators
}
Program to find a no ODD or EVEN
• The signed right shift operator '>>' uses the sign bit to fill the trailing positions.
• The unsigned right shift operator ‘>>>' do not use the sign bit to fill the trailing positions.
It always fills the trailing positions by 0s.
• Assume if a = 60 and b = -60; now in binary format, they will be as follows −
a = 0000 0000 0000 0000 0000 0000 0011 1100 (60)
b = 1111 1111 1111 1111 1111 1111 1100 0100(-60)
In Java, negative numbers are stored as 2's complement.
a >> 1 = 0000 0000 0000 0000 0000 0000 0001 1110 (30)
b >> 1 = 1111 1111 1111 1111 1111 1111 1110 0010 (-30)
a >>> 1 = 0000 0000 0000 0000 0000 0000 0001 1110 (30)
b >>> 1 = 0111 1111 1111 1111 1111 1111 1110 0010 (2147483618)
Bitwise Operators - Signed right shift operator(>>) vs Un - Signed
right shift operator(>>>)
}
Conditional Operators / Ternary Operator
c = (a>b)? a : b;
}
Java expressions