OOP Unit 2 Notes
OOP Unit 2 Notes
Variable
A variable is the name of a reserved area allocated in memory. In other words, it is a name of the
memory location. It is a combination of "vary + able" which means its value can be changed.
A variable is a container which holds the value while the Java program is executed. A variable is
assigned with a data type.
Types of Variables
There are three types of variables in Java:
o local variable
o instance variable
o static variable
1) Local Variable
A variable declared inside the body of the method is called local variable. You can use this
variable only within that method and the other methods in the class aren't even aware that the
variable exists.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an instance
variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared among
instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You can create a
single copy of the static variable and share it among all the instances of the class. Memory
allocation for static variables happens only once when the class is loaded in the memory.
Example to understand the types of variables in java
public class A
{
static int m=500;//static variable
void method()
{
int n=10;//local variable
}
public static void main(String args[])
{
int data=40;//instance variable
}
}//end of class
If any variable is not assigned with value at compile-time and assigned at run time is called
dynamic initialization of a variable. Basically, this is achieved through constructors, setter
methods, normal methods and builtin api methods which returns a value or object.
Instance Variables
A variable which is declared inside a class and outside all the methods and blocks is an instance
variable. The general scope of an instance variable is throughout the class except in static
methods. The lifetime of an instance variable is until the object stays in memory.
Class Variables
A variable which is declared inside a class, outside all the blocks and is marked static is known
as a class variable. The general scope of a class variable is throughout the class and the lifetime
of a class variable is until the end of the program or as long as the class is loaded in memory.
Local Variables
All other variables which are not instance and class variables are treated as local variables
including the parameters in a method. Scope of a local variable is within the block in which it is
declared and the lifetime of a local variable is until the control leaves the block in which it is
declared.
1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
Boolean Data Type
The Boolean data type is used to store only two possible values: true and false. This data type is
used for simple flags that track true/false conditions.
The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.
Example:
The byte data type is an example of primitive data type. It isan 8-bit signed two's complement
integer. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and
maximum value is 127. Its default value is 0.
The byte data type is used to save memory in large arrays where the memory savings is most
required. It saves space because a byte is 4 times smaller than an integer. It can also be used in
place of "int" data type.
Example:
The short data type can also be used to save memory just like byte data type. A short data type is
2 times smaller than an integer.
Example:
The int data type is generally used as a default data type for integral values unless if there is no
problem about memory.
Example:
Example:
The float data type is a single-precision 32-bit IEEE 754 floating point.Its value range is
unlimited. It is recommended to use a float (instead of double) if you need to save memory in
large arrays of floating point numbers. The float data type should never be used for precise
values, such as currency. Its default value is 0.0F.
Example: float f1 = 234.5f
Example:
double d1 = 12.3
Example:
Type casting
Convert a value from one data type to another data type is known as type casting.
byte -> short -> char -> int -> long -> float -> double
For example, the conversion between numeric data type to char or Boolean is not done
automatically. Also, the char and Boolean data types are not compatible with each other. Let's
see an example.
WideningTypeCastingExample.java
double -> float -> long -> int -> char -> short -> byte
In the following example, we have performed the narrowing type casting two times. First, we
have converted the double type into long data type after that long data type is converted into int
type.
NarrowingTypeCastingExample.java
There are many types of operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Assignment Operator.
o negating an expression
The bitwise & operator always checks both conditions whether first condition is true or false.
The logical || operator doesn't check the second condition if the first condition is true. It checks
the second condition only if the first one is false.
The bitwise | operator always checks both conditions whether first condition is true or false.