SlideShare a Scribd company logo
Variables, Data Type in JAVA
Dr. Kuppusamy .P
Associate Professor / SCOPE
Package
• Collection of classes and interfaces
Java API
• Application programming interface(API) is a collection of prewritten packages,
classes, and interfaces with their respective methods, fields and constructors
• The Java API describes the function of each of its components
• In Java programming, many of these components are pre-created and commonly
used
Java Features
• Object-Oriented
• Supports encapsulation, inheritance, abstraction, and polymorphism
• Distributed
• Libraries for network programming
• Remote Method Invocation
• Architecture neutral
• Java Bytecodes are interpreted by the JVM
Dr. Kuppusamy P
Java Advantages
• Secure
• Difficult to break Java security mechanisms
• Java Bytecode verification
• Portable
• Primitive data type sizes and their arithmetic behavior specified
by the language
• Libraries define portable interfaces
• Multithreaded
• Threads are easy to create and use
Dr. Kuppusamy P
Keywords in Java
Dr. Kuppusamy P
Primitive Data Types
Dr. Kuppusamy P
Types of Variables in JAVA
• Local Variables
• Tied to a method
• Scope of a local variable is within the method
• Instance Variables (Non-static)
• Tied to an object
• Its an object at specific moment
• Scope of an instance variable is the whole class
• Static Variables
• Tied to a class
• Shared by all instances of a class
Dr. Kuppusamy P
Java – Types of Operators
• Operators are used to manipulate operations.
Types of operators in java are,
• Arithmetic Operators
• Unary Operator
• Relational Operators
• Logical Operators
• Simple Assignment Operator
• Bitwise Operators
Dr. Kuppusamy P
Arithmetic Operators
Dr. Kuppusamy P
Arithmetic Operators
/* Arithmetic operations */
class Example
{
public static void main(String[ ] args )
{
int a = 10;
int b = 3;
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) );
}
}
Dr. Kuppusamy P
Unary Operators
Dr. Kuppusamy P
Unary Operators
/* Unary operator */
class SampleUnary
{
public static void main(String args[])
{
int a = 10;
int b = 20;
System.out.println("++a = " + (++a) );
System.out.println("--b= " + (--b) );
}
}
Dr. Kuppusamy P
Quiz
• What will be the output, if compile and execute the following
code?
class Quiz1
{
public static void main(String [ ] args)
{
int a=15;
int b=25;
System.out.println(++a + (--b));
}
}
Dr. Kuppusamy P
Relational Operators
Dr. Kuppusamy P
Relational Operators
/* Relational operations */
class SampleRelation
{
public static void main(String[] args )
{
int a = 25;
int b = 45;
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("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}
}
Dr. Kuppusamy P
String Comparison
/* String Comparison*/
class Sample3
{
public static void main(String[] args )
{
String s1=new String(“come");
String s2=" Welcome ";
String s3=" Welcome ";
System.out.println(s2==s3);
System.out.println(s2.equals(s3));
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
}
}
Dr. Kuppusamy P
Logical Operators
Dr. Kuppusamy P
Logical Operators
/* Logical Operations*/
class Sample4
{
public static void main(String[] args )
{
boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a && b) );
System.out.println("a || b = " + (a || b) );
System.out.println("!(a && b) = " + !(a && b) );
}
}
Dr. Kuppusamy P
Assignment Operator
• = Simple assignment operator assigns right hand side value to
left hand side variable
• Ex: int a;
a = 10;
• String name;
name = ‘Akhil’
Dr. Kuppusamy P
Quiz
What will be the output, if we compile and execute the below code?
public class Sample5
{
public static void main()
{
int i= 50, j= 78;
boolean chk;
chk = i > j;
System.out.println("chk value: “ +chk);
}
}
Dr. Kuppusamy P
Shift Operators
• Two types.
o Right shift (>>)
o Left shift (<<)
• The shift operators (<< and >>) shift the bits of a given number to the left or
right, resulting a new number.
• They are used only on integer numbers i.e. decimals (not on floating point
numbers).
• Right shift operator(>>) is used to divide a number in the multiples of 2.
• Left shift operator(<<) is used to multiply a number in the multiples of 2.
Dr. Kuppusamy P
Right Shift (>>) Operators
• Let’s use right shift operator with the following example :
• int x = 16;
• x = x >> 3;
• When apply the right shift operator >>, the value gets divided by 2 to the
power of number specified after the operator.
• In this case, 3 is the value after the right shift operator.
• So, 16 will be divided by the value 2 to the power of 3. (16 / 23 = 2)
• The result is 2.
Dr. Kuppusamy P
Right Shift (>>) Operators
• Represent 16 in binary form as follows:
0 0 0 1 0 0 0 0
• When apply >> right shift operator, every bit moves by 3 positions to the right
(represented by the number after the right shift operator).
• After shifting the binary digits:
0 0 0 0 0 0 1 0
x = 2
Dr. Kuppusamy P
class ShiftExample1 {
public static void main(String[] args) {
int x = 16;
System.out.println("The original value of x is "+x);
x = x >> 3;
System.out.println("After using >> 3, the new value is "+x);
} }
Left Shift (<<) Operators
• Let’s use left shift operator with the following example :
• int x = 4;
• x = x << 2;
• When apply the left shift operator <<, the value gets multiplied by 2 to the
power of number specified after the operator.
• In this case, 2 is the value after the left shift operator.
• So, 4 will be multiplied by the value 2 to the power of 2. (4 * 22 = 16)
• The result is 16.
Dr. Kuppusamy P
Left Shift (<<) Operators
• Represent 4 in binary form as follows:
0 0 0 0 0 1 0 0
• When apply << left shift operator, every bit moves by 2 positions to the left
(represented by the number after the left shift operator).
• After shifting the binary digits:
0 0 0 1 0 0 0 0
x = 16
Dr. Kuppusamy P
class ShiftExample2 {
public static void main(String[] args) {
int x = 4;
System.out.println("The original value of x is "+x);
x = x << 2;
System.out.println("After using << 2, the new value is "+x);
} }
Bitwise Operators
• The bitwise operators take two bit numbers, use OR/AND to determine the
result on a bit by bit basis.
• The 3 bitwise operators are :
• & (which is the bitwise AND)
• | (which is the bitwise inclusive OR)
• ^ (which is the bitwise exclusive OR)
Dr. Kuppusamy P
Bitwise Operators
class BitwiseExample1
{
public static void main(String[] args)
{
int x = 7;
int y = 9;
System.out.println(x & y);
System.out.println(x | y);
System.out.println(x ^ y);
}
}
Dr. Kuppusamy P
Output:
1
15
14
Explanation: AND
7 = 0 1 1 1
9 = 1 0 0 1
___________
& = 0 0 0 1
OR
7 = 0 1 1 1
9 = 1 0 0 1
_________
| = 1 1 1 1
NOT
7 = 0 1 1 1
9 = 1 0 0 1
_________
^ = 1 1 1 0
References
Dr. Kuppusamy P
Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition,
2017.

More Related Content

What's hot (20)

PDF
Java 8 new features or the ones you might actually use
Sharon Rozinsky
 
PDF
Algorithms: II
Joyjit Choudhury
 
PDF
Functional programming java
Maneesh Chaturvedi
 
PDF
Functional programming 101
Maneesh Chaturvedi
 
PPTX
Grid search (parameter tuning)
Akhilesh Joshi
 
PPTX
Python programming –part 3
Megha V
 
PPTX
Functions in advanced programming
VisnuDharsini
 
PDF
Algorithms: I
Joyjit Choudhury
 
PPTX
Basic Sorting algorithms csharp
Micheal Ogundero
 
PPT
Python session 10
Navaneethan Naveen
 
PPT
Python advanced 3.the python std lib by example –data structures
John(Qiang) Zhang
 
PDF
Stack & Queue
Joyjit Choudhury
 
PDF
Java 8 Lambda Expressions
Scott Leberknight
 
DOCX
Java programs
Dr.M.Karthika parthasarathy
 
PPTX
Algorithm analysis and design
Megha V
 
PPT
Chap03alg
Munkhchimeg
 
PDF
Priority Queue
Joyjit Choudhury
 
PDF
Java programs
Mukund Gandrakota
 
PDF
Java8: Language Enhancements
Yuriy Bondaruk
 
ODP
2.2 higher order-functions
futurespective
 
Java 8 new features or the ones you might actually use
Sharon Rozinsky
 
Algorithms: II
Joyjit Choudhury
 
Functional programming java
Maneesh Chaturvedi
 
Functional programming 101
Maneesh Chaturvedi
 
Grid search (parameter tuning)
Akhilesh Joshi
 
Python programming –part 3
Megha V
 
Functions in advanced programming
VisnuDharsini
 
Algorithms: I
Joyjit Choudhury
 
Basic Sorting algorithms csharp
Micheal Ogundero
 
Python session 10
Navaneethan Naveen
 
Python advanced 3.the python std lib by example –data structures
John(Qiang) Zhang
 
Stack & Queue
Joyjit Choudhury
 
Java 8 Lambda Expressions
Scott Leberknight
 
Algorithm analysis and design
Megha V
 
Chap03alg
Munkhchimeg
 
Priority Queue
Joyjit Choudhury
 
Java programs
Mukund Gandrakota
 
Java8: Language Enhancements
Yuriy Bondaruk
 
2.2 higher order-functions
futurespective
 

Similar to Java data types (20)

PPTX
Computer programming 2 Lesson 7
MLG College of Learning, Inc
 
PPTX
Operators
VijayaLakshmi506
 
PPT
object oriented programming java lectures
MSohaib24
 
PPT
3.OPERATORS_MB.ppt .
happycocoman
 
PPTX
Arithmetic Operators ____ java.pptx
gnyanadeepa
 
PPTX
OOPJ_PPT2,JAVA OPERATORS TPYE WITH EXAMPLES.pptx
SrinivasGopalan2
 
PPTX
presentation on array java program operators
anushaashraf20
 
PPT
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
PDF
5_Operators.pdf
NiraliArora2
 
PDF
Java basic operators
Emmanuel Alimpolos
 
PDF
Java basic operators
Emmanuel Alimpolos
 
PPTX
PPT ON JAVA AND UNDERSTANDING JAVA'S PRINCIPLES
merabapudc
 
PDF
itft-Operators in java
Atul Sehdev
 
DOCX
Operators
loidasacueza
 
PPT
Java operators
Shehrevar Davierwala
 
PPT
Operators
Daman Toor
 
PPTX
Pj01 4-operators and control flow
SasidharaRaoMarrapu
 
PDF
4.Lesson Plan - Java Operators.pdf...pdf
AbhishekSingh757567
 
PPTX
Java Operators with Simple introduction.pptx
kuntadinesh21
 
PPTX
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
Computer programming 2 Lesson 7
MLG College of Learning, Inc
 
Operators
VijayaLakshmi506
 
object oriented programming java lectures
MSohaib24
 
3.OPERATORS_MB.ppt .
happycocoman
 
Arithmetic Operators ____ java.pptx
gnyanadeepa
 
OOPJ_PPT2,JAVA OPERATORS TPYE WITH EXAMPLES.pptx
SrinivasGopalan2
 
presentation on array java program operators
anushaashraf20
 
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
5_Operators.pdf
NiraliArora2
 
Java basic operators
Emmanuel Alimpolos
 
Java basic operators
Emmanuel Alimpolos
 
PPT ON JAVA AND UNDERSTANDING JAVA'S PRINCIPLES
merabapudc
 
itft-Operators in java
Atul Sehdev
 
Operators
loidasacueza
 
Java operators
Shehrevar Davierwala
 
Operators
Daman Toor
 
Pj01 4-operators and control flow
SasidharaRaoMarrapu
 
4.Lesson Plan - Java Operators.pdf...pdf
AbhishekSingh757567
 
Java Operators with Simple introduction.pptx
kuntadinesh21
 
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
Ad

More from Kuppusamy P (20)

PDF
Recurrent neural networks rnn
Kuppusamy P
 
PDF
Deep learning
Kuppusamy P
 
PDF
Image segmentation
Kuppusamy P
 
PDF
Image enhancement
Kuppusamy P
 
PDF
Feature detection and matching
Kuppusamy P
 
PDF
Image processing, Noise, Noise Removal filters
Kuppusamy P
 
PDF
Flowchart design for algorithms
Kuppusamy P
 
PDF
Algorithm basics
Kuppusamy P
 
PDF
Problem solving using Programming
Kuppusamy P
 
PDF
Parts of Computer, Hardware and Software
Kuppusamy P
 
PDF
Strings in java
Kuppusamy P
 
PDF
Java arrays
Kuppusamy P
 
PDF
Java iterative statements
Kuppusamy P
 
PDF
Java conditional statements
Kuppusamy P
 
PDF
Java introduction
Kuppusamy P
 
PDF
Logistic regression in Machine Learning
Kuppusamy P
 
PDF
Anomaly detection (Unsupervised Learning) in Machine Learning
Kuppusamy P
 
PDF
Machine Learning Performance metrics for classification
Kuppusamy P
 
PDF
Machine learning Introduction
Kuppusamy P
 
PDF
Reinforcement learning, Q-Learning
Kuppusamy P
 
Recurrent neural networks rnn
Kuppusamy P
 
Deep learning
Kuppusamy P
 
Image segmentation
Kuppusamy P
 
Image enhancement
Kuppusamy P
 
Feature detection and matching
Kuppusamy P
 
Image processing, Noise, Noise Removal filters
Kuppusamy P
 
Flowchart design for algorithms
Kuppusamy P
 
Algorithm basics
Kuppusamy P
 
Problem solving using Programming
Kuppusamy P
 
Parts of Computer, Hardware and Software
Kuppusamy P
 
Strings in java
Kuppusamy P
 
Java arrays
Kuppusamy P
 
Java iterative statements
Kuppusamy P
 
Java conditional statements
Kuppusamy P
 
Java introduction
Kuppusamy P
 
Logistic regression in Machine Learning
Kuppusamy P
 
Anomaly detection (Unsupervised Learning) in Machine Learning
Kuppusamy P
 
Machine Learning Performance metrics for classification
Kuppusamy P
 
Machine learning Introduction
Kuppusamy P
 
Reinforcement learning, Q-Learning
Kuppusamy P
 
Ad

Recently uploaded (20)

PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
Introduction to Indian Writing in English
Trushali Dodiya
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Horarios de distribución de agua en julio
pegazohn1978
 

Java data types

  • 1. Variables, Data Type in JAVA Dr. Kuppusamy .P Associate Professor / SCOPE
  • 2. Package • Collection of classes and interfaces Java API • Application programming interface(API) is a collection of prewritten packages, classes, and interfaces with their respective methods, fields and constructors • The Java API describes the function of each of its components • In Java programming, many of these components are pre-created and commonly used Java Features • Object-Oriented • Supports encapsulation, inheritance, abstraction, and polymorphism • Distributed • Libraries for network programming • Remote Method Invocation • Architecture neutral • Java Bytecodes are interpreted by the JVM Dr. Kuppusamy P
  • 3. Java Advantages • Secure • Difficult to break Java security mechanisms • Java Bytecode verification • Portable • Primitive data type sizes and their arithmetic behavior specified by the language • Libraries define portable interfaces • Multithreaded • Threads are easy to create and use Dr. Kuppusamy P
  • 4. Keywords in Java Dr. Kuppusamy P
  • 6. Types of Variables in JAVA • Local Variables • Tied to a method • Scope of a local variable is within the method • Instance Variables (Non-static) • Tied to an object • Its an object at specific moment • Scope of an instance variable is the whole class • Static Variables • Tied to a class • Shared by all instances of a class Dr. Kuppusamy P
  • 7. Java – Types of Operators • Operators are used to manipulate operations. Types of operators in java are, • Arithmetic Operators • Unary Operator • Relational Operators • Logical Operators • Simple Assignment Operator • Bitwise Operators Dr. Kuppusamy P
  • 9. Arithmetic Operators /* Arithmetic operations */ class Example { public static void main(String[ ] args ) { int a = 10; int b = 3; 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) ); } } Dr. Kuppusamy P
  • 11. Unary Operators /* Unary operator */ class SampleUnary { public static void main(String args[]) { int a = 10; int b = 20; System.out.println("++a = " + (++a) ); System.out.println("--b= " + (--b) ); } } Dr. Kuppusamy P
  • 12. Quiz • What will be the output, if compile and execute the following code? class Quiz1 { public static void main(String [ ] args) { int a=15; int b=25; System.out.println(++a + (--b)); } } Dr. Kuppusamy P
  • 14. Relational Operators /* Relational operations */ class SampleRelation { public static void main(String[] args ) { int a = 25; int b = 45; 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("b >= a = " + (b >= a) ); System.out.println("b <= a = " + (b <= a) ); } } Dr. Kuppusamy P
  • 15. String Comparison /* String Comparison*/ class Sample3 { public static void main(String[] args ) { String s1=new String(“come"); String s2=" Welcome "; String s3=" Welcome "; System.out.println(s2==s3); System.out.println(s2.equals(s3)); System.out.println(s1==s2); System.out.println(s1.equals(s2)); } } Dr. Kuppusamy P
  • 17. Logical Operators /* Logical Operations*/ class Sample4 { public static void main(String[] args ) { boolean a = true; boolean b = false; System.out.println("a && b = " + (a && b) ); System.out.println("a || b = " + (a || b) ); System.out.println("!(a && b) = " + !(a && b) ); } } Dr. Kuppusamy P
  • 18. Assignment Operator • = Simple assignment operator assigns right hand side value to left hand side variable • Ex: int a; a = 10; • String name; name = ‘Akhil’ Dr. Kuppusamy P
  • 19. Quiz What will be the output, if we compile and execute the below code? public class Sample5 { public static void main() { int i= 50, j= 78; boolean chk; chk = i > j; System.out.println("chk value: “ +chk); } } Dr. Kuppusamy P
  • 20. Shift Operators • Two types. o Right shift (>>) o Left shift (<<) • The shift operators (<< and >>) shift the bits of a given number to the left or right, resulting a new number. • They are used only on integer numbers i.e. decimals (not on floating point numbers). • Right shift operator(>>) is used to divide a number in the multiples of 2. • Left shift operator(<<) is used to multiply a number in the multiples of 2. Dr. Kuppusamy P
  • 21. Right Shift (>>) Operators • Let’s use right shift operator with the following example : • int x = 16; • x = x >> 3; • When apply the right shift operator >>, the value gets divided by 2 to the power of number specified after the operator. • In this case, 3 is the value after the right shift operator. • So, 16 will be divided by the value 2 to the power of 3. (16 / 23 = 2) • The result is 2. Dr. Kuppusamy P
  • 22. Right Shift (>>) Operators • Represent 16 in binary form as follows: 0 0 0 1 0 0 0 0 • When apply >> right shift operator, every bit moves by 3 positions to the right (represented by the number after the right shift operator). • After shifting the binary digits: 0 0 0 0 0 0 1 0 x = 2 Dr. Kuppusamy P class ShiftExample1 { public static void main(String[] args) { int x = 16; System.out.println("The original value of x is "+x); x = x >> 3; System.out.println("After using >> 3, the new value is "+x); } }
  • 23. Left Shift (<<) Operators • Let’s use left shift operator with the following example : • int x = 4; • x = x << 2; • When apply the left shift operator <<, the value gets multiplied by 2 to the power of number specified after the operator. • In this case, 2 is the value after the left shift operator. • So, 4 will be multiplied by the value 2 to the power of 2. (4 * 22 = 16) • The result is 16. Dr. Kuppusamy P
  • 24. Left Shift (<<) Operators • Represent 4 in binary form as follows: 0 0 0 0 0 1 0 0 • When apply << left shift operator, every bit moves by 2 positions to the left (represented by the number after the left shift operator). • After shifting the binary digits: 0 0 0 1 0 0 0 0 x = 16 Dr. Kuppusamy P class ShiftExample2 { public static void main(String[] args) { int x = 4; System.out.println("The original value of x is "+x); x = x << 2; System.out.println("After using << 2, the new value is "+x); } }
  • 25. Bitwise Operators • The bitwise operators take two bit numbers, use OR/AND to determine the result on a bit by bit basis. • The 3 bitwise operators are : • & (which is the bitwise AND) • | (which is the bitwise inclusive OR) • ^ (which is the bitwise exclusive OR) Dr. Kuppusamy P
  • 26. Bitwise Operators class BitwiseExample1 { public static void main(String[] args) { int x = 7; int y = 9; System.out.println(x & y); System.out.println(x | y); System.out.println(x ^ y); } } Dr. Kuppusamy P Output: 1 15 14 Explanation: AND 7 = 0 1 1 1 9 = 1 0 0 1 ___________ & = 0 0 0 1 OR 7 = 0 1 1 1 9 = 1 0 0 1 _________ | = 1 1 1 1 NOT 7 = 0 1 1 1 9 = 1 0 0 1 _________ ^ = 1 1 1 0
  • 27. References Dr. Kuppusamy P Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition, 2017.