You are on page 1of 26

IS 2104 - Rapid Application Development

Java Variables and Data Types


kav@ucsc.cmb.ac.lk
Slides adapted from Mr. Shavindra Wickramathilaka
1
ILOs
▷ Define “What is a variable in Java?”
▷ Describe Types of variables
▷ Describe Data types
▷ Compare and contrast Data types
▷ Justify the use of a data type in a certain scenario
What is a Variable?
▷ A variable can be thought of as a container which holds value during the life of a Java
program
▷ Every variable is assigned a data type which designates the type and quantity of value
it can hold
▷ In order to use a variable in a program you to need to perform 2 steps
○ Variable Declaration
○ Variable Initialization
Variable Declaration:
To declare a variable, you must specify the data type & give the variable a unique name.

int a,b,c;

float pi;

double d;

char a;
Variable Initialization:
To initialize a variable, you must assign it a valid value.

pi =3.14f;

do =20.22d;

a=’v’;
Combine Variable Declaration
and Initialization:
▷ You can combine variable declaration and initialization
▷ Examples :
○ int a=2,b=4,c=6;
○ float pi=3.14f;
○ double do=20.22d;
○ char a=’v’;
Types of variables
Local Variables
▷ Local Variables are a variable that are declared inside the body of a method.

Instance Variables
▷ Instance variables are defined without the STATIC keyword .
▷ They are defined Outside a method declaration.
▷ They are Object specific and are known as instance variables.

Static Variables
▷ Static variables are initialized only once, at the start of the program execution.
▷ These variables should be initialized first, before the initialization of any instance
variables.
class VariableTypes {
static int a = 1; //static variable
int data = 99; //instance variable

void method() {
int b = 90; //local variable
}
}
Data Types in Java
▷ Data types classify the different values to be stored in the variable.
▷ In Java, there are two categories of data types:
1. Primitive Data Types
2. Non-primitive Data Types
Primitive Data Types
▷ Primitive data types are predefined and available within the Java language.
▷ Primitive values do not share state with other primitive values.
▷ There are 8 primitive types:
○ byte
○ short
○ int
○ long
○ char
○ float
○ double
○ boolean

https://www.tutorialspoint.com/java/java_basic_datatypes.htm
https://www.tutorialspoint.com/java/java_variable_types.htm
Primitive Data Types
Primitive Data Types
▷ All numeric data types are signed (+/-)
▷ The size of data types remain the same on all platforms (standardized)
▷ Char data type in Java is 2 bytes because it uses UNICODE character set
▷ Java supports internationalization. UNICODE is a character set which covers all
known scripts and language in the world
Java Variable Type Conversion &
Type Casting
▷ A variable of one type can receive the value of another type.
▷ Case 1
○ Variable of smaller capacity is be assigned to another variable of bigger capacity.
○ This process is Automatic, and non-explicit. Known as Conversion
▷ Case 2
○ Variable of larger capacity is be assigned to another variable of smaller capacity
○ You have to explicitly specify the type cast operator. This process is known as
Type Casting.
○ In case, you do not specify a type cast operator, the compiler gives an error.
○ Since this rule is enforced by the compiler, it makes the programmer aware that
the conversion he is about to do may cause some loss in data and prevents
accidental losses.
Java Variable Type Conversion &
Type Casting
System.out.println("int converted to byte");
class Demo {
x = (byte) a;
public static void main(String args[]) {
System.out.println("a and x " + a + " " + x);
System.out.println("double converted to int");
byte x;
a = (int) b;
int a = 270;
System.out.println("b and a " + b + " " + a);
double b = 128.128;
System.out.println("\ndouble converted to
byte");
x = (byte)b;
System.out.println("b and x " + b + " " + x);
}
}
Non-primitive data types in Java
There are 5 types of non–primitive data types in Java
1. Class
2. Object
3. String
4. Array
5. Interface
Class
▷ A “blueprint” or creating objects
▷ Uses keyword class

Eg

public class Main {


int x = 5;
}

https://www.w3schools.com/java/java_classes.asp
Object
▷ An instance of a class
▷ Uses keyword new

Eg
public class Main {
int x = 5;

public static void main(String[] args) {


Main myObj = new Main();
System.out.println(myObj.x);
}
}
String
▷ A collection of characters
▷ Uses keyword String

Eg-
String university= "UCSC";

https://www.w3schools.com/java/java_strings.asp
String Methods
▷ concat()
▷ contains()
▷ equals()
▷ indexOf()
▷ length()
▷ replace()
▷ split()
▷ toString()
▷ toLowerCase() / toUpperCase()
▷ trim()

https://www.w3schools.com/java/java_ref_string.asp
Arrays
▷ Hold multiple values
▷ Uses square brackets [] to define

Eg

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


int[] myNum = {10, 20, 30, 40};

https://www.w3schools.com/java/java_arrays.asp
Arrays cont.
▷ Accessing elements
○ cars[0]
○ myNum[2]
▷ Changing an element
○ cars[0]=”Tesla”

https://www.w3schools.com/java/java_arrays.asp
Interface
▷ A blueprint of a class
▷ A mechanism in Java to achieve abstraction
▷ There can be only abstract methods in Java interface
▷ Supports multiple inheritance
▷ Uses keyword interface

https://www.javatpoint.com/interface-in-java
Interface
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}

public static void main(String args[]){


A6 obj = new A6();
obj.print();
}
}
Home Work
▷ Compare and Contrast Class, Abstract Class and Interface

Thank You

You might also like