SlideShare a Scribd company logo
1 of 21
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

What's hot

What's hot (20)

07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programming
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
 
Break and continue
Break and continueBreak and continue
Break and continue
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
user defined function
user defined functionuser defined function
user defined function
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Data Types In C
Data Types In CData Types In C
Data Types In C
 
Interface in java
Interface in javaInterface in java
Interface in java
 
C function
C functionC function
C function
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Control statements
Control statementsControl statements
Control statements
 

Viewers also liked

9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception HandlingNilesh Dalvi
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and IntefacesNilesh Dalvi
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and MethodsNilesh Dalvi
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to DatastructureNilesh Dalvi
 
Gracias por estar aqui
Gracias por estar aquiGracias por estar aqui
Gracias por estar aquiAngela Muñoz
 
Gestor de proyecto diplomado
Gestor de proyecto diplomadoGestor de proyecto diplomado
Gestor de proyecto diplomadoRuby Perez
 
Development of an imagery system for automatic classification of geological m...
Development of an imagery system for automatic classification of geological m...Development of an imagery system for automatic classification of geological m...
Development of an imagery system for automatic classification of geological m...Aurélien Dor
 
ciclonesTrabajo ciclos
ciclonesTrabajo ciclosciclonesTrabajo ciclos
ciclonesTrabajo ciclosJorge Eduardo
 
Weblogs als Website
Weblogs als WebsiteWeblogs als Website
Weblogs als WebsiteKMTO
 
Wi d vortrag screenshots
Wi d vortrag screenshotsWi d vortrag screenshots
Wi d vortrag screenshotspaulamarlene
 
TDC2016SP - Crystal-lang. Tipo o Ruby, mas é C.
TDC2016SP - Crystal-lang. Tipo o Ruby, mas é C.TDC2016SP - Crystal-lang. Tipo o Ruby, mas é C.
TDC2016SP - Crystal-lang. Tipo o Ruby, mas é C.tdc-globalcode
 

Viewers also liked (20)

9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 
8. String
8. String8. String
8. String
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
 
14. Linked List
14. Linked List14. Linked List
14. Linked List
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to Datastructure
 
11. Arrays
11. Arrays11. Arrays
11. Arrays
 
13. Queue
13. Queue13. Queue
13. Queue
 
Gracias por estar aqui
Gracias por estar aquiGracias por estar aqui
Gracias por estar aqui
 
Velvet freyre
Velvet freyreVelvet freyre
Velvet freyre
 
Gestor de proyecto diplomado
Gestor de proyecto diplomadoGestor de proyecto diplomado
Gestor de proyecto diplomado
 
B&P
B&PB&P
B&P
 
Sil
SilSil
Sil
 
Development of an imagery system for automatic classification of geological m...
Development of an imagery system for automatic classification of geological m...Development of an imagery system for automatic classification of geological m...
Development of an imagery system for automatic classification of geological m...
 
ciclonesTrabajo ciclos
ciclonesTrabajo ciclosciclonesTrabajo ciclos
ciclonesTrabajo ciclos
 
Weblogs als Website
Weblogs als WebsiteWeblogs als Website
Weblogs als Website
 
Wi d vortrag screenshots
Wi d vortrag screenshotsWi d vortrag screenshots
Wi d vortrag screenshots
 
TDC2016SP - Crystal-lang. Tipo o Ruby, mas é C.
TDC2016SP - Crystal-lang. Tipo o Ruby, mas é C.TDC2016SP - Crystal-lang. Tipo o Ruby, mas é C.
TDC2016SP - Crystal-lang. Tipo o Ruby, mas é C.
 

Similar to 3. Data types and Variables

Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelRamrao Desai
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorialrajkamaltibacademy
 
Assignment 7
Assignment 7Assignment 7
Assignment 7IIUM
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programmingshinyduela
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh Dalvi
 
Pooja Sharma , BCA Third Year
Pooja Sharma , BCA Third YearPooja Sharma , BCA Third Year
Pooja Sharma , BCA Third YearDezyneecole
 
OOP-java-variables.pptx
OOP-java-variables.pptxOOP-java-variables.pptx
OOP-java-variables.pptxssuserb1a18d
 
Best Core Java Training In Bangalore
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalorerajkamaltibacademy
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and typesDaman Toor
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Conceptsmdfkhan625
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Sagar Verma
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
Unit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptxUnit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptxDrYogeshDeshmukh1
 

Similar to 3. Data types and Variables (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Pooja Sharma , BCA Third Year
Pooja Sharma , BCA Third YearPooja Sharma , BCA Third Year
Pooja Sharma , BCA Third Year
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Java session4
Java session4Java session4
Java session4
 
OOP-java-variables.pptx
OOP-java-variables.pptxOOP-java-variables.pptx
OOP-java-variables.pptx
 
Best Core Java Training In Bangalore
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalore
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Unit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptxUnit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptx
 

More from Nilesh Dalvi

1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of JavaNilesh Dalvi
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryNilesh Dalvi
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cppNilesh Dalvi
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops conceptsNilesh Dalvi
 

More from Nilesh Dalvi (12)

12. Stack
12. Stack12. Stack
12. Stack
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Templates
TemplatesTemplates
Templates
 
File handling
File handlingFile handling
File handling
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Strings
StringsStrings
Strings
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 

Recently uploaded

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 

Recently uploaded (20)

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 

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