3. Variables
A variable is a container that holds the value when the java program executed or we can say that a variable is
a name of memory location.
The value of a variable can be changed.
Syntax –
<data type> <variable name> = <value of variable>
In java, there are three types of variable:
Local Variable
Instance Variable
Static Variable
Local Variable – A variable declared inside methods, constructors, or blocks is called local variable. Few
important points of local variable are as follows:
We can not use access modifier for local variable.
4. Variables
We can not use access modifier for local variable.
The scope of local variable is within the method, constructor or block in which it is declared.
There is no default value for local variables, so local variable should be initialized at the declaration
time.
A local variable can not be defined with static keyword.
public class Demo {
public void sum () {
int x = 10, y=20, z=0;
z = x + y;
System.out.println(“Addition of x and y =” +z);
}
public static void main (String args []) {
Demo d = new Demo ();
d.sum ();
}
}
Output - Addition of x and y = 30
public class Demo {
public void sum () {
int x+5;
z = x + y;
System.out.println(“Addition of x and y =” +z);
}
public static void main (String args []) {
Demo d = new Demo ();
d.sum ();
} }
Output - variable number might not have been initialized x = x + 5;
^
1 error
5. Variables
Instance Variable – A variable declared outside the body of methods, constructors, or blocks but inside the
class is called instance variable. Access Modifiers can be used with instance variable.
class Test {
int x=10;//instance variable
void show () {
int y=20;//local variable
} }
Static Variable - A variable declared with static keyword is called static variable. Static variable can be shared
among all the instances of the class. Static variable cannot be local.
Ex. static int x = 10;
6. Variables
Naming Convention
It should start with a lowercase letter such as id, name.
It should not start with the special characters like & (ampersand), $ (dollar), _ (underscore).
If the name contains multiple words, start it with the lowercase letter followed by an uppercase letter such as
firstName, lastName.
Avoid using one-character variables such as x, y, z.
Ex.
class Employee {
int id;
//code snippet
}
7. Data Type
Data type means that define the data or variables.
Based on the data type of a variable, the operating system allocates memory for a variable and decides what
can be stored in the memory.
There are two types of data type in java as follows:
Reference Data Types
Primitive Data Types
Reference Data Type - The reference data types include Classes, Interfaces and Arrays. The default value of
any reference data type in null. Reference variables are created by using constructors of the classes. Reference
variables are used to access objects. Ex.
Demo d = new Demo ();
Here Demo is a class and d is a reference variable.
8. Data Type
Data Type Default Value Default size Description
boolean false 1 bit for simple flags that track true/false conditions. Default value is false.
char 'u0000' 2 byte used to store any character
byte 0 1 byte used to save space in large arrays, mainly in place of integers, since a byte is four
times smaller than an integer. Default value is 0
short 0 2 byte used to save memory as byte data type. A short is 2 times smaller than an integer.
Default value is 0
int 0 4 byte used as the default data type for integral values unless there is a concern about
memory. The default value is 0
long 0L 8 byte used when a wider range than int is needed. Default value is 0L
float 0.0f 4 byte mainly used to save memory in large arrays of floating-point numbers. Default value
is 0.0f
double 0.0d 8 byte used as the default data type for decimal values, generally the default choice.
Default value is 0.0d
Primitive Data Types:
9. Operators
Operator is a symbol that is used to operate on variables to perform some operations. There are following types
of operator in java:
Unary Operator
It operates on only one variable. Unary operators are performed following operation:
• Increment Operator (++)
• Decrement Operator (--)
class Test {
public static void main (String args []) {
int x = 20;
System.out.pritln(x++);
System.out.pritln(++x);
System.out.pritln(x--);
System.out.pritln(--x);
} }
Output – 20
22
22
20
10. Operators
Arithmetic Operator
This operator is used to operate on two variables. (+, -, *, /, %). % - Return remainder value.
class Demo {
public static void main (String args []) {
int x = 20, y = 10;
int z = x + y;
System.out.pritln(z);
}
}
Output – 30
11. Operators
Shift Operator
This operator is used to shift all the bits in a value to the left or right side of a specified number of times. Shift
Operator are two types: Left shift (<<) and Right Shift Operator (>>).
class LeftShift {
public static void main (String args []) {
System.out.println (10<<2); //10*2^2=10*4=40
System.out.println (10<<3); //10*2^3=10*8=80
}
}
Output – 40
80
class RightShift {
public static void main (String args []) {
System.out.println (10>>2); //10 / 2^2=10 / 4=2
System.out.println (20>>3); //20 / 2^3=20 / 8=2
System.out.println (80>>4); //80 / 2^4=80 / 16=5
}}
Output – 2
2
5
12. Operators
Relational Operator
Operator Description Example (a=10, b=20)
equal to (==) Checks that the value of two variables are equal or not. Return true if
equal otherwise false.
(a == b) is false
not equal to (!=) Checks that the value of two variables are equal or not. Return true if
equal otherwise false.
(a != b) is true
greater than (>) Checks that the value of left variable is greater than the right one. If
yes then true.
(a>b) is not true
less than (<) Checks that the value of left variable is smaller than the right one. If
yes then true.
(a<b) is true
Greater than or equal
to (>=)
Checks that the value of left variable is greater than or equal to the
right one. If yes then true.
(a >= b) is not true
less than or equal to
(<=)
Checks that the value of left variable is smaller than or equal to the
right one. If yes then true.
(a <= b) is true
13. Operators
Logical Operator
Operator Description
Example (a is true & b is
false)
&&
Called logical AND operator. If the value of both variables is
non-zero, then condition is true.
(a && b) is false
||
Called logical OR operator. If the value of any one variable is
non-zero, then condition is true.
(a || b) is true
!
Called logical NOT operator. Use to reverse the variable’s
logical state. If a condition is false then logical NOT operator
will make true.
! (a && b) is true
14. Operators
Assignment Operator
Operator Example
=
z = x + y, Here the value of “x + y” assign into z.
+=
x += y means that x = x + y
-=
x -= y means that x = x - y
*=
x *= y means that x = x * y
/=
x /= y means that x = x / y
%=
x %= y means that x = x % y, etc.
15. Operators
Ternary Operator
It is also known as Conditional Operator. This operator consists of three variables and is used to evaluate
boolean expressions.
Syntax – <Data Type> <Variable Name> = <Expression>? <Value if True>:<Value if False>
class Test {
public static void main (String args []) {
int a=2;
int b=5;
int min=(a<b)? a: b;
System.out.println(min);
}
}
Output - 2
16. Keywords
Keywords are also known as reserved words. These are predefined words by java so it can not be used as a
variable or object name. There are many keywords are used in java. Here we discussed few keywords:
this keyword - In java, this is a reference variable that refers to the current class object. In other words, we can
say that this keyword is used to differentiate local and instance variable when both the variable name is same.
Uses of this keyword
• this keyword is used to call current class method.
• this () is used to invoke current class constructor.
• this can be passed as an argument in method and constructor call.
17. Keywords
abstract - It is used to declare an abstract class. Abstract class can have abstract and non-abstract method.
catch – In java catch keyword is used catch the exception generated by try statements.
class – Java class keyword is used to declare a class.
extends – This keyword indicate that a class is derived from another class or interface.
final – Java final keyword is used to make a constant.
import – This keyword is used to makes classes and interfaces accessible to current class code.
abstract, boolean, break, byte, case, class, char, catch, default, continue, do,
double, else, enum, extends, final, finally, if, for, implements, import,
instanceof, int, interface, long, native, null, new, package, public, protected,
private, return, short, static, strictfp, super, switch, synchronized, this, throw,
throws, try, transient, void, while, volatile
18. OOPs Concept
OOPs stands for Object-Oriented Programming System.
As a language that has the Object-Oriented feature, Java supports the following fundamental concepts:
Object
Class
Abstraction
Inheritance
Encapsulation
Polymorphism
The mail goal of Object-Oriented Programming is to implement real-world entities, such as classes, object,
etc.
Java, Python, C#, etc. are the example of object-oriented programming.
19. OOPs Concept
Object
Everything in the world that has state and behavior is an object such as human has states – name, color and
behavior – performing some action.
An object can also be defined as “an instance of a class”.
There are two ways to create an object :
1st Method
Test t = new Test();
Here Test is a class, t is reference variable that is assigned by object (new Test()) and Test() is a constructor. In
this method declaration of reference variable and initialization both happened together.
2nd Method
Test t;
t = new Test();
In this method first declare reference variable after that initialize it.
20. OOPs Concept
Class
A class is a keyword that are used to create a class in java.
Class can also be defined as a collection of similar type of objects.
Class is a blueprint/template that describes the state and behavior of an object.
Ex.
class Test {
// Statements;
}
Abstraction
Showing the functionality and hiding the internal details is called Abstraction. To achieve abstraction in java
through Abstract class and Interfaces.
E.g. Send Email – We don’t know the internal details after sending mail.
21. OOPs Concept
Inheritance
To inherit the common properties from base class to subclass is called inheritance.
The class which inherits the properties of other class is known as sub class (derived class, child class) and the
class whose properties are inherited is called base class (super class, parent class).
The extends is used to inherit the properties of the base class.
Syntax –
class Test {
// properties;
}
class Test1 extends Test {
………………
………………
}
22. OOPs Concept
Types of Inheritance
There are five types of inheritance but in java only three are supported. Here we discussed all types of
inheritance as follows:
1. Single Inheritance - In this type of inheritance, one parent and one child class exist.
23. OOPs Concept
2. Multilevel Inheritance - In this type of inheritance a generation concept involves such as grandparent,
parent and child class.
25. OOPs Concept
4. Multiple Inheritance - In this type of inheritance, more than one parent exists.
26. OOPs Concept
5. Hybrid Inheritance - It is a combination of all above types of inheritance.
Note: - Multiple and Hybrid inheritance are not supported in java.
Advantages of Inheritance –
It provides code reusability
It is used to achieve runtime polymorphism
Memory Management
27. OOPs Concept
Encapsulation
Wrapping or binding of data and code together in a single unit is called encapsulation.
The variables of a class will be hidden from other classes, and can be accessed only through the methods of
their current class. Therefore, it is also known as data hiding.
A java class is the example of encapsulation. Java Bean is the fully implementation of encapsulation because
all the data members are private in bean class.
28. OOPs Concept
Polymorphism
Polymorphism is the ability of an object to take many forms.
In other words, we can say that if one task can be implemented in many ways.
For example, the role of a boy can play differently such as student, friend, brother, son, etc.
We achieve polymorphism through method overloading and overriding.
29. OOPs Concept
Association
In association, an object can be associated with one or many objects. There can be four types of association
between the objects.
One to one
One to many
Many to one
Many to many
Let's understand the relationship with real-time examples. For example, one country can have one prime
minister (one to one), and a prime minister can have many ministers (one to many). Also, many MP's can have
one prime minister (many to one), and many ministers can have many departments (many to many).
Association can be unidirectional or bidirectional.
30. OOPs Concept
Aggregation
Aggregation represents the relationship where one object contains other objects as a part of its state.
It represents the weak relationship between objects.
To achieve association, we use aggregation.
Object-Oriented Vs. Object Based Programming
Object-based programming language follows all the features of OOPs except Inheritance.
JavaScript and VBScript are examples of object-based programming languages.