SlideShare a Scribd company logo
Java - Basic Datatypes
P NAGARAJU
Assistant Professor
Department of CSE
INTRODUCTION
 Variables are nothing but reserved memory locations to store values.
 Based on the data type of a variable, the operating system allocates memory and
decides what can be stored in the reserved memory.
There are two data types available in Java:
1. Primitive Data Types
2. Reference/Object Data Types
Primitive Data Types:
 Primitive data types are predefined by the language and named by a keyword.
 There are eight primitive data types supported by Java.
byte:
• Byte data type is an 8-bit signed two's complement integer.
• Minimum value is -128 (-2^7)
• Maximum value is 127 (inclusive)(2^7 -1)
• Default value is 0
• Byte data type is used to save space in large arrays, mainly in place of integers,
since a byte is four times smaller than an int.
• Example: byte a = 100 , byte b = -50
short:
• Short data type is a 16-bit signed two's complement integer.
• Minimum value is -32,768 (-2^15)
• Maximum value is 32,767 (inclusive) (2^15 -1)
• Short data type can also be used to save memory as byte data type. A short is 2
times smaller than an int
• Default value is 0.
• Example: short s = 10000, short r = -20000
int:
 Int data type is a 32-bit signed two's complement integer.
 Minimum value is - 2,147,483,648.(-2^31)
 Maximum value is 2,147,483,647(inclusive).(2^31 -1)
 Int is generally used as the default data type for integral values unless there is a
concern about memory.
 The default value is 0.
 Example: int a = 100000, int b = -200000
long:
 Long data type is a 64-bit signed two's complement integer.
 Minimum value is -9,223,372,036,854,775,808.(-2^63)
 Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
 This type is used when a wider range than int is needed.
 Default value is 0L.
 Example: long a = 100000L, long b = -200000L
float:
 Float data type is a single-precision 32-bit IEEE 754 floating point.
 Float is mainly used to save memory in large arrays of floating point numbers.
 Default value is 0.0f.
 Float data type is never used for precise values such as currency.
 Example: float f1 = 234.5f
double:
 double data type is a double-precision 64-bit IEEE 754 floating point.
 This data type is generally used as the default data type for decimal values,
generally the default choice.
 Double data type should never be used for precise values such as currency.
 Default value is 0.0d.
 Example: double d1 = 123.4
boolean:
 boolean data type represents one bit of information.
 There are only two possible values: true and false.
 This data type is used for simple flags that track true/false conditions.
 Default value is false.
 Example: boolean one = true
char:
 char data type is a single 16-bit Unicode character.
 Minimum value is 'u0000' (or 0).
 Maximum value is 'uffff' (or 65,535 inclusive).
 Char data type is used to store any character.
 Example: char letterA ='A'
Reference Data Types:
 Reference variables are created using defined constructors of the classes. They are
used to access objects. These variables are declared to be of a specific type that
cannot be changed. For example, Employee, Puppy etc.
 Class objects, and various type of array variables come under reference data type.
 Default value of any reference variable is null.
 A reference variable can be used to refer to any object of the declared type or any
compatible type.
 Example: Animal animal = new Animal("giraffe");
Java Scanner class
 There are various ways to read input from the keyboard, the java.util.Scanner
class is one of them.
 The Java Scanner class breaks the input into tokens using a delimiter that is
whitespace by default. It provides many methods to read and parse various
primitive values.
 Java Scanner class is widely used to parse text for string and primitive types using
regular expression.
 Java Scanner class extends Object class and implements Iterator and Closeable
interfaces.
Cont….
Method Description
public String next()
it returns the next token from the
scanner.
public String nextLine()
it moves the scanner position to the next
line and returns the value as a string.
public byte nextByte() it scans the next token as a byte.
public short nextShort() it scans the next token as a short value.
public int nextInt() it scans the next token as an int value.
public long nextLong() it scans the next token as a long value.
public float nextFloat() it scans the next token as a float value.
public double nextDouble() it scans the next token as a double value.
Java Scanner Example to get input from console
import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter your rollno");
int rollno=sc.nextInt();
System.out.println("Enter your name");
String name=sc.next();
System.out.println("Enter your fee");
double fee=sc.nextDouble();
System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);
sc.close();
}
}

More Related Content

PPTX
Computer programming 2 Lesson 5
MLG College of Learning, Inc
 
PPSX
Java session3
Jigarthacker
 
PPTX
5. variables & data types
M H Buddhika Ariyaratne
 
PPTX
DATA TYPES in Python done by Sanajai of MCA
JPrince9
 
PDF
Java basic datatypes
Soba Arjun
 
PPTX
Java data types
AbhishekMondal42
 
PPTX
Data types
nehajhala4
 
PPTX
This is a python data types ppt it is used for bca student also
sandhiyamaliga2005
 
Computer programming 2 Lesson 5
MLG College of Learning, Inc
 
Java session3
Jigarthacker
 
5. variables & data types
M H Buddhika Ariyaratne
 
DATA TYPES in Python done by Sanajai of MCA
JPrince9
 
Java basic datatypes
Soba Arjun
 
Java data types
AbhishekMondal42
 
Data types
nehajhala4
 
This is a python data types ppt it is used for bca student also
sandhiyamaliga2005
 

Similar to Java - Basic Datatypes.pptx (20)

PPTX
a variable in Java must be a specified data type
Kavitha S
 
DOCX
data types in java will help to clear the base
ashwinibhosale27
 
PPTX
Advanced java programming - DATA TYPES, VARIABLES, ARRAYS
vijayalakshmis184431
 
PPTX
Computer Programming Java Data Types.pptx
MarianneIsid
 
PPTX
Java_Access_Specifiers_and_Data_Types.pptx
Satyanandaram Nandigam
 
PPTX
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
PPTX
JAVA LESSON-01.pptx
StephenOczon1
 
PPTX
Topic-5_-Programming-Fundamentals-Part-2.pptx
HaroldOmega1
 
PPTX
L2 datatypes and variables
teach4uin
 
PDF
Java basic datatypes
Soba Arjun
 
PPTX
DATATYPES IN JAVA primitive and nonprimitive.pptx
mustaq4
 
PPTX
Java Data Types
Spotle.ai
 
PPTX
Datatype introduction- JAVA
Hamna_sheikh
 
PPTX
java programming basics - part ii
jyoti_lakhani
 
PDF
Java data types, variables and jvm
Madishetty Prathibha
 
PPTX
Data types IN JAVA
garishma bhatia
 
PPTX
Lect5
Jamsher bhanbhro
 
PPTX
Java fundamentals
Jayfee Ramos
 
PPS
Data types ,variables,array
Gujarat Technological University
 
PPT
Java SpringMVC SpringBOOT (Divergent).ppt
Aayush Chimaniya
 
a variable in Java must be a specified data type
Kavitha S
 
data types in java will help to clear the base
ashwinibhosale27
 
Advanced java programming - DATA TYPES, VARIABLES, ARRAYS
vijayalakshmis184431
 
Computer Programming Java Data Types.pptx
MarianneIsid
 
Java_Access_Specifiers_and_Data_Types.pptx
Satyanandaram Nandigam
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
JAVA LESSON-01.pptx
StephenOczon1
 
Topic-5_-Programming-Fundamentals-Part-2.pptx
HaroldOmega1
 
L2 datatypes and variables
teach4uin
 
Java basic datatypes
Soba Arjun
 
DATATYPES IN JAVA primitive and nonprimitive.pptx
mustaq4
 
Java Data Types
Spotle.ai
 
Datatype introduction- JAVA
Hamna_sheikh
 
java programming basics - part ii
jyoti_lakhani
 
Java data types, variables and jvm
Madishetty Prathibha
 
Data types IN JAVA
garishma bhatia
 
Java fundamentals
Jayfee Ramos
 
Data types ,variables,array
Gujarat Technological University
 
Java SpringMVC SpringBOOT (Divergent).ppt
Aayush Chimaniya
 
Ad

Recently uploaded (20)

PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
Zero Carbon Building Performance standard
BassemOsman1
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Ad

Java - Basic Datatypes.pptx

  • 1. Java - Basic Datatypes P NAGARAJU Assistant Professor Department of CSE
  • 2. INTRODUCTION  Variables are nothing but reserved memory locations to store values.  Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. There are two data types available in Java: 1. Primitive Data Types 2. Reference/Object Data Types Primitive Data Types:  Primitive data types are predefined by the language and named by a keyword.  There are eight primitive data types supported by Java.
  • 3. byte: • Byte data type is an 8-bit signed two's complement integer. • Minimum value is -128 (-2^7) • Maximum value is 127 (inclusive)(2^7 -1) • Default value is 0 • Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int. • Example: byte a = 100 , byte b = -50 short: • Short data type is a 16-bit signed two's complement integer. • Minimum value is -32,768 (-2^15) • Maximum value is 32,767 (inclusive) (2^15 -1) • Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int • Default value is 0. • Example: short s = 10000, short r = -20000
  • 4. int:  Int data type is a 32-bit signed two's complement integer.  Minimum value is - 2,147,483,648.(-2^31)  Maximum value is 2,147,483,647(inclusive).(2^31 -1)  Int is generally used as the default data type for integral values unless there is a concern about memory.  The default value is 0.  Example: int a = 100000, int b = -200000 long:  Long data type is a 64-bit signed two's complement integer.  Minimum value is -9,223,372,036,854,775,808.(-2^63)  Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)  This type is used when a wider range than int is needed.  Default value is 0L.  Example: long a = 100000L, long b = -200000L
  • 5. float:  Float data type is a single-precision 32-bit IEEE 754 floating point.  Float is mainly used to save memory in large arrays of floating point numbers.  Default value is 0.0f.  Float data type is never used for precise values such as currency.  Example: float f1 = 234.5f double:  double data type is a double-precision 64-bit IEEE 754 floating point.  This data type is generally used as the default data type for decimal values, generally the default choice.  Double data type should never be used for precise values such as currency.  Default value is 0.0d.  Example: double d1 = 123.4
  • 6. boolean:  boolean data type represents one bit of information.  There are only two possible values: true and false.  This data type is used for simple flags that track true/false conditions.  Default value is false.  Example: boolean one = true char:  char data type is a single 16-bit Unicode character.  Minimum value is 'u0000' (or 0).  Maximum value is 'uffff' (or 65,535 inclusive).  Char data type is used to store any character.  Example: char letterA ='A'
  • 7. Reference Data Types:  Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy etc.  Class objects, and various type of array variables come under reference data type.  Default value of any reference variable is null.  A reference variable can be used to refer to any object of the declared type or any compatible type.  Example: Animal animal = new Animal("giraffe");
  • 8. Java Scanner class  There are various ways to read input from the keyboard, the java.util.Scanner class is one of them.  The Java Scanner class breaks the input into tokens using a delimiter that is whitespace by default. It provides many methods to read and parse various primitive values.  Java Scanner class is widely used to parse text for string and primitive types using regular expression.  Java Scanner class extends Object class and implements Iterator and Closeable interfaces.
  • 9. Cont…. Method Description public String next() it returns the next token from the scanner. public String nextLine() it moves the scanner position to the next line and returns the value as a string. public byte nextByte() it scans the next token as a byte. public short nextShort() it scans the next token as a short value. public int nextInt() it scans the next token as an int value. public long nextLong() it scans the next token as a long value. public float nextFloat() it scans the next token as a float value. public double nextDouble() it scans the next token as a double value.
  • 10. Java Scanner Example to get input from console import java.util.Scanner; class ScannerTest{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Enter your rollno"); int rollno=sc.nextInt(); System.out.println("Enter your name"); String name=sc.next(); System.out.println("Enter your fee"); double fee=sc.nextDouble(); System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee); sc.close(); } }