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

3. Data types and Variables

  • 1.
    Data types andVariablesData 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 datatype 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 TypeDefault 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 datatype 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 datatype 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: typeidentifier = 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: • Declaredin 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: NileshDalvi, 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: NileshDalvi, 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 andCasting 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 andCasting 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 andCasting Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Java type casting is classified in two types:
  • 20.
  • 21.