JAVA NOTES
JAVA NOTES
Terminologies:
1. JVM - Java Virtual Machine
2. JDK - Java Development Kit
3. JRE - Java Runtime Environment
Method - Method is a block of code that performs specific actions when called
1. Main Method - Main method is the entry point of a Java program and is the
first code to execute when the program starts
2. Sub Method - Set of actions to be performed
Object - Object is used to call a sub method. The object name can be started with
capital or small letter
Syntax : ClassName objectName = new ClassName();
4. Hierarchical Inheritance - One parent class and more than one child class
Abstraction - Hiding the implementation part and showing the necessary part. No
main method.
1. Partial abstraction (abstract class) - It supports abs and non abs method.
Use extends keyword
2. Fully abstraction (interface) - It supports abs method. Use implements keyword
Polymorphism - Taking more than one forms. Poly -> many, morphism -> forms
1.Method overloading (Static Binding / Compile Time Polymorphism):
Class-same
Method-same
Argument-differ
2.Method overriding (Dynamic Binding / Run Time Polymorphism):
Class name-differ(using extends)
Method-same
Argument- same
Encapsulation - Bundles data and methods into a single unit, typically a class.
Hides the internal state of an object from outside classes and controls access to
it through public methods.
Access specifiers / Access modifiers in Java define the scope and visibility of
classes, methods, constructors, and variables. They help implement encapsulation
and control how different parts of a program can interact with each other.
Class Package Subclass World
1. private / x x x
2. default / / x x
3. protected / / / x
4. public / / / /
private - Accessible only within the class where it is declared. Used to hide the
internal details of a class.
default - Accessible only within the same package. No keyword is used to declare
default access. it is default when no modifier is specified.
protected - Accessible within the same package and by subclasses in other
packages. Used when you want to allow inheritance and limited external access.
public - Accessible from any class, package, or subclass. Used for classes,
methods, or fields that need to be globally accessible.
Datatypes - It specify the different sizes and values that can be stored in the
variable
1. Primitive Datatypes - boolean, byte, char, short, int, float, long and
double
2. Non-Primitive Datatypes - String, Array
2. while loop:
while(condition){
// code block to be executed
increment/decrement
}
3. do-while loop:
do{
// code block to be executed
increment/decrement
}
while (condition);
Scanner - It is used to get user input and it is found in the java.util package
Import Statement : import java.util.Scanner;
Scanner Object : Scanner variable = new Scanner(System.in);
2. Time:
Import Statement : import java.time.LocalTime;
Syntax : LocalTime variable = LocalTime.now();
Math Methods:
1. Math.abs() - returns the absolute (positive) value of a number
2. Math.round() - rounds a number to the nearest integer
3. Math.random() - returns a random number between 0 and 1
4. Math.ceil() - rounds a number UP to the nearest integer
5. Math.floor() - rounds a number DOWN to the nearest integer
6. Math.sqrt() - returns the square root of a number
String Methods:
1. length() - returns the length of a specified string
2. concat() - appends a string to the end of another string
3. replace() - searches a string for a specified value and returns a new string
where the specified values are replaced
4. charAt() - returns the character at the specified index (position)
5. toUpperCase() - Converts a string to upper case letters
6. toLowerCase() - converts a string to lower case letters
StringBuffer Methods:
1. append() - concatenates the given argument with this string
2. insert() - inserts the given string with this string at the given position
3. replace() - replaces the given string from the specified begin index and end
index
4. reverse() - replaces the given string from the specified begin index and end
index
5. delete() - deletes the string from the specified begin index to end index
2. Set:
a. HashSet - Random order
Import Statement : import java.util.HashSet;
Syntax : HashSet<datatype> variable = new HashSet<datatype>();
3. Map:
a. HashMap - Random order. If duplicate key is there, it takes the last one.
The key will allows the only one null. Value allows the duplicate null.
Asynchronies(not thread safe)
Import Statement : import java.util.HashMap;
Syntax : HashMap<datatype,datatype> variable = new
HashMap<datatype,datatype>();
b. LinkedHashMap - Insertion order. Key will allows the only one null. Value
allows the duplicate null
Import Statement : import java.util.LinkedHashMap;
Syntax : LinkedHashMap<datatype,datatype> variable = new
LinkedHashMap<datatype,datatype>();
c. TreeMap - Ascending order. Key won't allow null. Value allows the
duplicate null
Import Statement : import java.util.TreeMap;
Syntax : TreeMap<datatype,datatype> variable = new
TreeMap<datatype,datatype>();
d. Hashtable - Random order. Both key and value won't allow null.
Synchronies(thread safe)
Import Statement : import java.util.Hashtable;
Syntax : Hashtable<datatype,datatype> variable = new
Hashtable<datatype,datatype>();
Exceptions - It is like an error, the program will terminated that line itself. It
is an unwanted or unexpected event, which occurs during the execution of a program.
1. ArithmeticException - If we are trying to divide a number by zero, we will get
Arithmetic Exception.
2. NullPointerException - If we give null in the string, we will get Null Pointer
Exception because the default value of null is zero.
3. InputMismatchException - If we are trying to get some datatype input from the
user, but the user is trying to put another datatype value, then we will get Input
Mismatch Exception.
4. ArrayIndexOutOfBoundsException - In particular array, the index value is not
available, then we will get Array Index Out Of Bounds Exception.
5. StringIndexOutOfBoundsException - In particular string, the index value is not
available, then we will get String Index Out Of Bounds Exception.
6. IndexOutOfBoundsException - In a list, the index value is not available, then
we will get Index Out Of Bounds Exception.
7. NumberFormatException - If we give numbers in the string, we can convert the
string data type into integer. But if we give number and character combination in
the string, we can't convert to integer. If we are trying to
convert, we will get Number Format Exception.
Try Catch Exception - If we get exception, try will throw the exception and catch
will catch the exception.The try statement allows you to define a block of code to
be tested for errors while it is being executed. The catch statement allows you to
define a block of code to be executed, if an error occurs in the try block.
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}