OOP1 Unit1
OOP1 Unit1
OOP1 - 3140705
• Java is an object-oriented programming
language.
• What is Object?
What is Object?
• Everything in Java is associated with classes and
objects, along with its attributes and methods.
5. Java Keywords
• Java keywords are also known as reserved words.
• These are predefined words by Java so it cannot be used as a
variable or object name.
• e.g. boolean, byte, abstract, break, char, class, continue, default, do,
while
Data Types
• Data types specify the different sizes and values that can be
stored in the variable. There are two types of data types in
Java:
• Unary Operator
• Arithmetic Operator
• Relational Operator
• Assignment Operator
• Bitwise Operator
• Logical Operator
• Ternary Operator
• Shift Operator
Unary Operator
• The Java unary operators require only one operand.
Unary operators are used to perform various
operations i.e.:
class OperatorExample
{
public static void main(String args[])
{
int x=10;
System.out.println(x++); //10 (11)
System.out.println(++x); //12
System.out.println(x--); //12 (11)
System.out.println(--x); //10
}
}
Java Unary Operator Example – inverting boolean value
class OperatorExample
{
public static void main(String args[])
{
boolean c=true;
boolean d=false;
System.out.println(!c);//false (opposite of boolean value)
System.out.println(!d);//true
}
}
Arithmetic Operators
Java arithmetic operators are used to perform addition, subtraction, multiplication,
and division. They act as basic mathematical operations.
class Demo
{
public static void main(String[] args)
{
int a = 12, b = 5;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
}
}
Output
a + b = 17
a-b=7
a * b = 60
a/b=2
a%b=2
Relational Operator
Relational operators are used to check the relationship between two operands.
Note: Relational operators are used in decision making and loops.
class Main
{
public static void main(String[] args)
{
int a = 7, b = 11;
The value on the right side must be of the same data-type of the
operand on the left side otherwise the compiler will raise an error.
a = 0011 1100
b = 0000 1101
a = 0011 1100
b = 0000 1101
& (bitwise and) - Binary AND Operator copies a bit to the result if it exists in both
operands.
a&b = 0000 1100
^ (bitwise XOR) - Binary XOR Operator copies the bit if it is set in one operand but
not both.
a^b = 0011 0001
~ (bitwise compliment) - Binary Ones Complement Operator is unary and has the
effect of 'flipping' bits.
~a = 1100 0011
Logical Operator
&& (Logical AND)
|| (Logical OR)
! (Logical NOT)
class Main
{
public static void main(String[] args)
{
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
Ternary Operator
A ternary operator evaluates the test condition
and executes a block of code based on the result
of the condition.
// variable declaration
int n1 = 5, n2 = 10, max;
<< (left shift) - The left operands value is moved left by the number of bits specified by the
right operand.
>> (right shift) - The left operands value is moved right by the number of bits specified by
the right operand.
A >> 2 will give 15 which is 1111
>>> (zero fill right shift) - Shift right zero fill operator. The left operands value is moved right
by the number of bits specified by the right operand and shifted values are filled up
with zeros.
A >>>2 will give 15 which is 0000 1111
>>> always shifts a zero into the leftmost position whereas >> shifts based on sign of the
number i.e. 1 for negative number and 0 for positive number.
Java Operator Precedence
Operator precedence determines the order in which the operators in an
expression are 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.
Scope of Variables in Java
If the data types are compatible, then Java will perform the conversion
automatically known as Automatic Type Conversion and if not then they need
to be casted or converted explicitly.
Widening conversion takes place when two data types are automatically
converted. This happens when:
– The two data types are compatible.
– When we assign value of a smaller data type to a bigger data type.
For Example, in java the numeric data types are compatible with each other
but no automatic conversion is supported from numeric type to char or
boolean.
Also, char and boolean are not compatible with each other.
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
}
Narrowing Casting
Narrowing casting must be done manually by placing the type in parentheses
in front of the value:
for example, what if you want to assign an int value to a byte variable?
This conversion will not be performed automatically, because a byte is smaller
than an int.