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

PDF
Java 8 Stream API. A different way to process collections.
PPTX
Computer programming 2 Lesson 10
PDF
8 python data structure-1
PPT
Java 8 Streams
PPTX
Java 8 Intro - Core Features
PDF
Java methods or Subroutines or Functions
PPT
Stack Implementation
PDF
Guava Overview. Part 1 @ Bucharest JUG #1
Java 8 Stream API. A different way to process collections.
Computer programming 2 Lesson 10
8 python data structure-1
Java 8 Streams
Java 8 Intro - Core Features
Java methods or Subroutines or Functions
Stack Implementation
Guava Overview. Part 1 @ Bucharest JUG #1

What's hot (20)

PDF
Java 8 new features or the ones you might actually use
PDF
Algorithms: II
PDF
Functional programming java
PDF
Functional programming 101
PPTX
Grid search (parameter tuning)
PPTX
Python programming –part 3
PPTX
Functions in advanced programming
PDF
Algorithms: I
PPTX
Basic Sorting algorithms csharp
PPT
Python session 10
PPT
Python advanced 3.the python std lib by example –data structures
PDF
Stack & Queue
PDF
Java 8 Lambda Expressions
DOCX
PPTX
Algorithm analysis and design
PPT
Chap03alg
PDF
Priority Queue
PDF
Java programs
PDF
Java8: Language Enhancements
ODP
2.2 higher order-functions
Java 8 new features or the ones you might actually use
Algorithms: II
Functional programming java
Functional programming 101
Grid search (parameter tuning)
Python programming –part 3
Functions in advanced programming
Algorithms: I
Basic Sorting algorithms csharp
Python session 10
Python advanced 3.the python std lib by example –data structures
Stack & Queue
Java 8 Lambda Expressions
Algorithm analysis and design
Chap03alg
Priority Queue
Java programs
Java8: Language Enhancements
2.2 higher order-functions
Ad

Similar to Java data types (20)

PPTX
Java8lambda
PPTX
Exploring Streams and Lambdas in Java8
PPT
ch04-conditional-execution.ppt
PPTX
Functional Programming in Swift
PPTX
Java chapter 3
PPT
Data types and Operators
PPT
Introduction to Java Programming Part 2
PPTX
Polish
PDF
New Functional Features of Java 8
PPT
02basics
PDF
Lec16-CS110 Computational Engineering
PPT
object oriented programming java lectures
PPTX
Pi j1.3 operators
PPTX
Python basics
PPTX
Java 8 new features
PPTX
Arithmetic Operators ____ java.pptx
PPTX
Java introduction
PPTX
presentation on array java program operators
PDF
Simulink 1.pdf
PPT
Chapter 2&3 (java fundamentals and Control Structures).ppt
Java8lambda
Exploring Streams and Lambdas in Java8
ch04-conditional-execution.ppt
Functional Programming in Swift
Java chapter 3
Data types and Operators
Introduction to Java Programming Part 2
Polish
New Functional Features of Java 8
02basics
Lec16-CS110 Computational Engineering
object oriented programming java lectures
Pi j1.3 operators
Python basics
Java 8 new features
Arithmetic Operators ____ java.pptx
Java introduction
presentation on array java program operators
Simulink 1.pdf
Chapter 2&3 (java fundamentals and Control Structures).ppt
Ad

More from Kuppusamy P (20)

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

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Complications of Minimal Access Surgery at WLH
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Types and Its function , kingdom of life
PPTX
Cell Structure & Organelles in detailed.
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Pharma ospi slides which help in ospi learning
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Basic Mud Logging Guide for educational purpose
Microbial disease of the cardiovascular and lymphatic systems
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPH.pptx obstetrics and gynecology in nursing
Complications of Minimal Access Surgery at WLH
O5-L3 Freight Transport Ops (International) V1.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Types and Its function , kingdom of life
Cell Structure & Organelles in detailed.
GDM (1) (1).pptx small presentation for students
Pharma ospi slides which help in ospi learning
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
TR - Agricultural Crops Production NC III.pdf
Basic Mud Logging Guide for educational purpose

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.