SlideShare a Scribd company logo
Data types and VariablesData types and Variables
By
Nilesh Dalvi
Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College.
http://www.slideshare.net/nileshdalvi01
Java and Data StructuresJava and Data Structures
Data Types
A data type in a programming language is a set of data
with values having pre-defined characteristics.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Data Types
Data Type Default Value Default size
boolean false 1 bit
char 'u0000‘ or 0 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Demonstrating double data type
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Area
{
public static void main(String args [])
{
double pi, r, a;
r = 10.8; // radius of a circle
pi = 3.146; // value of pi
a = pi*r*r; // compute area
System.out.println("Area of circle" +a);
}
}
Demonstrating char data type
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class CharDemo
{
public static void main(String args [])
{
char ch = 'x';
System.out.println("Character is" +ch);
ch ++; // increment ch;
System.out.println("Character is" +ch);
}
}
Variables:
• Declaring variables:
type identifier = value;
• Example:
• Types of variables:
There are three types of variables in java
– local variable
– instance variable
– static variable
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
int a, b, c; // Demonstrating three integers
int d = 3, e, f = 5;
byte z = 22;
double pi = 3.1416;
char x = ‘x’;
Variables:
Dynamic initialization:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class DynInit {
public static void main(String args []){
double a = 3.0, b = 4.0;
//c is dynamically initialized
double c = Math.sqrt(a*a+b*b);
System.out.println("Hypotenuse is" +c);
}
}
Local variable:
• Declared in methods, constructors, or blocks.
• Local variables are created when the method, constructor or
block is entered and the variable will be destroyed once it exits
the method, constructor or block.
• Access modifiers cannot be used for local variables.
• Local variables are visible only within the declared method,
constructor or block.
• There is no default value for local variables so local variables
should be declared and an initial value should be assigned
before the first use.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Local variable:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Test{
public void pupAge(){
int age;
age = age + 7;
System.out.println("Puppy age is : " + age);
}
public static void main(String args[]){
Test test = new Test();
test.pupAge();
}
}
Local variable:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Test{
public void pupAge(){
int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
}
public static void main(String args[]){
Test test = new Test();
test.pupAge();
}
}
Instance variable:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Declared in a class, but outside a method, constructor or any block.
• When a space is allocated for an object in the heap, a slot for each
instance variable value is created.
• Created when an object is created with the use of the keyword 'new' and
destroyed when the object is destroyed.
• Instance variables can be declared in class level.
• Access modifiers can be given for instance variables.
• They are visible for all methods, constructors and block in the class.
• Instance variables have default values.
• It can be accessed directly by calling the variable name inside the class.
ObjectReference.VariableName.
Instance variable:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Employee{
// this instance variable is visible for any child class.
public String name;
// salary variable is visible in Employee class only.
private double salary;
// The name variable is assigned in the constructor.
public Employee (String empName){
name = empName;
}
// The salary variable is assigned a value.
public void setSalary(double empSal){
salary = empSal;
}
// This method prints the employee details.
public void printEmp(){
System.out.println("name : " + name );
System.out.println("salary :" + salary);
}
public static void main(String args[]){
Employee empOne = new Employee("Ram");
empOne.setSalary(1000);
empOne.printEmp();
}
}
Class/ Static variable:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Declared with the static keyword in a class, but outside a
method, constructor or a block.
• There would only be one copy of each class variable per class,
regardless of how many objects are created from it.
• Static variables are created when the program starts and
destroyed when the program stops.
• Visibility is similar to instance variables.
• Default values are same as instance variables.
• Static variables can be accessed by calling with the class name
ClassName.variableName
Class/ Static variable:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Employee{
// salary variable is a private static variable
private static double salary;
// DEPARTMENT is a constant
public static final String DEPARTMENT = "Development ";
public static void main(String args[]){
salary = 1000;
System.out.println(DEPARTMENT+"average salary:"+salary);
}
}
Type Conversion and Casting
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Java’s Automatic Conversions(Implicit):
• When one type of data is assigned to another type of
variable, an automatic type conversion will take place if the
following conditions satisfied:
– Two types are compatible.
– Destination type is larger than the source type.
– e.g.
int a = byte b;
Type Conversion and Casting
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Casting Incompatible types(Explicit):
• To create a conversion between two incompatible types, you
must use a cast.
• A cast is simply an explicit type conversion.
• It has the general form:
– (target-type) value;
– target-type – specifies the desired type to convert the specified value
to.
– e.g. int a;
byte b;
// ...
b = (byte) a;
Demonstrating Casting
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Conversion
{
public static void main(String args[])
{
byte b;
int i = 257;
double d = 323.142;
System.out.println("nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}
Demonstrating Casting
(323)10 = (1 0 1 0 0 0 0 1 1 )2
= 0 x 27
+ 1 x 26 + 0 x 25+ 0 x 24+
0 x 23+ 0 x 22+ 1 x 21+ 1 x 20
= (67)10
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Output:
Conversion of int to byte.
i and b 257 -> 1
Conversion of double to int.
d and i 323.142 -> 323
Conversion of double to byte.
d and b 323.142 -> 67
2 323
2 161 1
2 80 1
2 40 0
2 20 0
2 10 0
2 5 0
2 2 1
2 1 0
2 0 1
Byte is 8-bit
Type Conversion and Casting
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Java type casting is classified in two types:
Arrays
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Q & A

More Related Content

PPTX
Manipulators in c++
PPTX
Function overloading and overriding
PPT
Savitch Ch 03
PDF
Comparison between runtime polymorphism and compile time polymorphism
PPTX
Pointers, virtual function and polymorphism
PPTX
Macro ( National income Accounting)
PDF
Managing I/O in c++
PPSX
Support for Object-Oriented Programming (OOP) in C++
Manipulators in c++
Function overloading and overriding
Savitch Ch 03
Comparison between runtime polymorphism and compile time polymorphism
Pointers, virtual function and polymorphism
Macro ( National income Accounting)
Managing I/O in c++
Support for Object-Oriented Programming (OOP) in C++

What's hot (17)

PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
Managing console input and output
PPTX
Control statements in c
PPTX
StringBuffer.pptx
PPT
Class & Object - User Defined Method
PPTX
Managing input and output operation in c
PPTX
Access Modifier.pptx
PPT
4 sector model of Macro Economic
PPTX
Inheritance in c++
PDF
Print function in PHP
PPTX
Pointers in c - Mohammad Salman
PPTX
Polymorphism in C++
PPT
Types of operators in C
PPTX
Std 12 computer chapter 6 object oriented concepts (part 1)
PDF
Constants Variables Datatypes by Mrs. Sowmya Jyothi
PPTX
Data Types In C
PPTX
Data members and member functions
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
Managing console input and output
Control statements in c
StringBuffer.pptx
Class & Object - User Defined Method
Managing input and output operation in c
Access Modifier.pptx
4 sector model of Macro Economic
Inheritance in c++
Print function in PHP
Pointers in c - Mohammad Salman
Polymorphism in C++
Types of operators in C
Std 12 computer chapter 6 object oriented concepts (part 1)
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Data Types In C
Data members and member functions
Ad

Viewers also liked (20)

PPT
9. Input Output in java
PPT
6. Exception Handling
PPT
8. String
PPT
7. Multithreading
PPT
5. Inheritances, Packages and Intefaces
PPT
4. Classes and Methods
PPT
14. Linked List
PPT
10. Introduction to Datastructure
PPT
11. Arrays
PPT
13. Queue
PPTX
Gracias por estar aqui
PPTX
Velvet freyre
PPTX
Gestor de proyecto diplomado
PPT
PPTX
PDF
Development of an imagery system for automatic classification of geological m...
DOCX
ciclonesTrabajo ciclos
PDF
Weblogs als Website
PPTX
Wi d vortrag screenshots
PDF
TDC2016SP - Crystal-lang. Tipo o Ruby, mas é C.
9. Input Output in java
6. Exception Handling
8. String
7. Multithreading
5. Inheritances, Packages and Intefaces
4. Classes and Methods
14. Linked List
10. Introduction to Datastructure
11. Arrays
13. Queue
Gracias por estar aqui
Velvet freyre
Gestor de proyecto diplomado
Development of an imagery system for automatic classification of geological m...
ciclonesTrabajo ciclos
Weblogs als Website
Wi d vortrag screenshots
TDC2016SP - Crystal-lang. Tipo o Ruby, mas é C.
Ad

Similar to 3. Data types and Variables (20)

PPT
Polymorphism
PPT
Java tutorial for Beginners and Entry Level
PPTX
Detailed_description_on_java_ppt_final.pptx
PPT
Corejava Training in Bangalore Tutorial
DOCX
Assignment 7
PPTX
Introduction to java programming
PPT
Inheritance : Extending Classes
PPTX
Class and Object.pptx from nit patna ece department
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
PDF
Pooja Sharma , BCA Third Year
PPTX
STATIC IN JAVA.pptx STATIC IN JaAVA.pptx
PDF
Java keywords
PDF
7. VARIABLEs presentation in java programming. Pdf
PPTX
Object oriented concepts
PDF
Keywords and classes
PPSX
Java session4
PPTX
OOP-java-variables.pptx
PPT
Best Core Java Training In Bangalore
PDF
OOPs & Inheritance Notes
PPTX
Identifiers, keywords and types
Polymorphism
Java tutorial for Beginners and Entry Level
Detailed_description_on_java_ppt_final.pptx
Corejava Training in Bangalore Tutorial
Assignment 7
Introduction to java programming
Inheritance : Extending Classes
Class and Object.pptx from nit patna ece department
Class and Object JAVA PROGRAMMING LANG .pdf
Pooja Sharma , BCA Third Year
STATIC IN JAVA.pptx STATIC IN JaAVA.pptx
Java keywords
7. VARIABLEs presentation in java programming. Pdf
Object oriented concepts
Keywords and classes
Java session4
OOP-java-variables.pptx
Best Core Java Training In Bangalore
OOPs & Inheritance Notes
Identifiers, keywords and types

More from Nilesh Dalvi (13)

PPT
12. Stack
PPT
2. Basics of Java
PPT
1. Overview of Java
PPT
Standard Template Library
PPT
Templates
PPT
File handling
PPT
Input and output in C++
PPT
Strings
PPT
Operator Overloading
PDF
Constructors and destructors
PDF
Classes and objects
PDF
Introduction to cpp
PDF
Introduction to oops concepts
12. Stack
2. Basics of Java
1. Overview of Java
Standard Template Library
Templates
File handling
Input and output in C++
Strings
Operator Overloading
Constructors and destructors
Classes and objects
Introduction to cpp
Introduction to oops concepts

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Pre independence Education in Inndia.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Classroom Observation Tools for Teachers
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
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
Insiders guide to clinical Medicine.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pre independence Education in Inndia.pdf
Week 4 Term 3 Study Techniques revisited.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Anesthesia in Laparoscopic Surgery in India
Classroom Observation Tools for Teachers
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Complications of Minimal Access Surgery at WLH
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial disease of the cardiovascular and lymphatic systems
Microbial diseases, their pathogenesis and prophylaxis
Basic Mud Logging Guide for educational purpose
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
102 student loan defaulters named and shamed – Is someone you know on the list?
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Insiders guide to clinical Medicine.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf

3. Data types and Variables

  • 1. Data types and VariablesData types and Variables By Nilesh Dalvi Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College. http://www.slideshare.net/nileshdalvi01 Java and Data StructuresJava and Data Structures
  • 2. Data Types A data type in a programming language is a set of data with values having pre-defined characteristics. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 3. Data Types Data Type Default Value Default size boolean false 1 bit char 'u0000‘ or 0 2 byte byte 0 1 byte short 0 2 byte int 0 4 byte long 0L 8 byte float 0.0f 4 byte double 0.0d 8 byte Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 4. Demonstrating double data type Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Area { public static void main(String args []) { double pi, r, a; r = 10.8; // radius of a circle pi = 3.146; // value of pi a = pi*r*r; // compute area System.out.println("Area of circle" +a); } }
  • 5. Demonstrating char data type Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class CharDemo { public static void main(String args []) { char ch = 'x'; System.out.println("Character is" +ch); ch ++; // increment ch; System.out.println("Character is" +ch); } }
  • 6. Variables: • Declaring variables: type identifier = value; • Example: • Types of variables: There are three types of variables in java – local variable – instance variable – static variable Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int a, b, c; // Demonstrating three integers int d = 3, e, f = 5; byte z = 22; double pi = 3.1416; char x = ‘x’;
  • 7. Variables: Dynamic initialization: Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class DynInit { public static void main(String args []){ double a = 3.0, b = 4.0; //c is dynamically initialized double c = Math.sqrt(a*a+b*b); System.out.println("Hypotenuse is" +c); } }
  • 8. Local variable: • Declared in methods, constructors, or blocks. • Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block. • Access modifiers cannot be used for local variables. • Local variables are visible only within the declared method, constructor or block. • There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 9. Local variable: Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Test{ public void pupAge(){ int age; age = age + 7; System.out.println("Puppy age is : " + age); } public static void main(String args[]){ Test test = new Test(); test.pupAge(); } }
  • 10. Local variable: Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Test{ public void pupAge(){ int age = 0; age = age + 7; System.out.println("Puppy age is : " + age); } public static void main(String args[]){ Test test = new Test(); test.pupAge(); } }
  • 11. Instance variable: Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Declared in a class, but outside a method, constructor or any block. • When a space is allocated for an object in the heap, a slot for each instance variable value is created. • Created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed. • Instance variables can be declared in class level. • Access modifiers can be given for instance variables. • They are visible for all methods, constructors and block in the class. • Instance variables have default values. • It can be accessed directly by calling the variable name inside the class. ObjectReference.VariableName.
  • 12. Instance variable: Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Employee{ // this instance variable is visible for any child class. public String name; // salary variable is visible in Employee class only. private double salary; // The name variable is assigned in the constructor. public Employee (String empName){ name = empName; } // The salary variable is assigned a value. public void setSalary(double empSal){ salary = empSal; } // This method prints the employee details. public void printEmp(){ System.out.println("name : " + name ); System.out.println("salary :" + salary); } public static void main(String args[]){ Employee empOne = new Employee("Ram"); empOne.setSalary(1000); empOne.printEmp(); } }
  • 13. Class/ Static variable: Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). • Declared with the static keyword in a class, but outside a method, constructor or a block. • There would only be one copy of each class variable per class, regardless of how many objects are created from it. • Static variables are created when the program starts and destroyed when the program stops. • Visibility is similar to instance variables. • Default values are same as instance variables. • Static variables can be accessed by calling with the class name ClassName.variableName
  • 14. Class/ Static variable: Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Employee{ // salary variable is a private static variable private static double salary; // DEPARTMENT is a constant public static final String DEPARTMENT = "Development "; public static void main(String args[]){ salary = 1000; System.out.println(DEPARTMENT+"average salary:"+salary); } }
  • 15. Type Conversion and Casting Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Java’s Automatic Conversions(Implicit): • When one type of data is assigned to another type of variable, an automatic type conversion will take place if the following conditions satisfied: – Two types are compatible. – Destination type is larger than the source type. – e.g. int a = byte b;
  • 16. Type Conversion and Casting Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Casting Incompatible types(Explicit): • To create a conversion between two incompatible types, you must use a cast. • A cast is simply an explicit type conversion. • It has the general form: – (target-type) value; – target-type – specifies the desired type to convert the specified value to. – e.g. int a; byte b; // ... b = (byte) a;
  • 17. Demonstrating Casting Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Conversion { public static void main(String args[]) { byte b; int i = 257; double d = 323.142; System.out.println("nConversion of int to byte."); b = (byte) i; System.out.println("i and b " + i + " " + b); System.out.println("nConversion of double to int."); i = (int) d; System.out.println("d and i " + d + " " + i); System.out.println("nConversion of double to byte."); b = (byte) d; System.out.println("d and b " + d + " " + b); } }
  • 18. Demonstrating Casting (323)10 = (1 0 1 0 0 0 0 1 1 )2 = 0 x 27 + 1 x 26 + 0 x 25+ 0 x 24+ 0 x 23+ 0 x 22+ 1 x 21+ 1 x 20 = (67)10 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Output: Conversion of int to byte. i and b 257 -> 1 Conversion of double to int. d and i 323.142 -> 323 Conversion of double to byte. d and b 323.142 -> 67 2 323 2 161 1 2 80 1 2 40 0 2 20 0 2 10 0 2 5 0 2 2 1 2 1 0 2 0 1 Byte is 8-bit
  • 19. Type Conversion and Casting Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Java type casting is classified in two types:
  • 21. Q & A