SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
Data Types and
Variables
What are DataTypes?
 Data-types means containers.
 They specify what kind of data to hold
 In java, there are two types of data types
 Primitive data types
 Non-primitive data types
Primitive Data Types
Non Primitive Data Types
 Strings
 Arrays
 Class
Variables
 Variable is name of reserved area allocated in memory.
 There are three types of variables in java
 local variable
 A variable that is declared inside the method is called local variable.
 instance variable
 A variable that is declared inside the class but outside the method is
called instance variable . It is not declared as static.
 static variable
 A variable that is declared as static is called static variable. It cannot
be local.
Variables
Syntax:
Datatype variable_name = value ;
Example
int id = 34;
float mks=67.34;
int int =45; //invalid
Literals
 Literals are numbers, characters, and string representations used
in a program.
 Numeric Literals
int roll_no=45;
 Character literals
char letter = ‘b';
letter = ‘S';
 String literals
String message = "HellotWorldn";
Casting is required when there is a need to explicitly convert a
value from one type to another.
The general syntax for a cast is:
Examples
Casting
(result_type) value;
float price = 37.53;
int dollars = (int) price; // fractional portion lost
// dollars = 37
char response = 'A';
byte temp =(byte) response; // temp = 65 (ASCII value
// for 'a')
Type Casting
 Assigning a value of one type to a variable of another
type is known as Type Casting.
 In Java, type casting is classified into two types,
 Widening Casting(Implicit)
 Narrowing Casting(Explicitly done)
A widening conversion occurs when a value stored in a
smaller space is converted to a type of a larger space.
There will never be a loss of information
Widening conversions occur automatically when needed.
Widening Conversions
A narrowing conversion occurs when a value stored in a larger
space is converted to a type of a smaller space.
Information may be lost
Never occurs automatically. Must be explicitly requested by
the programmer using a cast.
Narrowing Conversions
Strings
 String is a sequence of characters. In the Java programming
language, strings are objects. There are two types of String
i.e
 1. Mutable
 2.Immutable
 In java, string objects are immutable.
 Immutable simply means unmodifiable or unchangeable. It
is immutable because object reference is created.
String Formatting
StringBuffer Class
 Java StringBuffer class is used to created mutable
(modifiable) string. The StringBuffer class in java is same as
String class except it is mutable i.e. it can be changed.
class StringDemo{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
StringBuilder Class
 Java StringBuilder class is used to create mutable (modifiable) string.
 The Java StringBuilder class is same as StringBuffer class except that it
is non-synchronized.
class A{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
Difference between String and
StringBuffer
String
 String class is immutable
 String is slow and
consumes more memory
 String class overrides the
equals() method of
Object class.
StringBuffer
 StringBuffer class is
mutable
 StringBuffer is fast and
consumes less memory
 StringBuffer class doesn't
override the equals()
method of Object class
Difference between String and
StringBuffer conti..
StringBuffer
 StringBuffer
is synchronized
 StringBuffer is less
efficient than
StringBuilder.
StringBuilder
 StringBuilder is non-
synchronized
 StringBuilder is more
efficient than
StringBuffer.
Arrays
 An array is an ordered list of values
0 1 2 3 4 5 6 7 8 9
79 87 94 82 67 98 87 81 74 91
An array of size N is indexed from zero to N-1
rollno
The entire array
has a single name
Each value has a numeric index
This array holds 10 values that are indexed from 0 to 9
Array - Syntax
 A particular value in an array is referenced using the array
name followed by the index in brackets.
 Datatype [ ] variable_name = new datatype[size];
 Ex: int [] scores = new int [10];
 In JAVA, int is of 4 bytes, total space=4*10=40 bytes.
 Space Allocation is based on the datatype used to declare
array.
Types of Array
 Single Dimensional Array (1 D Array)
 Two Dimensional Array (2 D Array)
 Multidimensional Array(Arrays of Arrays)
Dot Operator
 Dot operator is used to access methods and variables
within classes.
 It is represented as . (dot)
Example
class Test
{
public int a;
public display()
{
System.out.println(“Hello”);
}
}
public static void main(String args[])
{
Test t=new Test();
t.a=32; //accessing dot
t.display(); //accessing display
}
}

More Related Content

What's hot (20)

PPTX
Type casting in java
Farooq Baloch
 
PPTX
Inheritance in java
RahulAnanda1
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPTX
Method overloading
Lovely Professional University
 
PPTX
Access specifiers(modifiers) in java
HrithikShinde
 
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Arrays in Java
Abhilash Nair
 
PPT
Abstract class in java
Lovely Professional University
 
PPTX
Interface in java
PhD Research Scholar
 
PDF
Applets
Prabhakaran V M
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPTX
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
PPT
Final keyword in java
Lovely Professional University
 
PPTX
Operators in java
Then Murugeshwari
 
PPTX
Java constructors
QUONTRASOLUTIONS
 
PPTX
Java string handling
Salman Khan
 
PDF
Class and Objects in Java
Spotle.ai
 
Type casting in java
Farooq Baloch
 
Inheritance in java
RahulAnanda1
 
Exception Handling in JAVA
SURIT DATTA
 
Arrays in Java
Naz Abdalla
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Method overloading
Lovely Professional University
 
Access specifiers(modifiers) in java
HrithikShinde
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Constructor in java
Pavith Gunasekara
 
Arrays in Java
Abhilash Nair
 
Abstract class in java
Lovely Professional University
 
Interface in java
PhD Research Scholar
 
Classes objects in java
Madishetty Prathibha
 
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
Final keyword in java
Lovely Professional University
 
Operators in java
Then Murugeshwari
 
Java constructors
QUONTRASOLUTIONS
 
Java string handling
Salman Khan
 
Class and Objects in Java
Spotle.ai
 

Similar to Data Types & Variables in JAVA (20)

PPTX
Chapter i(introduction to java)
Chhom Karath
 
PDF
Java data types, variables and jvm
Madishetty Prathibha
 
PPTX
Java data types
AbhishekMondal42
 
PPT
JAVA CONCEPTS
Shivam Singh
 
PPTX
OOP-java-variables.pptx
ssuserb1a18d
 
PPTX
Identifiers, keywords and types
Daman Toor
 
PPT
Lesson3
Arpan91
 
PPT
Lesson3
Arvind Shah
 
PPTX
Java String
SATYAM SHRIVASTAV
 
PPTX
Java Basics 1.pptx
TouseeqHaider11
 
PPTX
arrays.pptx
SachinBhosale73
 
PPTX
Day_5.1.pptx
ishasharma835109
 
PPTX
DATATYPES IN JAVA primitive and nonprimitive.pptx
mustaq4
 
PPTX
a variable in Java must be a specified data type
Kavitha S
 
PPTX
javastringexample problems using string class
fedcoordinator
 
PPTX
Computer Programming Java Data Types.pptx
MarianneIsid
 
PPTX
Java programing language unit 1 introduction
chnrketan
 
PPTX
Java ce241
Minal Maniar
 
PPTX
1st.pptx
MahalCenteno
 
PPTX
CH1 ARRAY (1).pptx
AnkitaVerma776806
 
Chapter i(introduction to java)
Chhom Karath
 
Java data types, variables and jvm
Madishetty Prathibha
 
Java data types
AbhishekMondal42
 
JAVA CONCEPTS
Shivam Singh
 
OOP-java-variables.pptx
ssuserb1a18d
 
Identifiers, keywords and types
Daman Toor
 
Lesson3
Arpan91
 
Lesson3
Arvind Shah
 
Java String
SATYAM SHRIVASTAV
 
Java Basics 1.pptx
TouseeqHaider11
 
arrays.pptx
SachinBhosale73
 
Day_5.1.pptx
ishasharma835109
 
DATATYPES IN JAVA primitive and nonprimitive.pptx
mustaq4
 
a variable in Java must be a specified data type
Kavitha S
 
javastringexample problems using string class
fedcoordinator
 
Computer Programming Java Data Types.pptx
MarianneIsid
 
Java programing language unit 1 introduction
chnrketan
 
Java ce241
Minal Maniar
 
1st.pptx
MahalCenteno
 
CH1 ARRAY (1).pptx
AnkitaVerma776806
 
Ad

Recently uploaded (20)

PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
Digital water marking system project report
Kamal Acharya
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
PPTX
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Digital water marking system project report
Kamal Acharya
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Ad

Data Types & Variables in JAVA

  • 2. What are DataTypes?  Data-types means containers.  They specify what kind of data to hold  In java, there are two types of data types  Primitive data types  Non-primitive data types
  • 4. Non Primitive Data Types  Strings  Arrays  Class
  • 5. Variables  Variable is name of reserved area allocated in memory.  There are three types of variables in java  local variable  A variable that is declared inside the method is called local variable.  instance variable  A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.  static variable  A variable that is declared as static is called static variable. It cannot be local.
  • 6. Variables Syntax: Datatype variable_name = value ; Example int id = 34; float mks=67.34; int int =45; //invalid
  • 7. Literals  Literals are numbers, characters, and string representations used in a program.  Numeric Literals int roll_no=45;  Character literals char letter = ‘b'; letter = ‘S';  String literals String message = "HellotWorldn";
  • 8. Casting is required when there is a need to explicitly convert a value from one type to another. The general syntax for a cast is: Examples Casting (result_type) value; float price = 37.53; int dollars = (int) price; // fractional portion lost // dollars = 37 char response = 'A'; byte temp =(byte) response; // temp = 65 (ASCII value // for 'a')
  • 9. Type Casting  Assigning a value of one type to a variable of another type is known as Type Casting.  In Java, type casting is classified into two types,  Widening Casting(Implicit)  Narrowing Casting(Explicitly done)
  • 10. A widening conversion occurs when a value stored in a smaller space is converted to a type of a larger space. There will never be a loss of information Widening conversions occur automatically when needed. Widening Conversions
  • 11. A narrowing conversion occurs when a value stored in a larger space is converted to a type of a smaller space. Information may be lost Never occurs automatically. Must be explicitly requested by the programmer using a cast. Narrowing Conversions
  • 12. Strings  String is a sequence of characters. In the Java programming language, strings are objects. There are two types of String i.e  1. Mutable  2.Immutable  In java, string objects are immutable.  Immutable simply means unmodifiable or unchangeable. It is immutable because object reference is created.
  • 14. StringBuffer Class  Java StringBuffer class is used to created mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed. class StringDemo{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Hello "); sb.append("Java");//now original string is changed System.out.println(sb);//prints Hello Java } }
  • 15. StringBuilder Class  Java StringBuilder class is used to create mutable (modifiable) string.  The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized. class A{ public static void main(String args[]){ StringBuilder sb=new StringBuilder("Hello "); sb.append("Java");//now original string is changed System.out.println(sb);//prints Hello Java } }
  • 16. Difference between String and StringBuffer String  String class is immutable  String is slow and consumes more memory  String class overrides the equals() method of Object class. StringBuffer  StringBuffer class is mutable  StringBuffer is fast and consumes less memory  StringBuffer class doesn't override the equals() method of Object class
  • 17. Difference between String and StringBuffer conti.. StringBuffer  StringBuffer is synchronized  StringBuffer is less efficient than StringBuilder. StringBuilder  StringBuilder is non- synchronized  StringBuilder is more efficient than StringBuffer.
  • 18. Arrays  An array is an ordered list of values 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 rollno The entire array has a single name Each value has a numeric index This array holds 10 values that are indexed from 0 to 9
  • 19. Array - Syntax  A particular value in an array is referenced using the array name followed by the index in brackets.  Datatype [ ] variable_name = new datatype[size];  Ex: int [] scores = new int [10];  In JAVA, int is of 4 bytes, total space=4*10=40 bytes.  Space Allocation is based on the datatype used to declare array.
  • 20. Types of Array  Single Dimensional Array (1 D Array)  Two Dimensional Array (2 D Array)  Multidimensional Array(Arrays of Arrays)
  • 21. Dot Operator  Dot operator is used to access methods and variables within classes.  It is represented as . (dot) Example class Test { public int a; public display() { System.out.println(“Hello”); } } public static void main(String args[]) { Test t=new Test(); t.a=32; //accessing dot t.display(); //accessing display } }