JAVA
r
Topics for Today’s Session
How to download and install
Java in PC
First Java Program
Operatos & Types of
Operators
Java
 Java is a computing platform for application
development and an object-oriented,
 Java is Class-based and Concurrent programming
language
 It means the code can be executed by multiple
processes at the same time.
 Java can run on all platforms and free to access.
 Java is Simple, Secure, Robust, Complete Object
oriented and Platform Independent High level
Language
 It is Portable and Multi-thread technology gives
High Performance.
How to Download & Install Java JDK 8 in
Windows
 Java Development Kit(JDK) allows us to code and run Java
programs.
 It's possible that you install multiple JDK versions on the same PC.
 But, you install only latest version.
Step 1: Go to the link
Click on Download JDK. and select Java latest version.
Step 2: Next,
1. Accept License Agreement
2. Download latest Java JDK for your version(32 or 64 bit) of java
for Windows.
Step 3: Once the download is complete, run the exe for install JDK.
Click Next
Step 4: Select the PATH for Java installation and click next.
Step 5: Once installation is complete click Close
How to set Environment Variables in Java:
Path and Classpath
 The PATH variable gives the location of executables like javac, java etc.
 It is not necessary to specifying PATH
 but you will need to give full path of executable like
C:Program FilesJavajdk-13.0.1binjavac Hello.java
instead of simple
javac Hello.java
 The CLASSPATH variable gives location of the Library Files.
Steps to set the PATH and CLASSPATH
Step 1) Right Click on the My Computer and Select the properties.
Step 2) Click on advanced system settings
Step 3) Click on Environment Variables
Step 4) Click on new Button of User variables
Step 5) Type PATH in the Variable name.
(contd..)
Step 6) Copy the path of bin folder which is installed in JDK folder.
Step 7) Paste Path of bin folder in Variable value and click on OK Button.
Note: If already have a PATH variable created in your PC, edit the PATH variable to
PATH = <JDK installation directory>bin;%PATH%;
Here, %PATH% appends the existing path variable to our new value
Step 8) Follow a similar process to set CLASSPATH.
Note: In case, java installation does not work after installation, change classpath to
CLASSPATH = <JDK installation directory>libtools.jar;
Step 9) Click on OK button.
Step 10) Go to command prompt and type javac commands.
How to Create Your First Java Program
 You need two softwares to create your first Java Program
1. The Java SE Development Kit
2. A Text Editor
 Here I am using Notepad.
Notepad is a simple editor included with the Windows
Operating System.
You can use a different text editor like NotePad++, Waordpad
etc.,
Steps to Compile and Run first Java program
Step 1) Open Notepad from Start menu by selecting
Programs > Accessories > Notepad.
Step 2) Create a Source Code for your Program
// Simple Java Program
class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
} }
Step 3) Save the file as Hello.java
Step 4) Open the command prompt. Go to Directory wherer Java is installed.
C:Program Files]Java]jdk13.1
Compile the code using command,
javac Hello.java
Step 5) If you look in your working folder, the file Hello.class has been created.
Step 6) To execute the code, enter the command java followed by the class name,
then
output Hello World is displayed
Explanation about the above Java Program











Operators
 An operator is a symbol that represents a mathematical or
non- mathematical operation.
 Operators are special symbols (characters) that carry out
operations on operands (variables and values).
 Java divides the operators into the following :
1. Arithmetic operators
2. Assignment operators
3. Relational / Comparison operators
4. Logical operators
5. Unary Operators ( Increment , Decrement Operators)
6. Bitwise operators
7. Conditional Operators
8. Special Operators
The Arithmetic Operators
 Arithmetic operators are used in mathematical expressions in the same
way that they are used in algebra.
 Arithmetic operators are used to perform addition, subtraction,
multiplication, and division.
 They act as basic mathematical operations.
 Assume integer variable A holds 5 and variable B holds 10,
Operator Description Example
+ (Addition)
Adds values on either side of the
operator.
A + B will give 15
- (Subtraction)
Subtracts right-hand operand
from left-hand operand.
A - B will give -5
 (Multiplication)
Multiplies values on either side of
the operator.
A * B will give 50
/ (Division)
Divides left-hand operand by
right-hand operand.
B / A will give 2
% (Modulus)
Divides left-hand operand by
right-hand operand and returns
remainder.
B % A will give 0
++ (Increment)
Increases the value of operand by
1 .
B++ gives 11
-- (Decrement)
Decreases the value of operand by
1.
B-- gives 9
//Java Arithmetic Operator Example
Class ArithmeticEx
{
public static void main(String args[])
{
int a=10;
int b=20;
System.out.println(a+b); //30
System.out.println(a-b); //-10
System.out.println(a*b); // 200
System.out.println(a/b); // .2
System.out.println(a%b); // 0
}
}
Relational Operators (Comparison Operators)
 There are following relational operators supported by Java language.
 Comparison operators are used to compare two values:
 Assume variable X holds 10 and variable Y holds 20, then −
Operator Description Example
== (equal to)
Checks if the values of two operands are equal or not, if yes then
condition becomes true.
(X == Y) is
not true.
!= (not equal to)
Checks if the values of two operands are equal or not, if values
are not equal then condition becomes true.
(X != Y) is
true.
> (greater than)
Checks if the value of left operand is greater than the value of
right operand, if yes then condition becomes true.
(X > Y) is not
true.
< (less than)
Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.
(X < Y) is
true.
>= (greater than
or equal to)
Checks if the value of left operand is greater than or equal to the
value of right operand, if yes then condition becomes true.
(X >= Y) is
not true.
<= (less than or
equal to)
Checks if the value of left operand is less than or equal to the
value of right operand, if yes then condition becomes true.
(X <=Y) is
true.
Example : Equality and Relational Operators
class RelationalOp
{
public static void main(String[] args)
{
int number1 = 5, number2 = 6;
if (number1 > number2)
{
System.out.println("number1 is greater than number2.");
} else
{ System.out.println("number2 is greater than number1.");
}
}
}
Bitwise Operators
 Bitwise operators are used to perform binary logic
with the bits.
 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.
Operator Description
Exampl
e
Same as Result Decimal
&
AND- Sets each bit to 1 if both bits
are 1
5 & 1
0101 &
0001
0001 1
|
OR - Sets each bit to 1 if any of the
two bits is 1
5 | 1
0101 | 0001 0101 5
~ NOT - Inverts all the bits ~ 5 ~0101 1010 10
^
XOR - Sets each bit to 1 if only one of
the two bits is 1
5^1
0101 ^
0001
0100 4
<<
Zero-fill left shift - Shift left by
pushing zeroes in from the right and
letting the leftmost bits fall off
9<<1
1001<<1 0010 2
>>
Signed right shift - Shift right by
pushing copies of the leftmost bit in
from the left and letting the
rightmost bits fall off
9>>1
1001>>1 1100 12
>>>
Zero-fill right shift - Shift right by
pushing zeroes in from the left and
letting the rightmost bits fall off
9 >>> 1
1001 >>> 1 0100 4
Logical Operators
 Logical operators operate on boolean expressions.
 Logical operators are used to determine the logic between variables
or values:
Operator Name Description Example
&& Logical and Returns true if both
statements are true
x < 5 && x <
10
|| Logical or Returns true if one of the
statements is true
x < 5 || x < 4
! Logical not Reverse the result, returns
false if the result is true
!(x < 5 && x <
10)
// Using Logical Operators
class LogicalOp {
public static void main(String[] args)
{
int num1 = 1, num2 = 2, num3 = 9;
boolean result;
// At least one expression needs to be true for the result to be true
result = (num1 > num2) || (num3 > num1);
// result will be true because (num1 > num2) is true
System.out.println(result);
// All expression must be true from result to be true
result = (num1 > num2) && (num3 > num1);
// result will be false because (num3 > num1) is false
System.out.println(result);
}
}
Ternary Operator ( Conditional Operator)
 The conditional operator or ternary operator ?: is shorthand
for the if-then-else statement.
 It is the only conditional operator which takes three operands.
 Syntax :
variable = Expression ? expression1 : expression2
Here
 If the Expression is true, expression1 is assigned to the variable.
 If the Expression is false, expression2 is assigned to the variable.
// Using Conditional operator
class CondOp {
public static void main(String [ ] args)
{
int febDays = 29;
String result;
result = (febyDays == 28) ? "Not a leap year" : "Leap year";
System.out.println(result); }
}
Assignment Operators
 Assignment operators are used to assign values to variables.
 ‘=’ Assignment operator is used to assign a value to any variable.
 It has a right to left associativity, i.e value given on right hand side of
operator is assigned to the variable on the left and therefore right
hand side value must be declared before using it or should be a
constant.
General format of assignment operator is,
variable = value;
 In many cases assignment operator can be combined with other
operators to build a shorter version of statement called Compound
Statement.
Operator Description Example
=
Simple assignment operator. Assigns values from right side
operands to left side operand. C = A + B will assign value of
A + B into C
+=
Add AND assignment operator. It adds right operand to the left
operand and assign the result to left operand.
C += A is equivalent to C = C +
A
-=
Subtract AND assignment operator. It subtracts right operand
from the left operand and assign the result to left operand.
C -= A is equivalent to C = C –
A
*=
Multiply AND assignment operator. It multiplies right operand
with the left operand and assign the result to left operand.
C *= A is equivalent to C = C *
A
/= Divide AND assignment operator. It divides left operand with
the right operand and assign the result to left operand.
C /= A is equivalent to C = C /
A
%=
Modulus AND assignment operator. It takes modulus using
two operands and assign the result to left operand.
C %= A is equivalent to C = C
% A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^=
bitwise exclusive OR and assignment operator.
C ^= 2 is same as C = C ^ 2
|=
bitwise inclusive OR and assignment operator.
C |= 2 is same as C = C | 2
// Using Assignment Operators
class AssignOp
{
public static void main(String args[])
{
int a=10;
int b=20;
a+=4; //a=a+4 (a=10+4)
b-=4; //b=b-4 (b=20-4)
System.out.println(a);
System.out.println(b);
}
}
Output:
14
16
Unary Operator
 Unary operators need only one operand.
 They are used to increment, decrement or negate a value.
1. – :Unary minus, used for negating the values.
2. + :Unary plus, used for giving positive values. Only used when
deliberately converting a negative value to positive.
3. ++ :Increment operator, used for incrementing the value by 1.
There are two varieties of increment operator.
 Post-Increment : Value is first used for computing the result and then
incremented.
 Pre-Increment : Value is incremented first and then result is computed.
4. — : Decrement operator, used for decrementing the value by
There are two varieties of decrement operator.
 Post-decrement : Value is first used for computing the result and then
decremented.
 Pre-Decrement : Value is decremented first and then result is
computed.
5. ! : Logical not operator, used for inverting a boolean value.
// Java program using unary operators
public class UnaryOp {
public static void main(String[] args) {
int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30;
boolean condi = true;
// pre-increment operator a = a+1 and then c = a;
c = ++a;
System.out.println("Value of c (++a) = " + c);
// post increment operator c=b then b=b+1
c = b++;
System.out.println("Value of c (b++) = " + c);
// pre-decrement operator d=d-1 then c=d
c = --d;
System.out.println("Value of c (--d) = " + c);
// post-decrement operator c=e then e=e-1
c = e--;
System.out.println("Value of c (e--) = " + c);
// Logical not operator
System.out.println("Value of !condition =“ + !condi);
} }
instanceof Operator
 In addition to relational operators, there is also a type
comparison operator instanceof which compares an object
to a specified type.
 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).
Syntax:
( 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.
// Using instanceof Operator
class instanceofOp {
public static void main(String[] args)
{
String test = “Harsha";
boolean result;
result = test instanceof String;
System.out.println("Is test an object of String? " + result);
}
}
Output:
Is test an object of String? true
Here,
since the variable test is of String type.
Hence, the instanceof operator returns true.
Operator Precedence Table
 Operator precedence determines the grouping of terms in an expression.
 This affects how an expression is evaluated.
 Certain operators have higher precedence than others;
Precedence Operator Type Associativity
15
()
[]
·
Parentheses
Array subscript
Member selection
Left to Right
14
++
--
Unary post-increment
Unary post-decrement
Right to left
13
++
--
+
-
!
~
( type )
Unary pre-increment
Unary pre-decrement
Unary plus
Unary minus
Unary logical negation
Unary bitwise complement
Unary type cast
Right to left
12
*
/
%
Multiplication
Division
Modulus
Left to right
11
+
-
Addition
Subtraction
Left to right
10
<<
>>
>>>
Bitwise left shift
Bitwise right shift with sign extension
Bitwise right shift with zero extension
Left to right
Precedence Operator Type Associativity
9
<
<=
>
>=
instanceof
Relational less than
Relational less than or equal
Relational greater than
Relational greater than or equal
Type comparison (objects only)
Left to right
8
==
!=
Relational is equal to
Relational is not equal to
Left to right
7 & Bitwise AND Left to right
6 ^ Bitwise exclusive OR Left to right
5 | Bitwise inclusive OR Left to right
4 && Logical AND Left to right
3 || Logical OR Left to right
2 ? : Ternary conditional Right to left
1
=
+=
-=
*=
/=
%=
Assignment
Addition assignment
Subtraction assignment
Multiplication assignment
Division assignment
Modulus assignment
Right to left
Summary
 In this lesson you learnt about
 About Java
 How to download and install Java in PC.
 Java First Program
 Operators and its Types
 Operator Precedence
Operators in java

Operators in java

  • 1.
  • 2.
    Topics for Today’sSession How to download and install Java in PC First Java Program Operatos & Types of Operators
  • 3.
    Java  Java isa computing platform for application development and an object-oriented,  Java is Class-based and Concurrent programming language  It means the code can be executed by multiple processes at the same time.  Java can run on all platforms and free to access.  Java is Simple, Secure, Robust, Complete Object oriented and Platform Independent High level Language  It is Portable and Multi-thread technology gives High Performance.
  • 4.
    How to Download& Install Java JDK 8 in Windows  Java Development Kit(JDK) allows us to code and run Java programs.  It's possible that you install multiple JDK versions on the same PC.  But, you install only latest version. Step 1: Go to the link Click on Download JDK. and select Java latest version. Step 2: Next, 1. Accept License Agreement 2. Download latest Java JDK for your version(32 or 64 bit) of java for Windows. Step 3: Once the download is complete, run the exe for install JDK. Click Next Step 4: Select the PATH for Java installation and click next. Step 5: Once installation is complete click Close
  • 5.
    How to setEnvironment Variables in Java: Path and Classpath  The PATH variable gives the location of executables like javac, java etc.  It is not necessary to specifying PATH  but you will need to give full path of executable like C:Program FilesJavajdk-13.0.1binjavac Hello.java instead of simple javac Hello.java  The CLASSPATH variable gives location of the Library Files. Steps to set the PATH and CLASSPATH Step 1) Right Click on the My Computer and Select the properties. Step 2) Click on advanced system settings Step 3) Click on Environment Variables Step 4) Click on new Button of User variables Step 5) Type PATH in the Variable name.
  • 6.
    (contd..) Step 6) Copythe path of bin folder which is installed in JDK folder. Step 7) Paste Path of bin folder in Variable value and click on OK Button. Note: If already have a PATH variable created in your PC, edit the PATH variable to PATH = <JDK installation directory>bin;%PATH%; Here, %PATH% appends the existing path variable to our new value Step 8) Follow a similar process to set CLASSPATH. Note: In case, java installation does not work after installation, change classpath to CLASSPATH = <JDK installation directory>libtools.jar; Step 9) Click on OK button. Step 10) Go to command prompt and type javac commands.
  • 7.
    How to CreateYour First Java Program  You need two softwares to create your first Java Program 1. The Java SE Development Kit 2. A Text Editor  Here I am using Notepad. Notepad is a simple editor included with the Windows Operating System. You can use a different text editor like NotePad++, Waordpad etc.,
  • 8.
    Steps to Compileand Run first Java program Step 1) Open Notepad from Start menu by selecting Programs > Accessories > Notepad. Step 2) Create a Source Code for your Program // Simple Java Program class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); } } Step 3) Save the file as Hello.java Step 4) Open the command prompt. Go to Directory wherer Java is installed. C:Program Files]Java]jdk13.1 Compile the code using command, javac Hello.java Step 5) If you look in your working folder, the file Hello.class has been created. Step 6) To execute the code, enter the command java followed by the class name, then output Hello World is displayed
  • 9.
    Explanation about theabove Java Program           
  • 10.
    Operators  An operatoris a symbol that represents a mathematical or non- mathematical operation.  Operators are special symbols (characters) that carry out operations on operands (variables and values).  Java divides the operators into the following : 1. Arithmetic operators 2. Assignment operators 3. Relational / Comparison operators 4. Logical operators 5. Unary Operators ( Increment , Decrement Operators) 6. Bitwise operators 7. Conditional Operators 8. Special Operators
  • 11.
    The Arithmetic Operators Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.  Arithmetic operators are used to perform addition, subtraction, multiplication, and division.  They act as basic mathematical operations.  Assume integer variable A holds 5 and variable B holds 10, Operator Description Example + (Addition) Adds values on either side of the operator. A + B will give 15 - (Subtraction) Subtracts right-hand operand from left-hand operand. A - B will give -5  (Multiplication) Multiplies values on either side of the operator. A * B will give 50 / (Division) Divides left-hand operand by right-hand operand. B / A will give 2 % (Modulus) Divides left-hand operand by right-hand operand and returns remainder. B % A will give 0 ++ (Increment) Increases the value of operand by 1 . B++ gives 11 -- (Decrement) Decreases the value of operand by 1. B-- gives 9
  • 12.
    //Java Arithmetic OperatorExample Class ArithmeticEx { public static void main(String args[]) { int a=10; int b=20; System.out.println(a+b); //30 System.out.println(a-b); //-10 System.out.println(a*b); // 200 System.out.println(a/b); // .2 System.out.println(a%b); // 0 } }
  • 13.
    Relational Operators (ComparisonOperators)  There are following relational operators supported by Java language.  Comparison operators are used to compare two values:  Assume variable X holds 10 and variable Y holds 20, then − Operator Description Example == (equal to) Checks if the values of two operands are equal or not, if yes then condition becomes true. (X == Y) is not true. != (not equal to) Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (X != Y) is true. > (greater than) Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (X > Y) is not true. < (less than) Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (X < Y) is true. >= (greater than or equal to) Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (X >= Y) is not true. <= (less than or equal to) Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (X <=Y) is true.
  • 14.
    Example : Equalityand Relational Operators class RelationalOp { public static void main(String[] args) { int number1 = 5, number2 = 6; if (number1 > number2) { System.out.println("number1 is greater than number2."); } else { System.out.println("number2 is greater than number1."); } } }
  • 15.
    Bitwise Operators  Bitwiseoperators are used to perform binary logic with the bits.  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.
  • 16.
    Operator Description Exampl e Same asResult Decimal & AND- Sets each bit to 1 if both bits are 1 5 & 1 0101 & 0001 0001 1 | OR - Sets each bit to 1 if any of the two bits is 1 5 | 1 0101 | 0001 0101 5 ~ NOT - Inverts all the bits ~ 5 ~0101 1010 10 ^ XOR - Sets each bit to 1 if only one of the two bits is 1 5^1 0101 ^ 0001 0100 4 << Zero-fill left shift - Shift left by pushing zeroes in from the right and letting the leftmost bits fall off 9<<1 1001<<1 0010 2 >> Signed right shift - Shift right by pushing copies of the leftmost bit in from the left and letting the rightmost bits fall off 9>>1 1001>>1 1100 12 >>> Zero-fill right shift - Shift right by pushing zeroes in from the left and letting the rightmost bits fall off 9 >>> 1 1001 >>> 1 0100 4
  • 17.
    Logical Operators  Logicaloperators operate on boolean expressions.  Logical operators are used to determine the logic between variables or values: Operator Name Description Example && Logical and Returns true if both statements are true x < 5 && x < 10 || Logical or Returns true if one of the statements is true x < 5 || x < 4 ! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)
  • 18.
    // Using LogicalOperators class LogicalOp { public static void main(String[] args) { int num1 = 1, num2 = 2, num3 = 9; boolean result; // At least one expression needs to be true for the result to be true result = (num1 > num2) || (num3 > num1); // result will be true because (num1 > num2) is true System.out.println(result); // All expression must be true from result to be true result = (num1 > num2) && (num3 > num1); // result will be false because (num3 > num1) is false System.out.println(result); } }
  • 19.
    Ternary Operator (Conditional Operator)  The conditional operator or ternary operator ?: is shorthand for the if-then-else statement.  It is the only conditional operator which takes three operands.  Syntax : variable = Expression ? expression1 : expression2 Here  If the Expression is true, expression1 is assigned to the variable.  If the Expression is false, expression2 is assigned to the variable. // Using Conditional operator class CondOp { public static void main(String [ ] args) { int febDays = 29; String result; result = (febyDays == 28) ? "Not a leap year" : "Leap year"; System.out.println(result); } }
  • 20.
    Assignment Operators  Assignmentoperators are used to assign values to variables.  ‘=’ Assignment operator is used to assign a value to any variable.  It has a right to left associativity, i.e value given on right hand side of operator is assigned to the variable on the left and therefore right hand side value must be declared before using it or should be a constant. General format of assignment operator is, variable = value;  In many cases assignment operator can be combined with other operators to build a shorter version of statement called Compound Statement.
  • 21.
    Operator Description Example = Simpleassignment operator. Assigns values from right side operands to left side operand. C = A + B will assign value of A + B into C += Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A -= Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand. C -= A is equivalent to C = C – A *= Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand. C *= A is equivalent to C = C * A /= Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand. C /= A is equivalent to C = C / A %= Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand. C %= A is equivalent to C = C % A <<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2 >>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2 &= Bitwise AND assignment operator. C &= 2 is same as C = C & 2 ^= bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2 |= bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
  • 22.
    // Using AssignmentOperators class AssignOp { public static void main(String args[]) { int a=10; int b=20; a+=4; //a=a+4 (a=10+4) b-=4; //b=b-4 (b=20-4) System.out.println(a); System.out.println(b); } } Output: 14 16
  • 23.
    Unary Operator  Unaryoperators need only one operand.  They are used to increment, decrement or negate a value. 1. – :Unary minus, used for negating the values. 2. + :Unary plus, used for giving positive values. Only used when deliberately converting a negative value to positive. 3. ++ :Increment operator, used for incrementing the value by 1. There are two varieties of increment operator.  Post-Increment : Value is first used for computing the result and then incremented.  Pre-Increment : Value is incremented first and then result is computed. 4. — : Decrement operator, used for decrementing the value by There are two varieties of decrement operator.  Post-decrement : Value is first used for computing the result and then decremented.  Pre-Decrement : Value is decremented first and then result is computed. 5. ! : Logical not operator, used for inverting a boolean value.
  • 24.
    // Java programusing unary operators public class UnaryOp { public static void main(String[] args) { int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30; boolean condi = true; // pre-increment operator a = a+1 and then c = a; c = ++a; System.out.println("Value of c (++a) = " + c); // post increment operator c=b then b=b+1 c = b++; System.out.println("Value of c (b++) = " + c); // pre-decrement operator d=d-1 then c=d c = --d; System.out.println("Value of c (--d) = " + c); // post-decrement operator c=e then e=e-1 c = e--; System.out.println("Value of c (e--) = " + c); // Logical not operator System.out.println("Value of !condition =“ + !condi); } }
  • 25.
    instanceof Operator  Inaddition to relational operators, there is also a type comparison operator instanceof which compares an object to a specified type.  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). Syntax: ( 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.
  • 26.
    // Using instanceofOperator class instanceofOp { public static void main(String[] args) { String test = “Harsha"; boolean result; result = test instanceof String; System.out.println("Is test an object of String? " + result); } } Output: Is test an object of String? true Here, since the variable test is of String type. Hence, the instanceof operator returns true.
  • 27.
    Operator Precedence Table Operator precedence determines the grouping of terms in an expression.  This affects how an expression is evaluated.  Certain operators have higher precedence than others; Precedence Operator Type Associativity 15 () [] · Parentheses Array subscript Member selection Left to Right 14 ++ -- Unary post-increment Unary post-decrement Right to left 13 ++ -- + - ! ~ ( type ) Unary pre-increment Unary pre-decrement Unary plus Unary minus Unary logical negation Unary bitwise complement Unary type cast Right to left 12 * / % Multiplication Division Modulus Left to right 11 + - Addition Subtraction Left to right 10 << >> >>> Bitwise left shift Bitwise right shift with sign extension Bitwise right shift with zero extension Left to right
  • 28.
    Precedence Operator TypeAssociativity 9 < <= > >= instanceof Relational less than Relational less than or equal Relational greater than Relational greater than or equal Type comparison (objects only) Left to right 8 == != Relational is equal to Relational is not equal to Left to right 7 & Bitwise AND Left to right 6 ^ Bitwise exclusive OR Left to right 5 | Bitwise inclusive OR Left to right 4 && Logical AND Left to right 3 || Logical OR Left to right 2 ? : Ternary conditional Right to left 1 = += -= *= /= %= Assignment Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulus assignment Right to left
  • 29.
    Summary  In thislesson you learnt about  About Java  How to download and install Java in PC.  Java First Program  Operators and its Types  Operator Precedence