Introduction to Java Variables and Classes. Primitive and Non Primitive Variables. Boolean, char, Short, Int, Long, Float, and double data type. Definition of Class and Objects with Examples. Various components of Class and Constructors
2. ❖ Container to hold value. Assigned with data type
❖ Reserved Area allocated in memory
Int x = 10;
RAM
Java Variable
Reserved place for
variable X to store
value
3. Java Example
public class SumofNumbers
{
public static void main(String args[])
{
int i = 20, j = 15, sum;
sum = i + j;
System.out.println("The sum of numbers is: "+sum);
}
}
I, J, sum are variables
which can hold value
and of type Integer
4. Java Primitive Data type
❖ Specifies the container size and characteristics to store the values
❖ Primitive Data type - Basic entity to store the data
❖ Primitive Data type includes
➢ Boolean
➢ Char
➢ Byte, Short, Int, Long
➢ Float & Double
5. Java Primitive Data type
Data Type Default value Default Size Example
boolean false 1 bit boolean a = false;
char 'u0000' 2 byte
'u0000' (or 0) to 'uffff' (or 65,535)
char c = 'c';
byte 0 1 byte (-128 to 127) byte num1=127;
short 0 2 byte (-32,768 to 32,767) short s = 10000;
int 0 4 byte (2,147,483,648 (-2^31) to
2,147,483,647 (2^31 -1))
int i = 100000;
long 0L 8 byte (-9,223,372,036,854,775,808(-
2^63) to 9,223,372,036,854,775,807(2^63 -
1))
long l = 100000L;
float 0.0f 4 byte (unlimited) float f1 = 234.5f;
double 0.0d 8 byte (unlimited) double d1 = 12.3
6. Java Non Primitive Data type
❖ Used to store a group of values
❖ These Data types are not Pre defined in JAVA
❖ Few Examples are
➢ Class
➢ Array
➢ String
➢ Interface
7. Object
❖ Object have
➢ State : data of an object
➢ Behaviour : functionally of an object
➢ Identity : unique id used by JVM
❖ Object is an instance of a class
❖ E.g., Xiaomi Redmi 10 is an Object which have
➢ Size : 6.5 Inches (state)
➢ Ram memory : 4GB and 6GB (state)
➢ Warp Charging (behaviour)
➢ Dual standby (behaviour)
8. Class
❖ Group of Object which have common properties
❖ Boilerplate or Template or Blueprint for objects to get created
State and Behaviour Xiaomi Redmi 10 ABC mobile model
Ram 6GB and 4GB 2GB
Size 6.5 inches 5 inches
Standby Dual standby Single Standby
GPU Mali-G52 MC2 -
9. Class Components
❖ Variable : Reserved Memory location to hold values
❖ Constructor
➢ Special method and Used to initialize an object.
➢ If Constructor is not declared, then Java compiler will create default constructor
❖ Methods
➢ Block of code where parameters can be passed.
➢ Only runs when it is called. These are called Functions
➢ It represents the Behavior of an object and it supports code reusability and Optimization
10. public class Phones {
int Ram;
float size;
String Name, GPU;
static String type = "Android";
Phones(String N, int R, float S, String G) {
Ram = R;
size = S;
Name = N;
GPU = G;
}
void knowmyphone() {
String sim = "Dual Sim";
System.out.println("Phone Name :"+Name);
System.out.println("Operating System :"+type);
System.out.println("Sim card slot :"+sim);
System.out.println("Size in inches : "+size);
System.out.println("Ram memory : "+Ram);
System.out.println("GPU value : "+GPU);
}
public static void main(String args[]) {
Phones p1 = new Phones("Redmi Note 10",6,6.5f,"");
p1.knowmyphone();
Phones p2 = new Phones("OnePlus 9
pro",12,6.7f,"Adreno 660");
p2.knowmyphone();
}
//Instance Variable
//Static Variable
//local Variable
11. Variable Type
❖ Local Variable : Only within a Method. Other methods or outside the
method doesn’t know about the variable
❖ Instance Variable : Inside the class but outside the method. It is instance
specific i.e., Different Objects have Different values if it is not declared
❖ Static Variable : Single copy of variable. Memory is allocated when the
class is loaded. i.e, same value for all objects.
Editor's Notes
Two data type - Primitive and Non Primitive.
Byte - handle the stream of data from a network or file
Short - to save memory as int is the default data type for numbers
Float - If you want to save memory use float instead of double
Float and double data types should never be used for precise values such as currency
Classes and Objects are basic components of Object Oriented programming
Object is the physical and logical entity whereas class is an logical entity only
I.e, Object is a real world entity
A constructor must be the same name of a Java class. It does not have a return type.