0% found this document useful (0 votes)
2 views

JAVA NOTES

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JAVA NOTES

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

JAVA

Java - high level, class based, object oriented programming language


1. Java is a simple programming language
2. Writing, compilation and debugging a program is very easy in java
3. Java helps to create reusable code
4. Java has more features
5. Java is a platform independent, open source, multithreading, more secure and
portable
Java was developed by James Gosling at Sun Microsystems in May 23, 1995

Terminologies:
1. JVM - Java Virtual Machine
2. JDK - Java Development Kit
3. JRE - Java Runtime Environment

OOPS - Object-Oriented Programming System is used to define the structure of the


program
OOPS Principles:
1. Class
2. Method
3. Object
4. Inheritance
5. Abstraction
6. Polymorphism
7. Encapsulation

Class - Class is a collection of methods and objects

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();

Inheritance - Inheritance is accessing one class method from another class


1. Single Inheritance - Accessing one class property from another class
Father -> Son
2. Multiple Inheritance
Why Multiple Inheritance is not supported in Java - It may cause priority
issues. Compilation error / Syntax error
3. Multilevel Inheritance -
Grandfather -> Father -> Son

4. Hierarchical Inheritance - One parent class and more than one child class

5. Hybrid Inheritance - Combination of single and multiple inheritance

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

Switch Statements - It selects one of many code blocks to be executed instead of


writing many if-else statements, you can use the switch statement
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

Loops - It is used to execute a block of code repeatedly as long as a specified


condition is met.
1. for loop:
for (initialization ; condition ; increment/decrement) {
// code block to be executed
}

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);

Jumping Statements - It is control statements that transfer execution control from


one point to another point in the program
1. break - it is used to break the current flow of the program and transfer the
control to the next statement outside a loop or switch statement
2. continue - it doesn't break the loop whereas it skips the specific part of the
loop and jumps to the next iteration of the loop immediately

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);

Date and Time:


1. Date:
Import Statement : import java.time.LocalDate;
Syntax : LocalDate variable = LocalDate.now();

2. Time:
Import Statement : import java.time.LocalTime;
Syntax : LocalTime variable = LocalTime.now();

3. Date & Time:


Import Statement : import java.time.LocalDateTime;
Syntax : LocalDateTime variable = LocalDateTime.now();

Array - It is used to store multiple values in a single variable instead of


declaring separate variables for each value. It must be declared before they are
used. Once an array is created, its size cannot be changed. The first element is
zeroth index, second element is first index, and so on.
Import Statement : import java.util.Arrays;
Syntax : datatype[] arrayName;

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

Collections - It is dynamic memory allocation, will support dissimilar data types,


has no memory wastage like array
1. List:
a. ArrayList - It is all insertion order. Allows duplicate value. Index based
Import Statement : import java.util.ArrayList;
Syntax : ArrayList<datatype> variable = new ArrayList<datatype>();

b. LinkedList - It is all insertion order. Allows duplicate value. Index


based
Import Statement : import java.util.LinkedList;
Syntax : LinkedList<datatype> variable = new LinkedList<datatype>();

2. Set:
a. HashSet - Random order
Import Statement : import java.util.HashSet;
Syntax : HashSet<datatype> variable = new HashSet<datatype>();

b. LinkedHashSet - Insertion order


Import Statement : import java.util.LinkedHashSet;
Syntax : LinkedHashSet<datatype> variable = new LinkedHashSet<datatype>();

c. TreeSet - Ascending order


Import Statement : import java.util.TreeSet;
Syntax : TreeSet<datatype> variable = new TreeSet<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
}

Functions - It is referred as methods. A method is a block of code designed to


perform a specific task when it is called.

Constructor - It is used to initialize objects. The name of constructor must be


same as the name of class. It is called when the object is created.

You might also like