SlideShare une entreprise Scribd logo
1  sur  12
Free Java
Tutorial Videos
(18,803 Programmers Joined)
 Name:
 Email:

Send Me
 We respect your privacy
Summary of operators in Java with examples
Last Updated on 21 November 2017 | Print Email
Master Microservices with Spring Boot and Spring Cloud
The Java programming language has around 30 operators as summarized in the following table:
Category Operators
Simple assignment =
Arithmetic + - * / %
Unary + - ++ -- !
Relational == != > >= < <=
Conditional && || ? : (ternary)
Type comparison instanceof
Bitwise and Bit
shift
~ << >> >>> & ^ |
Let’s look at each type of operator in details with examples.
1. Simple assignment
Perhaps this is the most commonly used operator. It assigns the value on its right to the operand
on its left. Here are some examples:

o Assigning numbers:
o int x = 10;
float y = 3.5F;
o Assigning object references:
String message = “Hello world”;
File csv = new File(“test.csv”);
2. Arithmetic operators
The arithmetic operators are used to perform mathematic calculations just like basic mathematics
in school. The following table lists all arithmetic operators in Java:
Operator Meaning
+ Addition (and strings concatenation) operator
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
Here’s an example program:
public class ArithmeticDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
int result = x + y;
System.out.println("x + y = " + result);
result = x - y;
System.out.println("x - y = " + result);
result = x * y;
System.out.println("x * y = " + result);
result = y / x;
System.out.println("y / x = " + result);
result = x % 3;
System.out.println("x % 3 = " + result);
}
}
Output:
x + y = 30
x - y = -10
x * y = 200
y / x = 2
x % 3 = 1
The addition operator (+) can also be used for joining two or more strings together (strings
concatenation). Here’s an example program:
public class StringConcatDemo {
public static void main(String[] args) {
String firstName = "James";
String lastName = "Gosling";
String greeting = "Hello " + firstName + " " + lastName;
System.out.println(greeting);
}
}
Output:
Hello James Gosling!
3. Unary operators
The unary operators involve in only a single operand. The following table lists all unary
operators in Java:
Operator Meaning
+ Unary plus operator; indicates positive value
(numbers are positive by default, without this
operator).
- Unary minus operator; negate an expression.
++ Increment operator; increments a value by 1.
-- Decrement operator; decrements a value by 1;
! Logical complement operator; inverts value of
a boolean.
Here’s an example program:
public class UnaryDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
int result = +x;
System.out.println("+x = " + result);
result = -y;
System.out.println("-y = " + result);
result = ++x;
System.out.println("++x = " + result);
result = --y;
System.out.println("--y = " + result);
boolean ok = false;
System.out.println(ok);
System.out.println(!ok);
}
}
Output:
+x = 10
-y = -20
++x = 11
++y = 19
false
true
Note that the increment and decrement operators can be placed before (prefix) or after (postfix)
the operand, e.g. ++x or x++, --y or y--. When using these two forms in an expression, the
difference is:

o Prefix form: the operand is incremented or decremented before used in the expression.
o Postfix form: the operand is incremented or decremented after used in the expression.
The following example illustrates the prefix/postfix:
public class PrefixPostfixDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
System.out.println(++x);
System.out.println(x++);
System.out.println(x);
System.out.println(--y);
System.out.println(y--);
System.out.println(y);
}
}
Output:
11
11
12
19
19
18
4. Relational operators
The relational operators are used to compare two operands or two expressions and result is a
boolean. The following table lists all relational operators in Java.
Operator Meaning
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
Here’s an example program:
public class RelationalDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
boolean result = x == y;
System.out.println("x == y? " + result);
result = x != y;
System.out.println("x != y? " + result);
result = x > y;
System.out.println("x > y? " + result);
result = x >= y;
System.out.println("x >= y? " + result);
result = x < y;
System.out.println("x < y? " + result);
result = x <= y;
System.out.println("x <= y? " + result);
}
}
Output:
x == y? false
x != y? true
x > y? false
x >= y? false
x < y? true
x <= y? true
5. Conditional operators
Operator Meaning
&& conditional -AND operator
|| conditional-OR operator
? : ternary operator in form of: A ? B : C
The conditional operators (&& and ||) are used to perform conditional-AND and conditional-OR
operations on two boolean expressions and result in a boolean value. They have “short-
circuiting” behavior:

o For the && operator: if the left expression is evaluated to false, then the right expression is
not evaluated. Final result is false.
o For the || operator: if the left expression is evaluated to true, then the right expression is
not evaluated. Final result is true.
Here’s an example program:
public class ConditionalDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
if ((x > 8) && (y > 8)) {
System.out.println("Both x and y are greater than 8");
}
if ((x > 10) || (y > 10)) {
System.out.println("Either x or y is greater than 10");
}
}
}
Output:
Both x and y are less than 8
Either x or y is greater than 10
Other conditional operators are ? and : which form a ternary (three operands) in the following
form:
result = A ? B : C
This is interpreted like this: if A evaluates to true, then evaluates B and assign its value to the
result. Otherwise, if A evaluates to false, then evaluates C and assign its value to the result. For
short, we can say: If A then B else C. So this is also referred as shorthand for an if-then-else
statement.
Here’s an example of the ternary operator:
public class TernaryDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
int result = (x > 10) ? x : y;
System.out.println("result 1 is: " + result);
result = (y > 10) ? x : y;
System.out.println("result 2 is: " + result);
}
}
Output:
result 1 is: 20
result 2 is: 10
6. Type comparison operator (instanceof)
The instanceof operator tests if an object is an instance of a class, a subclass or a class that
implements an interface; and result in a boolean value.
Here’s an example program:
public class InstanceofDemo {
public static void main(String[] args) {
String name = "Java";
if (name instanceof String) {
System.out.println("an instance of String class");
}
// test for subclass of Object
if (name instanceof Object) {
System.out.println("an instance of Object class");
}
// test for subclass of an interface
if (name instanceof CharSequence) {
System.out.println("an instance of CharSequence interface");
}
}
}
Output:
an instance of String class
an instance of Object class
an instance of CharSequence interface
7. Bitwise and Bit shift operators
These operators perform bitwise and bit shift operations on only integral types, not float types.
They are rarely used so the listing here is just for reference:
Operator Meaning
~ unary bitwise complement; inverts a bit pattern
<< signed left shift
>> signed right shift
>>> unsigned right shift
& bitwise AND
^ bitwise exclusive OR
| bitwise inclusive OR
Here’s an example program:
public class BitDemo {
public static void main(String[] args) {
int x = 10;
int result = x << 2;
System.out.println("Before left shift: " + x);
System.out.println("After left shift: " + result);
}
}
Output:
Before left shift: 10
After left shift: 40

Contenu connexe

Tendances

JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
Mohamed Ahmed
 

Tendances (20)

Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)
 
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswiftSwift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
11. java methods
11. java methods11. java methods
11. java methods
 
Array within a class
Array within a classArray within a class
Array within a class
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a Neckbeard
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
An introduction to property based testing
An introduction to property based testingAn introduction to property based testing
An introduction to property based testing
 
Logical and Conditional Operator In C language
Logical and Conditional Operator In C languageLogical and Conditional Operator In C language
Logical and Conditional Operator In C language
 
Templates
TemplatesTemplates
Templates
 
Teach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with ScalaTeach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with Scala
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 
basic concepts
basic conceptsbasic concepts
basic concepts
 
The lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsThe lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of tests
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 

Similaire à Operators

Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
sotlsoc
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
Chapter 4.2
Chapter 4.2Chapter 4.2
Chapter 4.2
sotlsoc
 

Similaire à Operators (20)

Operators
OperatorsOperators
Operators
 
5_Operators.pdf
5_Operators.pdf5_Operators.pdf
5_Operators.pdf
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
 
Basic_Java_02.pptx
Basic_Java_02.pptxBasic_Java_02.pptx
Basic_Java_02.pptx
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Java unit 3
Java unit 3Java unit 3
Java unit 3
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
 
Introduction to Java Programming Part 2
Introduction to Java Programming Part 2Introduction to Java Programming Part 2
Introduction to Java Programming Part 2
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
Operators in java
Operators in javaOperators in java
Operators in java
 
C operators
C operatorsC operators
C operators
 
Lambda Functions in Java 8
Lambda Functions in Java 8Lambda Functions in Java 8
Lambda Functions in Java 8
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Python programing
Python programingPython programing
Python programing
 
Chapter 4.2
Chapter 4.2Chapter 4.2
Chapter 4.2
 

Dernier

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Dernier (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 

Operators

  • 1. Free Java Tutorial Videos (18,803 Programmers Joined)  Name:  Email:  Send Me  We respect your privacy
  • 2.
  • 3. Summary of operators in Java with examples Last Updated on 21 November 2017 | Print Email Master Microservices with Spring Boot and Spring Cloud The Java programming language has around 30 operators as summarized in the following table: Category Operators Simple assignment = Arithmetic + - * / % Unary + - ++ -- ! Relational == != > >= < <= Conditional && || ? : (ternary) Type comparison instanceof Bitwise and Bit shift ~ << >> >>> & ^ | Let’s look at each type of operator in details with examples. 1. Simple assignment Perhaps this is the most commonly used operator. It assigns the value on its right to the operand on its left. Here are some examples:  o Assigning numbers: o int x = 10; float y = 3.5F; o Assigning object references: String message = “Hello world”; File csv = new File(“test.csv”); 2. Arithmetic operators The arithmetic operators are used to perform mathematic calculations just like basic mathematics in school. The following table lists all arithmetic operators in Java: Operator Meaning + Addition (and strings concatenation) operator - Subtraction operator * Multiplication operator / Division operator % Remainder operator Here’s an example program: public class ArithmeticDemo { public static void main(String[] args) {
  • 4. int x = 10; int y = 20; int result = x + y; System.out.println("x + y = " + result); result = x - y; System.out.println("x - y = " + result); result = x * y; System.out.println("x * y = " + result); result = y / x; System.out.println("y / x = " + result); result = x % 3; System.out.println("x % 3 = " + result); } } Output: x + y = 30 x - y = -10 x * y = 200 y / x = 2 x % 3 = 1
  • 5. The addition operator (+) can also be used for joining two or more strings together (strings concatenation). Here’s an example program: public class StringConcatDemo { public static void main(String[] args) { String firstName = "James"; String lastName = "Gosling"; String greeting = "Hello " + firstName + " " + lastName; System.out.println(greeting); } } Output: Hello James Gosling! 3. Unary operators The unary operators involve in only a single operand. The following table lists all unary operators in Java: Operator Meaning + Unary plus operator; indicates positive value (numbers are positive by default, without this operator). - Unary minus operator; negate an expression. ++ Increment operator; increments a value by 1. -- Decrement operator; decrements a value by 1; ! Logical complement operator; inverts value of a boolean. Here’s an example program: public class UnaryDemo { public static void main(String[] args) { int x = 10; int y = 20; int result = +x; System.out.println("+x = " + result);
  • 6. result = -y; System.out.println("-y = " + result); result = ++x; System.out.println("++x = " + result); result = --y; System.out.println("--y = " + result); boolean ok = false; System.out.println(ok); System.out.println(!ok); } } Output: +x = 10 -y = -20 ++x = 11 ++y = 19 false true Note that the increment and decrement operators can be placed before (prefix) or after (postfix) the operand, e.g. ++x or x++, --y or y--. When using these two forms in an expression, the difference is:  o Prefix form: the operand is incremented or decremented before used in the expression. o Postfix form: the operand is incremented or decremented after used in the expression. The following example illustrates the prefix/postfix: public class PrefixPostfixDemo {
  • 7. public static void main(String[] args) { int x = 10; int y = 20; System.out.println(++x); System.out.println(x++); System.out.println(x); System.out.println(--y); System.out.println(y--); System.out.println(y); } } Output: 11 11 12 19 19 18 4. Relational operators The relational operators are used to compare two operands or two expressions and result is a boolean. The following table lists all relational operators in Java. Operator Meaning == equal to != not equal to > greater than >= greater than or equal to < less than
  • 8. <= less than or equal to Here’s an example program: public class RelationalDemo { public static void main(String[] args) { int x = 10; int y = 20; boolean result = x == y; System.out.println("x == y? " + result); result = x != y; System.out.println("x != y? " + result); result = x > y; System.out.println("x > y? " + result); result = x >= y; System.out.println("x >= y? " + result); result = x < y; System.out.println("x < y? " + result); result = x <= y; System.out.println("x <= y? " + result); } }
  • 9. Output: x == y? false x != y? true x > y? false x >= y? false x < y? true x <= y? true 5. Conditional operators Operator Meaning && conditional -AND operator || conditional-OR operator ? : ternary operator in form of: A ? B : C The conditional operators (&& and ||) are used to perform conditional-AND and conditional-OR operations on two boolean expressions and result in a boolean value. They have “short- circuiting” behavior:  o For the && operator: if the left expression is evaluated to false, then the right expression is not evaluated. Final result is false. o For the || operator: if the left expression is evaluated to true, then the right expression is not evaluated. Final result is true. Here’s an example program: public class ConditionalDemo { public static void main(String[] args) { int x = 10; int y = 20; if ((x > 8) && (y > 8)) { System.out.println("Both x and y are greater than 8"); } if ((x > 10) || (y > 10)) { System.out.println("Either x or y is greater than 10"); } }
  • 10. } Output: Both x and y are less than 8 Either x or y is greater than 10 Other conditional operators are ? and : which form a ternary (three operands) in the following form: result = A ? B : C This is interpreted like this: if A evaluates to true, then evaluates B and assign its value to the result. Otherwise, if A evaluates to false, then evaluates C and assign its value to the result. For short, we can say: If A then B else C. So this is also referred as shorthand for an if-then-else statement. Here’s an example of the ternary operator: public class TernaryDemo { public static void main(String[] args) { int x = 10; int y = 20; int result = (x > 10) ? x : y; System.out.println("result 1 is: " + result); result = (y > 10) ? x : y; System.out.println("result 2 is: " + result); } } Output: result 1 is: 20 result 2 is: 10 6. Type comparison operator (instanceof)
  • 11. The instanceof operator tests if an object is an instance of a class, a subclass or a class that implements an interface; and result in a boolean value. Here’s an example program: public class InstanceofDemo { public static void main(String[] args) { String name = "Java"; if (name instanceof String) { System.out.println("an instance of String class"); } // test for subclass of Object if (name instanceof Object) { System.out.println("an instance of Object class"); } // test for subclass of an interface if (name instanceof CharSequence) { System.out.println("an instance of CharSequence interface"); } } } Output: an instance of String class an instance of Object class an instance of CharSequence interface 7. Bitwise and Bit shift operators These operators perform bitwise and bit shift operations on only integral types, not float types. They are rarely used so the listing here is just for reference: Operator Meaning ~ unary bitwise complement; inverts a bit pattern << signed left shift >> signed right shift
  • 12. >>> unsigned right shift & bitwise AND ^ bitwise exclusive OR | bitwise inclusive OR Here’s an example program: public class BitDemo { public static void main(String[] args) { int x = 10; int result = x << 2; System.out.println("Before left shift: " + x); System.out.println("After left shift: " + result); } } Output: Before left shift: 10 After left shift: 40