Java notes
Java notes
JAVA :-
It is a high level programming language.
Variables in JAVA :-
It is a piece of memory used to store the data.
One variable can store only one data.
1) Variable declaration – Syntax = data type variable name;
2) Variable initialization – Syntax – Variable name data / value;
3) Variable use
Data type :-
It is used to store the data.
Two types of data – 1] Premitive data type (Keyword)
2] Non-premitive data type (Identifiers)
1] Primitive data type –
A] Numeric-
1) byte [1 byte]
2) short [2 byte]
3) int [4 byte]
4) long [8 byte]
5) float [4 byte]
6) double [8 byte]
B] Character-
1) char [2 byte]
C] Boolean-
1) Boolean [1bit]
Java Page 1 of 23
1) class
2) String
Operators in Java :-
It is a symbol that performs operations on variables and
values.
1] Arithmetic operator-
1) Addition [+]
2) subtraction [-]
3) Multiplication [*]
4) Division [/]
5) Modulus [%]
6) Increment [++]
7) Decrement [--]
2] Relational / Comparison Operator-
Result is always in boolean.
1) Less than [<]
2) Greater than [>]
3) Less than or equals too [ <=]
4] Greater than or equals too [>=]
5) Equals too [==]
6) Not equals too [!=]
3] Logical operators-
Operand / data and result both are in boolean.
1) Anding [&&]
2) Oring [//]
3) Not [!]
4] Assignment operator –
Java Page 2 of 23
It is used to assign the value.
1) Assignment [=]
Conditional statement :-
1] If – statement=
Syntax= if(condition)
{
// Conditional code
}
- Conditional code will execute if condition is true.
- Conditional code will skip if condition is false.
2] if-else statement=
Syntax= if(condition)
{
// conditional code 1
}
else
{
// conditional code 2
}
- Conditional code 1 will execute if condition is true and skip if
condition is false.
- Conditional code 2 will execute if condition 1 is false and skip if
condition is true.
3] Nested if statement=
Java Page 3 of 23
Syntax= if(condition 1)
{ if(condition 2)
{
// conditional code
}
}
- Conditional code will execute if condition 1 is true and condition 2 is
true.
4] Ladder if / if-else statement=
Syntax= if(condition 1)
{
// code 1
}
else
{
if(condition 2)
{
// code 2
}
else
{
// code 3
}
}
- Conditional code 1 will execute if condition 1 is true.
- Conditional code 2 will execute if condition 1 is false and condition 2
is true.
- Conditional code 3 will execute if condition 1 is false and condition 2
is false.
Java Page 4 of 23
Loops in Java:-
To repeat the code multiple time continuously.
1] For loop-
Syntax=
for(variable declaration and initialization ; condition ; variable
increment and decrement)
{
// repeated code
}
2] Nested for-
Syntax=
for( )
{
for( )
{
S.O.P(“a”);
}
}
Println :- Prints the statement and changes the line.
Print :- Print the statement and does not change the line rather than it
continues to print.
Methods in Java :-
1] System define method –
Java Page 5 of 23
This is system default method.
Syntax=
Public static void main(String args[] ) {
} JDK can call this method directly at the time of execution.
2] User define method -
This method is called by programmer or user.
1) Static methods
2) Non static methods
- User define methods will be declared outside the main method and inside
the class.
- To execute user define method, we need to call it inside the main method.
Array in Java
It is used to store the multiple data having same data type.
1) One dimensional array
2) Two dimensional array
3) Multi dimensional array
Syntax=
1] Array declaration and initialization=
Data type, array name[] (data1, data 2, data 3, ---);
2] Array declaration only=
Data type, array name [] = new datatype [size];
- Index of array always start with zero.
- Array is data structure in java which is used to store the multiple data
at a time having same data type.
- Array is homogeneous in nature.
- Its size is fixed means not growable.
Class in Java :-
It is a collection of object / elements / properties / like
variables, methods, constructor, blocks.
Java Page 6 of 23
- Variables = data members.
- Methods = function.
Constructors in Java :-
It is a special member of class because constructor name
must be same as class name.
Purpose –
- Object creation – Loading the non-static properties into the object.
- Variable initialization.
- Class cannot be without constructor.
Syntax – class name ()
{
// Constructor code
}
o Types of constructor –
1] Default constructor-
If programmer don’t declare any constructor in class then at the
time of compilation compiler will declared the constructor which are default
constructor.
2] User define constructor –
Programmer declares the constructor.
User define constructor has two types =
1) without argument constructor-
Also called as = zero argument and without parameterized
Syntax= Class name ()
{
}
Java Page 7 of 23
{ int type constructor
}
Constructor overloading :-
Declaring the multiple constructor in a class with same
constructor name and different argument is called as
constructor overloading.
Object in Java :-
It is a copy of a class.
Syntax =
Class name, reference variable =new class name ();
e.g. Test x = new Test();
Types of variables :-
1] Local variable :-
Java Page 8 of 23
- A variable declare inside or within a method or constructor or
block is called as local variable.
- This scope of local variable is within a method or constructor
or block.
- Local variable is also called as temporary variable.
2] Global variable :-
- A variable which is declared outside any method or block
and inside the class is called as global variable.
- The scope of global variable is throughout the program
execution.
- Global variable is also called as permanent variable.
Java Page 9 of 23
- The scope of public access specifier is throughout
the project.
2] Default access specifier :-
- The properties declared with default access
specifier can be called in other classes of same
package.
- The scope of default access specifier id within the
package only.
- There is no keyword to specify the default access
specifier.
3] Protected access specifier :-
- The scope of protected access specifier is same as
default access specifier i.e. within the package
only.
- The protected properties of class can be called in
other classes of different package only on one
condition i.e. inheritance.
4] Private access specifier :-
- The properties declared with private access
specifier can not be called outside the class.
- The scope of private access specifier is within the
class only.
Java Page 10 of 23
- Object oriented programming is a methodology or
paradigm to design a program using classes and objects.
- It simplifies software development and maintenance by
providing some concepts such as –
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
1] Inheritance :-
- Acquiring / Inheriting property of one class into another
class with the help of ‘extends’ keyword is called as
inheritance.
- From which properties are inherited is called as super
class.
- In which properties are delivered / inherited is called as
sub class.
- Acquiring / inheriting the property of super class into sub
class with the help of ‘extends’ keyword is called as
inheritance.
o Types of inheritance :-
1] Single level inheritance –
- Inherit the property of one super class into one
sub class.
Java Page 11 of 23
Object is super most class in java due to which
diamond ambiguity problem occurs.
At the time of calling the sub class, JVM / object is
confuse and diamond ambiguity problem occurs.
4] Hierarchical inheritance –
- Acquiring / inheriting property of one super
class into multiple sub class is called
hierarchical inheritance.
Java Page 12 of 23
Method overloading :-
o Declaring the multiple method having same method name and
different arguments.
o Overloading can be done with static and non-static method.
o If one method is static and another method is non-static then it
is not overloading because static method will be save in static
pool area and non-static method will be save in heap area.
o Overloading in static method will be only done with static.
o Overloading in non-static is only done with non-static.
Method overriding :-
o Acquiring / inheriting super class method into sub class method
and changing the implementation / definition / body according
to sub class specification.
o Method overriding can be perform with non-static methods
only.
o Without inheritance overriding doesn’t takes place.
o We can’t override the main method.
o Binding occurs at the time of object creation.
o Condition - method and argument will be same.
Method hiding :-
o Declaring a static method in both the classes i.e. super class and
sub class with same method name and same argument.
o In this, memory is not stored and inherited property can’t be
used.
o Hence this scenario is not used in the project.
o It is only done with static method.
Super class static method inherited in sub class, its hide due to sub class
static method with same name and argument.
Polymorphism :-
o One object / property perform the different behavior at different
stages of life cycle is called as polymorphism.
o There are two types of polymorphism –
1) Compile time polymorphism
2) Run time polymorphism
Java Page 13 of 23
1) Compile time polymorphism :-
Method declaration and method definition are binded
together at the time of program compilation based on
argument is called as compile time polymorphism.
As the binding takes place at the time of compilation this
concept is also called as early binding.
For static method binding and initialization is done at
compilation.
Java Page 14 of 23
Concrete class :-
o A class which provide the implementation to the all abstract
method of abstract class is called as concrete class.
o By creating the object of concrete class we call the all
methods of abstract class.
Interface :-
o It is 100% abstract (incomplete).
o All methods are by default abstract.
o All variables are by default – public, static and final.
o Constructor concept is not present in interface so, we can not
create the object in interface.
Implementation class :-
o A class which provide the implementation to the abstract
method of interface is called as implementation class.
o By using implements keyword.
Difference between static and default method :-
1) Static method of interface :-
The static method of interface cannot be inherited in the
implementation class.
So to call the static method of interface, interface name is used.
2) Default method of interface :-
The complete method declared in interface with default
keyword is called as default method of interface.
Default method of interface can be inherited in implementation
class.
Implementation class can override the default method of
interface.
So to call the default method object of implementation class is
used.
Java Page 15 of 23
Generalization :-
o Extracting all the important and common properties from all the
classes and declared them in one super file is called as
generalization.
o Generalization file can be a super class or abstract class or
interface.
o But interface is preferred.
Interface multiple inheritance is the solution of multiple
inheritance.
Encapsulation :-
o Hiding the all data members (variables) of the class from other
classes with the help of private access specifier is called as
encapsulation.
o It is one of the OOP’S principle.
o It is for data security.
o All the properties are private.
Abstraction :-
o Hiding the implementation code from and providing the only
functionality to the end user is called as abstraction.
o It is one of the OOP’S principle.
o It is for code security.
o Ex. While we using any web application like amazon, we can
feel the functionality, but we can’t see the implementation code
behind application.
Casting in java :-
o Converting one data type of information into another data type
of information.
o Types of casting =
1. Primitive casting –
a) Implicit casting
b) Explicit casting
c) Boolean casting
2. Non-primitive casting –
a. Up-casting
b. Down-casting
Java Page 16 of 23
1. Primitive casting –
Converting one data type of information into another data type
of information.
a) Implicit casting =
- Converting lower data type of information into
higher data type of information.
- In implicit casting size of memory gets
increased therefore it is also called as Widening
Casting.
- byte => short => int => long => double
- byte => short => int => float => double
b) Explicit casting =
- Converting higher data type of information into
lower data type of information.
- In explicit casting size of memory gets reduced
therefore it is also called as Narrowing Casting.
- double => long => int => short => byte
- In explicit casting data can be loss.
c) Boolean =
- It is not supported in Java.
2. Non-primitive casting –
Converting one type of class into another type of class.
a. Up casting =
- Acquiring the sub class property into super
class.
- Inheritance is compulsory for up casting.
- In up casting only those properties are up casted
to super class which are inherited from super
class.
- i.e. new properties are declared into sub class
can not be upcasted in super class.
b. Down casting =
- It is not supported in java.
- Down casting can be perform after up casting.
- Acquiring super class property into sub class is
called as down casting.
Java Page 17 of 23
- Inheritance is already done from super class to
sub class that’s why it is not possible to down
cast, hence it is not supported in Java.
- Only syntax is supported.
How to stop and restrict inheritance?
- Make a super class as a final by using final keyword.
- Make all the properties of super class as private.
- Using arguments.
- Using comments.
Java Page 18 of 23
o All the strings are stored inside string pool area which is present inside
heap area.
o String pool area are divided in two area =
1. Constant pool area
2. Non-constant pool area
1. Constant pool area –
- All the strings declared without using new keyword are restored
in constant pool area.
- Constant pool area don’t allow the duplicate string.
2. Non-constant pool area –
- All the string declared with new keyword are stored inside non-
constant pool area.
- Non-constant pool area allow the duplicate values.
Methods of string :-
1. Equals =
- Syntax => .equals
- Working => To compare the variable. It is case sensitive.
- Return type => boolean.
- Argument => object an object.
- Type of method => Non-static.
- Class => String.
2. Equals ignore case =
- Syntax => .equalsIgnorecase
- Working => To compare the variable. It is not case sensitive.
- Return type => boolean.
- Argument => String.
- Type of method => Non-static.
- Class => String
3. Length =
- Syntax => .length
- Working => To find the length of variable.
- Return type => int.
- Argument => Zero.
- Type of method => Non-static.
- Class => String
4. Char at =
- Syntax => .charAt
- Working => To find the character by using number.
- Return type => char.
- Argument => int.
Java Page 19 of 23
- Type of method => Non-static.
- Class => String
5. Concat =
- Syntax => .concat
- Working => To add variables or sentence or words.
- Return type => String.
- Argument => String.
- Type of method => Non-static.
- Class => String
6. Index of =
- Syntax => .indexof
- Working => To find the starting index of letter or word or
string.
- Return type => int.
- Argument => String.
- Type of method => Non-static.
- Class => String
7. Last index of =
- Syntax => .lastIndexof
- Working => To find the starting index of letter or word or
string.
- If there is no any letter or string then result will be -1.
- Return type => int.
- Argument => String.
- Type of method => Non-static.
- Class => String
8. Sub string =
- Syntax => .substring
- Working => To cut and print the letters or words start from
substring value.
- Return type => String.
- Argument => int.
- Type of method => Non-static.
- Class => String
9. To upper case =
- Syntax => .toUppercase
- Working => To make all the letters capital(upper case).
- Return type => String.
- Argument => Zero.
- Type of method => Non-static.
Java Page 20 of 23
- Class => String
11.Split =
- Syntax => .split
- Working => To remove the letter or word from the variable
data.
- Return type => String [].
- Argument => String.
- Type of method => Non-static.
- Class => String
12.Is empty =
- Syntax => .isempty
- Working => To search whether the value of variable is fill or
empty..
- Return type => boolean.
- Argument => Zero.
- Type of method => Non-static.
- Class => String
13.Contains =
- Syntax => .contains
- Working => To check whether the string contains the word or
letter or sentence.
- Return type => boolean.
- Argument => char.
- Type of method => Non-static.
- Class => String
14.Replace =
- Syntax => .replace
- Working => To replace the letter or word.
- Return type => String.
- Argument => char.
- Type of method => Non-static.
Java Page 21 of 23
- Class => String
15.Starts with =
- Syntax => .startswith
- Working => To check whether the string starts with that word
or letter.
- Return type => boolean.
- Argument => String.
- Type of method => Non-static.
- Class => String
16.Ends with =
- Syntax => .endswith
- Working => To check whether the string ends with that letter or
word.
- Return type => boolean.
- Argument => String.
- Type of method => Non-static.
- Class => String
17.Trim =
- Syntax => .trim
- Working => To remove starting and ending spaces of string.
- Return type => String.
- Argument => Zero.
- Type of method => Non-static.
- Class => String
Java Page 22 of 23
Interpreted
High performance
Multithreaded
Distributed
Java Page 23 of 23