java.pdf
java.pdf
https://www.ccbp.in/
In programming languages, every value or data has an associated type known as data type.
Java supports various data types. These data types are categorized into,
Primitive Data Types
Non-Primitive Data Types
boolean: In general, anything that can take one of two possible values is considered a boolean. In Java,true and
false are considered boolean values.
byte: The byte data type is used to store integers without any fractional part whose values are in the range -128 to
127.
short : The short data type is used to store integers without any fractional part whose values are in the range
-32,768 to 32,767.
31 to 2 31-
int: The int data type is used to store integers without any fractional part whose values are in the range -2
1.
63 to
long: The long data type is used to store integers without any fractional part whose values are in the range -2
263-1. The long values should contain the suffix 'l' or 'L'.
float: The float data type is used to store any number with a decimal point. The float data type stores a value up to
7 point precision (ex: 12.1234567). The float values should contain the suffix 'f' or 'F’.
double: The double data type is used to store any number with a decimal point. The double data type stores a value
up to 16 point precision. The double values may contain the suffix 'd' or 'D'.
double breadth = 9.2345D;
String: The String data type is simply a sequence of characters. In Java, double quotes (") are used to represent a
string.
String name = "Rahul";
Array: In Java, an array is an object used to store similar data type values. In Java, the number of elements that an
array can store is always fixed.
Conditional Statements
Conditional Statement: Conditional Statement allows us to execute a block of code only when a specific condition
is true.
If…Else Statements: When an if...else conditional statement is used, the if block of code executes when the
condition is true, otherwise the else block of code is executed.
// Output is:
Collect Water Bottle
Else if Statement: Java provides an else if statement to have multiple conditional statements between if and
else. The else if statement is optional. We can add any number of else if statements after if conditional block.
Switch: A switch block can have multiple case or default labels. The switch statement allows us to execute a
block of code among many cases.
// Output is:
Ten
Nested Conditions: The conditional statements inside another conditional statement is called a nested conditional
statement.
// Output is:
Collect Sprite
Creating Strings:
Using String Literals: A string literal is a value which is enclosed in double quotes (" ").
String str = "ABC";
Using new Keyword: A string can be created by initializing the String class with the new keyword.
String str1 = new String(str);
String Concatenation: The concat() method appends one string to the end of another string. Using this method
we can concatenate only two strings.
Length of String: The string object has a length() method that returns the number of characters in a given string.
String Indexing: We can access an individual character in a string using their positions. These positions are also
called indexes.
The charAt() method is used to get the character at a specified index in a string.
String Slicing: Obtaining a part of a string is called string slicing. The substring() method can be used to access a
part of the string.
Slicing to end
String Repetition: The repeat() method returns a string whose value is the concatenation of the given string
repeated N times.
If the string is empty or the count is zero then the empty string is returned.
Calculations in Java
Subtraction: Subtraction is denoted by - sign. It gives the difference between the two numbers.
System.out.println(5 - 2); // 3
System.out.println(5 * 2); // 10
System.out.println(5 * 0.5); // 2.5
System.out.println(5 / 2); // 2
System.out.println(4 / 2); // 2
Modulo operation: Modulo is denoted by % sign. It returns the modulus (remainder) as a result.
System.out.println(5 % 2); // 1
System.out.println(4 % 2); // 0
Take Input From User: The Scanner class in the package java.util is used to read user input.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String username = input.nextLine();
System.out.println(username);
input.close();
}
}
Following are the methods, used to read different types of inputs from the user:
Method Description
nextInt() Reads an int value
nextLong() Reads a long value
next()
Method Description
nextLine() Reads a String value till the end of line
The print() accepts a value as a parameter and prints text on the console. It prints the result in the same line.
System.out.print("Hello ");
System.out.print("Rahul");
// Output is:
Hello Rahul
The println() accepts a value as a parameter and prints text on the console. It prints the result in the new line.
System.out.println("Hello");
System.out.println("Rahul");
// Output is:
Hello
Rahul
Comments
Single-line comments start with two forward slashes //
// This is a comment
Multi-line comments start with /* and end with */. In between these, we can write any number of statements.
/* This is a
Multi-line Comment */
String Methods
startsWith() str.startsWith(value); returns true if the given string starts with the specified
value. Otherwise, false is returned.
returns true if the given string ends with the specified
endsWith() str.endsWith(value);
value. Otherwise, false is returned.
replaces all the occurrences of the old
replace() str.replace(old, latest); character/substring with the latest character/substring
and returns a new string.
compareToIgnoreCase() str1.compareToIgnoreCase(str2);
String Formatting
As Java is a statically typed language, it requires the data type of value to be specified that is going to be formatted.
This is done with the help of format specifiers.
The format specifiers define the type of data that is going to be used. Below are the commonly used format
specifiers:
SpecifierDescription
String.format() Method: In Java, string formatting can be done using the format() method of the String class.
This method returns the formatted string.
// Output is:
James is 20 years old
printf() Method: The System.out.printf() method can be used to directly format and print the string.
// Output is:
James is 20 years old
Formatting Decimal Numbers: For floating-point numbers, we can specify the number of digits after the decimal
point needs to be formatted.
// Output is:
Actual Run rate: 10.256400
Run rate rounded to 2 decimals: 10.26
DecimalFormat Class: The DecimalFormat class from the package java.text used to format decimal numbers.
The format() method takes a double or long value and returns a String value. If float or int are data types are
passed, they are implicitly converted to double and long respectively.
Numbering Format Specifiers: We can provide an argument index for the format specifiers. It indicates which
argument to be inserted at its position.
String name = "James";
int age = 20;
System.out.printf("%2$s is %1$d years old", age, name);
// Output is:
James is 20 years old
Character Methods
isDigit() Character.isDigit(value); checks whether the given character is a number/digit and returns a
boolean value.
toUpperCase() Character.toUpperCase(value);converts the given lowercase letter to uppercase and returns the
new character.
converts the uppercase letter to lowercase and returns the new
toLowerCase() Character.toLowerCase(value);
character.
converts the character into the respective string.
toString() Character.toString(value);
Relational Operators are used to compare values. It returns true or false as the result of a comparison.
OperatorName ExampleOutput
> Is greater than 2>1 true
== Is equal to 3 == 4 false
Logical operators are used to perform logical operations on boolean values. These will also produce a true or
false as a result.
NameExample Output
&& (5 < 10) && (1 < 2)true
A B A && B
true true true
A B A || B
true true true
A !A
true false
false true
Ternary Operator
Ternary Operator: Ternary Operator is a conditional operator which works similar to if...else statements.
int a = 345 int b = 689 int c = 986 int largest = (a >= b) ? ((a
>= c) ? a : c) : ((b >= c) ? b : c); System.out.println(largest);
// 986
Compound Assignment Operators: Compound assignment operators provide a shorter way to assign an
expression to a variable.
There are different compound assignment operators available in Java: +=-=*=/=%=
, , , , , etc.
int a = 10;
int b = 11;
a -= 2;
b %= 3;
System.out.println(a); // 8
System.out.println(b); // 2
Unary Operators: The unary operators are those that operate upon a single operand and produce a new value.
We have learned some of the unary operators like the logical NOT (!) operator. A few other unary operators are,
Increment Operator
Decrement Operator
Increment Operator: The Increment Operator (++) is an operator which is used to increment the value of a
variable by 1, on which it is applied.
The increment operator can be used in two ways:
Prefix (++x): If an Increment operator is used in front of an operand, then it is called a Prefix.
int a = 10;
++a;
int number = ++a;
System.out.println("a = " + a);
System.out.println("number = " + number);
// Output is:
a = 12
number = 12
Postfix (x++): If an Increment operator is used after an operand, then is called a Postfix.
int a = 10;
a++;
int number = a++;
System.out.println("a = " + a);
System.out.println("number = " + number);
// Output is:
a = 12
number = 11
Decrement Operator: The Decrement operator is an operator which is used to decrease the value of the variable
by 1, on which it is applied.
The decrement operator can also be used in two ways:
Prefix (--x): Prefix decrement is similar to prefix increment, except that the variable value is decremented by
one instead of being incremented.
int a = 10;
--a;
int number = --a;
System.out.println("a = " + a);
System.out.println("number = " + number);
// Output is:
a = 8
number = 8
Postfix (x--): Postfix decrement is similar to postfix increment, except that the variable value is decremented
by one instead of being incremented.
int a = 10;
a--;
int number = a--;
System.out.println("a = " + a);
System.out.println("number = " + number);
// Output is:
a = 8
number = 9
Escape Sequences: A character with a backslash \ just before a character is called an escape character or escape
sequence.
Most commonly used escape sequences include:
\n
🡢 New Line
\t Tab Space
🡢
\\ Backslash
🡢
\' Single Quote
🡢
\"
🡢 Double Quote
Java Summary Cheat Sheet - 2
https://www.ccbp.in/
Loops
while loop: It allows us to execute a block of code several times as long as the condition evaluates to true
int counter = 0;
while (counter < 2) {
System.out.println(counter);
counter = counter + 1;
}
// Output is:
0
1
do...while loop: It is similar to the while loop. The only difference is that in the do...while loop, the check is
performed after the do...while block of code has been executed.
do {
System.out.println("Line Executed");
} while (3 > 10);
// Output is:
Line Executed
for loop: It is used to execute a block of code a certain number of times. It is generally used where the number of
iterations is known.
// Output is:
0
1
Arrays
Array: In Java, an array is an object used to store similar data type values. In Java, the number of elements that an
array can store is always fixed.
Accessing Array Elements: We can access the elements of an array using these index values.
Creating an Array using a new Keyword: We can also create the array using new keyword. We can create an
array of required length and later assign the values.
int[] arr;
arr = new int[3];
Printing an Array: The Arrays class provides a method toString(), which can be used to print the array in Java.
// Output is:
[12, 4, 5, 2, 5]
// Output is:
12
4
5
Length of an Array: In Java, we can find the array length by using the attribute length.
Array Concatenation: Joining two arrays is called Array Concatenation. In Java, the System class contains a method
named arraycopy() to concatenate two arrays.
// Output is:
[12, 4, 5, 2, 5, 6, 10, 11, 6]
Array Slicing: It is a method of obtaining a subarray of an array. We can get the subarray from an array using
Arrays.copyOfRange() method.
int[] originalArr = { 1, 2, 3, 4, 5 };
int startIndex = 2;
int endIndex = 4;
int[] slicedArr = Arrays.copyOfRange(originalArr, startIndex, endIndex);
System.out.println(Arrays.toString(slicedArr));
// Output is:
[3, 4]
Reversing Arrays: The Collections.reverse() method is used for reversing the elements present in the array
passed as an argument to this method.
Integer[] arr = {3, 30, 8, 24};
Collections.reverse(Arrays.asList(arr));
System.out.println(Arrays.toString(arr)); // [24, 8, 30, 3]
Descending Order: The reverse sorting is done by passing Collections.reverseOrder() as an argument to the
Arrays.sort() method.
Integer[] arr = {3, 1, 2};
Arrays.sort(arr, Collections.reverseOrder());
System.out.println(Arrays.toString(arr)); // [3, 2, 1]
Methods
Methods: Java doesn't have independent functions because every Java function belongs to a class and is called a
Method.
Method Declaration: A Method must be declared before it is used anywhere in the program.
Calling a Method: The block of code in the methods is executed only when the method is called.
// Output is:
Hello, I am in the greet method
Returning a Value: We can pass information from a method using the return keyword. In Java,return is a reserved
keyword.
// Output is:
Hello, I am in the greet method
Method with Parameters: The information can be passed to methods as arguments in Java.
// Output is:
Hello Rahul
Method Overloading: If a class has multiple methods having same name but different in parameters, it is known as
Method Overloading.
Passing Mutable Objects: when mutable objects are passed as method arguments, the changes done to the object
inside the method will affect the original object.
// Output is:
[72, 86, 20, 224, 132, 36]
Passing Immutable Objects: Immutable objects are passed as method arguments, the changes done to the object
inside the method will not affect the original object.
// Output is:
Inside fullName() method: William Smith
Inside main() method: William
Recursion: Recursion is a process in which a method calls itself in the process of its execution. A recursive method
terminates when a condition is met. This condition is also called the Base Case.
Nested Loops
Nested Loops: If a loop exists inside the body of another loop, it is called a nested loop. The inner loop will be
executed one time for each iteration of the outer loop.
Example 1:
// Output is:
Outer: 0
Inner: 0
Inner: 1
Outer: 1
Inner: 0
Inner: 1
After nested for loops
Example 2:
//Output is:
Outer For Loop: 0
Inner While Loop: 0
Inner While Loop: 1
Outer For Loop: 1
Inner While Loop: 0
Inner While Loop:1
Loop Control Statements: The statement which alters the flow of control of a loop is called a Loop Control
Statement.
Name Usage
Break break keyword is used to stop the execution of the loop.
Continue continue keyword is used to skip the current execution of the loop.
Break (in nested loops)break keyword in the inner loop stops the execution of the inner loop.
Big Integers
BigInteger: The BigInteger is the class used for mathematical operations which involve very big integer
calculations that are outside the limit of all available primitive data types.
BigInteger Methods
subtract()bigNum1.subtract(bigNum2);performs the subtraction for the given two BigIntegers and returns the
difference.
multiply()bigNum1.multiply(bigNum2);performs the multiplication for the given two BigIntegers and returns the
product.
performs the division for the given two BigIntegers and returns the
divide() bigNum1.divide(bigNum2);
quotient.
performs the exponentiation operation and returns the result.
pow() bigNum.pow(exponent);
returns a value that is equal to the absolute value of the given BigInteger.
abs() bigNum.abs();
Converting Integers to BigInteger: The valueOf() method can be used to convert integers values to a
BigInteger.
Converting BigInteger to Integers or String: We can convert BigInteger to int, long, and String data types.
BigInteger Constants: The BigInteger class defines some constants for the ease of initialization.
BigInteger.ZERO: The BigInteger constant for 0
BigInteger.ONE: The BigInteger constant for 1
BigInteger.TWO: The BigInteger constant for 2
BigInteger.TEN: The BigInteger constant for 10
ArrayList
ArrayList: The ArrayLists adjusts its size automatically when an element is added or removed. Hence, it is also
known as a Dynamic Array.
Adding Primitive Data Types: ArrayList can only store objects. To use primitive data types, we have to convert
them to objects.
In Java, Wrapper Classes are can be used to convert primitive types (int, char, float, etc) into corresponding objects.
Autoboxing: The conversion of primitive types into their corresponding wrapper class objects is called Autoboxing.
Unboxing: The conversion of wrapper class objects into their corresponding primitive types is called Unboxing.
Method Usage
add() used to add a single element to the ArrayList.
remove(index) removes the element at the specified position, i.e index, in the ArrayList.
removes the first occurrence of the specified element from the ArrayList
if it is present. Remains unchanged if not present
It completely removes all of the elements from the ArrayList.
clear() arrList.clear()
used to find the size of an ArrayList.
size() arrList.size()
returns the index of the first occurrence of the specified element in the
indexOf() arrList.indexOf(obj);
ArrayList. Returns -1 if not present
Iterating over an ArrayList: Similar to iterating over an array, we can use the loops to iterate over an ArrayList.
ArrayList Concatenation: The addAll() method is used to concatenate two ArrayLists. This method appends the
second ArrayList to the end of the first ArrayList.
// Output is:
[5, 10, 25, 30]
ArrayList Slicing: The subList() method is used for slicing of ArrayLists . It works similar to the copyOfRange()
method in Arrays.
// Output is:
[10, 15]
Arrays.asList(arr);
arrList.toArray(arr);
Frequency of an Element: The Collections.frequency() method is used to find the frequency with which an element
occurs in the given ArrayList.
Sorting an ArrayList: The Collections.sort() method can be used to sort the given ArrayList in two different
ways
Ascending order
Descending order: An ArrayList can be sorted in descending order by passing the argument
Collection.reverseOrder() to the Collections.sort() method.
Integer[] arr = {3, 6, 2, 1};
ArrayList<Integer> arrList= new ArrayList<>(Arrays.asList(arr));
Collections.sort(arrList, Collections.reverseOrder());
System.out.println(arrList); // [6, 3, 2, 1]
HashSet
HashSet: The HashSet is also an unordered collection of elements. The HashSet stores only unique elements and
duplicate elements are not allowed.
Iterating Over a HashSet: Similar to iterating over an Array or ArrayList, we can use the for-each loop to iterate
over a HashSet.
// Output is:
Rohit
Rahul
Virat
Sachin
HashSet Operations
Union: The addAll() method can be used to perform the union of two sets.
Syntax: hset1.addAll(hset2);
// Output is:
on: [32, 3, 8, 30]
Intersection: The retainAll() method can be used to perform the intersection of two sets.
Syntax: hset1.retainAll(hset2);
// Output is:
[32, 8]
Difference: The removeAll() method can be used to find the difference between two sets.
Syntax: hset1.removeAll(hset2);
HashSet<Integer> hset1 = new HashSet<>();
HashSet<Integer> hset2 = new HashSet<>();
hset1.add(3); hset1.add(32);
hset1.add(8); hset2.add(8);
hset2.add(24); hset2.add(30);
hset1.removeAll(hset2);
System.out.println(hset1);
//Output is:
[32, 3]
SuperSet: A superset of any given set is defined as the set which contains all the elements present in the given set.
The containsAll() can be used to check if the given set is the superset of any other set.
hset1.containsAll(hset2);
Here, containsAll()checks if all the elements in hset2 are present in hset1, i.e, it checks if hset1 is a superset of
hset2.
Subset: A subset of any given set is defined as the set which contains atleast one element present in the given set.
The containsAll() can also be used to check if the given set is the subset of any other set.
hset2.containsAll(hset1);
Here, containsAll()checks if all the elements in hset1 are present in hset2, i.e, it checks if hset1 is a subset of
hset2.
HashMap
HashMap: The HashMap is also an unordered collection of elements. HashMap stores the data in key/value pairs.
Here, keys should be unique and a value is mapped with the keyHashMap
. can have duplicate values.
get() hmap.get(key); used to access the value mapped with a specified key in a HashMap.
replace( hmap.replace(key, replaces the old value of the specified key with the new value.
newValue);
)
Method Syntax Usage
remove() hmap.remove(key); used to remove an element from a HashMap.
values() hmap.values(); to get all the values mapped to the keys in a HashMap.
containsKey() hmap.containsKey(key); It returns true if the HashMap contains the specified key. Otherwise
false is returned.
It returns true if the HashMap contains the specified value.
containsValue() hmap.containsValue(value);
Otherwise false is returned.
used to copy all the elements from a HashMap to another HashMap.
putAll() hmap2.putAll(hmap1);
Iterating a HashMap
// Output is:
James:121
Robert:145
John:78
Antony:136
Math Methods
MethodsDescription
pow() It calculates the exponents and returns the result.
round() It rounds the specified value to the closest int or long value and returns it.
min() Returns the numerically lesser number between the two given numbers.
max() Returns the numerically greater number between the two given numbers.
Type Conversion: Type conversion is a process of converting the value of one data type (int, char, float, etc.) to
another data type.
Java provides two types of type conversion :
Implicit Type Conversion
Explicit Type Conversion (Type Casting)
Implicit Type Conversion: Java compiler automatically converts one data type to another data type. This process is
called Implicit type conversion.
int value1 = 10;
float value2 = value1;
System.out.println(value1); // 10
System.out.println(value2); // 10.0
Explicit Type Conversion: In Explicit Type Conversion, programmers change the data type of a value to the desired
data type.
This type of conversion is also called Type Casting since the programmer changes the data type.
float x = 10.0f;
System.out.println((int)x); // 10
Converting any Primitive Type to String: Any data type can be converted to String using the string method
valueOf().
int num = 2;
float floatNum = 2.34f;
char ch = 'a';
System.out.println(String.valueOf(num)); // 2
System.out.println(String.valueOf(floatNum)); // 2.34
System.out.println(String.valueOf(ch)); // a
We may also use the toString() method of the corresponding wrapper class to convert primitive data types to
String.
Example 1:
int a = 10;
String str = Integer.toString(a);
System.out.println(str); // 10
Example 2:
char a = 'A';
String str = Character.toString(a);
System.out.println(str); // A
Converting String to any Primitive Type: We can convert String to int in Java using Integer.parseInt()
method.
Similarly, for other primitive data types as given the table below,
float Long.parseLong()
double Float.parseFloat()
boolean Double.parseDouble()
Boolean.parseBoolean()
Converting char to int: We can convert char to intin Java using Character.getNumericValue() method.
char ch = '3';
int num = Character.getNumericValue(ch);
System.out.println(num); // 3
Converting int to char: We can convert int to charin Java using Character.forDigit() method.
int num = 3;
char ch = Character.forDigit(num, 10);
System.out.println(ch); // 3
Getting Unicode Value of a Character: Using Explicit Type Conversion, we convert char to unicode value of type
int.
char ch = 'A';
System.out.println((int)ch); // 65
Getting Character Representation of a Unicode Value: we have to explicitly typecast the int value to char.
class ClassName {
// attributes
// methods
}
this keyword: Java consists of this keyword to access the instance attributes and methods. In Java, however, we can
access them without using this keyword.
Attributes & Methods: In Java, instance or class attributes/methods are distinguished with the static keyword.
Instance Attributes: In Java, instance attributes are also called non-static attributes.
class Mobile {
String model;
String camera;
// methods
}
Instance Methods: In Java, instance methods are also called non-static methods.
class Mobile {
// attributes
void makeCall() {
System.out.println("calling...");
}
}
Constructor: Unlike Java methods, a constructor should be named the same as the class and should not return a
value.
class Mobile {
String model;
String camera;
Mobile(String modelSpecs, String cameraSpecs) {
model = modelSpecs;
camera = cameraSpecs;
}
}
Instance of Class: An instance of a class is called an object. An object is simply a collection of attributes and
methods that act on those data.
class Mobile {
String model;
String camera;
Mobile(String modelSpecs, String cameraSpecs) {
model = modelSpecs;
camera = cameraSpecs;
}
}
c l a s
s
public static void main(String[] args) {
B a s e
Mobile mobile1 = new Mobile("Samsung Galaxy S22", "108 MP");
{
}
}
Accessing Attributes & Methods with Objects: we can use the dot ( .) notation to access the attributes and
methods with objects of a class.
class Mobile {
String model;
String camera;
Mobile(String modelSpecs, String cameraSpecs) {
model = modelSpecs;
camera = cameraSpecs;
}
void makeCall(long phnNum) {
System.out.printf("calling...%d", phnNum);
}
}
class Base {
// Output is:
iPhone 12 Pro
12 MP
calling...9876543210
Updating Attributes: We can update the attributes of the objects.
class Mobile {
String model;
String camera;
Mobile(String modelSpecs, String cameraSpecs) {
model = modelSpecs;
camera = cameraSpecs;
}
}
class Base {
public static void main(String[] args) {
Mobile mobile = new Mobile("iPhone 12 Pro", "12 MP");
mobile.model = "Samsung Galaxy S22";
mobile.camera = "108 MP";
System.out.println(mobile.model);
System.out.println(mobile.camera);
}
}
// Output is:
Samsung Galaxy S22
108 MP
Class Attributes: Attributes whose values stay common for all the objects are modelled as class Attributes. The
static keyword is used to create the class attributes.
class Cart {
static int flatDiscount = 0;
static int minBill = 100;
}
Accessing Class Attributes: The class attributes can also be accessed using the dot (. ) notation. We can access the
class attributes directly using the class name.
class Cart {
static int flatDiscount = 0;
static int minBill = 100;
} class Base
{
public static void main(String[] args) {
System.out.println(Cart.flatDiscount);
System.out.println(Cart.minBill);
}
}
// Output is:
0
100
Class Methods: In Java, class methods are also called static methods. Thestatic keyword is used to create the class
methods.
class Cart {
static int flatDiscount = 0;
static int minBill = 100;
static void updateFlatDiscount(int newFlatDiscount) {
flatDiscount = newFlatDiscount;
}
}
Accessing Class Methods: The class methods can also be accessed using the dot (. ) notation. We can access the
class methods directly using the class name.
class Cart {
static int flatDiscount = 0;
static int minBill = 100;
static void updateFlatDiscount(int newFlatDiscount) {
flatDiscount = newFlatDiscount;
}
}
c l a s
s public static void main(String[] args) {
B a s e
Cart.updateFlatDiscount(50);
{
System.out.println(Cart.flatDiscount); // 50
}
}
OOPS
OOPS: Object-Oriented Programming System (OOPS) is a way of approaching, designing, developing software that is
easy to change.
Encapsulation: It is a process of wrapping related code and data together into a single unit.
class Student {
private int age;
public int getAge() {
return age;
}
p u b l i c v o i d s e t A g e ( i n t a g e ) {
this.age = age;
}
}
class Main {
public static void main(String[] args) {
Student student = new Student();
student.setAge(20);
System.out.println(student.getAge()); // 20
}
}
Inheritance: It is the mechanism by which one class is allowed to inherit the features(fields and methods) of another
class.
class Mammal {
// attributes and methods
} class Horse extends Mammal
{
// attributes and methods of Horse
}
class Mammal {
String name;
Mammal(String name) {
this.name = name;
}
void eat() {
System.out.println("I am eating");
}
}
Method Overriding: It allows a subclass to provide a specific implementation of a method that its superclass already
provides.
class Mammal {
void eat() {
System.out.println("Mammal is eating");
}
} c l a s s
H o r s e
e x void
t e eat()
n d s{
M a m m a l {
System.out.println("Horse is eating");
}
}
c l a s
s public static void main(String[] args) {
B a s eHorse horse = new Horse();
{ horse.eat();
}
}
// Output is:
Horse is eating
Composition: It describes a class whose non-static attributes refer to one or more objects of other classes.
import java.util.*;
class Book {
String title;
String author;
Book(String title, String author) {
this.title = title;
this.author = author;
}
}
c l a s s
L i b r a r
ArrayList<Book> books;
y {
Library(ArrayList<Book> books) {
this.books = books;
}
v o i d d i s p l a y B o o k s ( ) {
for (Book book : books) {
System.out.printf("Title: %s, Author: %s\n", book.title, book.author);
}
}
}
c l a s
s
public static void main(String[] args) {
B a s e
Book book1 = new Book("Head First Design Patterns", "Eric Freeman");
{
Book book2 = new Book("Clean Code", "Robert C. Martin");
ArrayList<Book> booksList = new ArrayList<>();
booksList.add(book1);
booksList.add(book2);
Library library = new Library(booksList);
library.displayBooks();
}
}
// Output is:
Title: Head First Design Patterns, Author: Eric Freeman
Title: Clean Code, Author: Robert C. Martin
Polymorphism: It refers to an object’s capacity to take several forms. Polymorphism allows us to perform the same
action in multiple ways in Java. Polymorphism is of two types:
Compile-time polymorphism
Runtime polymorphism
Compile-time Polymorphism: A polymorphism that occurs during the compilation stage is known as a Compile-
time polymorphism. Method overloading is an example of compile-time polymorphism.
class Shapes {
public void area(double base, double height) {
System.out.println("Area of Triangle = " + 0.5 * base * height);
}
p u b l i c v o i d a r e a ( i n t l e n g t h , i n t b r e a d t
System.out.println("Area of Rectangle = " + length * breadth);
}
}
c l a s
s
public static void main(String[] args) {
B a s e
Shapes triangle = new Shapes();
{
Shapes rectangle = new Shapes();
triangle.area(8.5, 10.23);
rectangle.area(5, 3);
}
}
// Output is:
Area of Triangle = 43.4775
Area of Rectangle = 15
Runtime Polymorphism: A polymorphism that occurs during the execution stage is called Runtime polymorphism.
Method overriding is an example of Runtime polymorphism.
class Mammal {
void eat() {
System.out.println("Mammal is eating");
}
} c l a s s
D o g
e x void
t e eat()
n d s{
M a m mSystem.out.println("Dog
a l { is eating");
}
}
c l a s
s public static void main(String args[]) {
B a s eMammal mammal = new Dog();
{ mammal.eat();
}
}
// Output is:
Dog is eating
Abstraction: It is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with,
Abstract classes
Interfaces
Abstract Methods: A method which is declared with the abstract keyword is known as an abstract method.
Interface: It is similar to an abstract class. It cannot be instantiated and consists of abstract methods.
interface InterfaceName {
// body of the interface
}
The
implements keyword is used to inherit interfaces from a class.
interface CricketPlayer {
void run();
}
class Person implements CricketPlayer {
public void run() {
System.out.println("Running");
};
}
class Base {
public static void main(String[] args) {
Person person = new Person();
person.run();
}
}
// Output is:
Running
Default Methods in Interfaces: We can write the implementation of a method inside the interface. These methods
are called default methods.
The default keyword is used to declare a method in the interface as default method.
Inheritance among Interfaces: An interface can inherit another interface. We use the extends keyword to inherit an
interface from another interface.
interface CricketPlayer {
void run();
} interface Wicketkeeper extends CricketPlayer
{
void wicketkeeping();
} class Person implements Wicketkeeper
{
public void wicketkeeping() {
System.out.println("Wicketkeeping");
}
p u b l i c v o i d r u n ( ) {
System.out.println("Running");
};
} class Base
{
public static void main(String[] args) {
Person person = new Person();
person.wicketkeeping();
person.run();
}
}
// Output is:
Wicketkeeping
Running
Errors & Exceptions
try {
int result = 5 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Denominator can't be 0");
} catch (ArithmeticException e) {
System.out.println("Invalid value");
} finally {
System.out.println("Execution completed");
}
}
// Output is:
Denominator can't be 0
Execution completed
Raising Exceptions: In Java, we can throw an exception explicitly using throw keyword.
try {
throw new ArithmeticException("Denominator can't be 0");
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
// Output is:
Denominator can`t be 0
throws Keyword: In Java, throws keyword is used to specify the type of exception that might be thrown by a
method.
class Main {
static void divideByZero() throws ArithmeticException {
throw new ArithmeticException("Division with zero");
}
p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s )
try {
divideByZero();
} catch (ArithmeticException e) {
System.out.println(e);
}
}
}
// Output is:
java.lang.ArithmeticException: Division with zero
Today's Date: The LocalDate class provides now() method which returns the date object with today's date.
In Java, we have a Duration class to find the difference between two times in seconds.
Types of Inheritance
Single Inheritance: Single inheritance involves extending a single superclass from a single subclass.
class Mammal {
String type;
} class Horse extends Mammal
{
String breed;
}
Multilevel Inheritance: In multilevel inheritance, a subclass extends from a superclass and then the same subclass
acts as a superclass for another class.
class Mammal {
String type;
}
class Horse extends Mammal {
String breed;
} class MustangHorse extends Horse
{
String name;
}
Hierarchical Inheritance: In hierarchical inheritance, multiple subclasses extend from a single superclass.
class Mammal {
String type;
} class Horse extends Mammal
{
String breed;
} class Dog extends Mammal
{
String breed;
}
Multiple Inheritance: In multiple inheritance, a single class/interface can inherit multiple interfaces.
interface InswingBowler {
void inswing();
} interface OutswingBowler
{
void outswing();
} class BowlerA implements InswingBowler, OutswingBowler
{
public void inswing() {
System.out.println("Inswing bowling");
}
p u b l i c v o i d o u t s w i n g ( ) {
System.out.println("Outswing bowling");
}
}
Final
Final keyword: The final keyword is used for variables, classes and methods, which makes them non-changeable
(impossible to inherit or override).
Final variable: When the final keyword is used with a variable, it indicates that the variable is constant and the
value of it cannot be reassigned.
final int SPEED_LIMIT = 60;
SPEED_LIMIT = 90;
System.out.println(SPEED_LIMIT);
// Output is:
file.java:4: error: cannot assign a value to final variable SPEED_LIMIT
SPEED_LIMIT = 90;
^
Final method: A method declared with final is called a final method, it restrict the unwanted and improper use
of method definition while overriding the method.
class Mammal {
final void eat() {
System.out.println("Mammal is eating");
}
} c l a s s
H o r s e
e x void
t e eat()
n d s{
M a m mSystem.out.println("Horse
a l { is eating");
}
}
c l a s
s public static void main(String[] args) {
B a s eHorse horse = new Horse();
{ horse.eat();
}
}
// Output is:
file.java:10: error: eat() in Horse cannot override eat() in Mammal
void eat() {
^
overridden method is final
Final class: A class declared with final is called a final class , it cannot be inherited. All the wrapper classes are
final classes. Hence, we cannot inherit the wrapper classes.
// Output is:
Main.java:5: error: cannot inherit from final Product
class ElectronicItem extends Product {
^
Access Modifiers
Access Modifiers: Access modifiers are the keywords that set access levels when used with the classes, methods,
constructors, attributes, etc.
In Java, we have four access modifiers to set the accessibility. They are,
Private: The access level of a private modifier is only within the declared class. It is not accessible outside of the
class.
Default: The access level of a default modifier is up to the class/subclass of the same package. It cannot be accessed
from outside the package.
Protected: The access level of a protected modifier is up to the class/subclass of the same package and also to a
different package through the subclass. A subclass is required to access it from a different package.
Public: The access level of a public modifier is everywhere in the program. It means that it is accessible from the
class/subclass of the same/different package.
Access Same Same package Same package otherDifferent package Different package other
Modifier Class subclass classes subclass classes
private Yes No No No No
All the access modifiers work the same with the variables, methods and constructors.
Access Modifiers with Classes: Classes in Java can only have Public or Default as access modifiers.
Public: When a class is declared public, it is accessible from the class/subclass of the same/different package.
Default: When no access modifier is specified to the classes, then they can be called default classes, it is
accessible only to the classes or subclasses of the same package.
Upcasting
Upcasting: In Java, a superclass reference variable can be used to refer to its subclass object i.e., we can specify a
superclass as a type while creating an object of its subclass.
Invoking Methods: While upcasting, we can access all the members of the superclass but can only access a few
members like overriding methods of the subclass.
class Mammal {
void eat() {
System.out.println("Mammal is eating");
}
} c l a s s
H o r s e
e x void
t e eat()
n d s{
M a m mSystem.out.println("Horse
a l { is eating");
}
}
c l a s
s public static void main(String[] args) {
B a s eMammal animal = new Horse();
{ animal.eat();
}
}
// Output is:
Horse is eating
class Mammal { }
class Horse extends Mammal {
void eat() {
System.out.println("Horse is eating");
}
}
c l a s
s public static void main(String[] args) {
B a s eMammal animal = new Horse();
{ animal.eat();
}
}
// Output is:
Main.java:10: error: cannot find symbol
animal.eat();
class Mammal { }
class Horse extends Mammal {
String breed = "Shire";
} class Base
{
public static void main(String[] args) {
Mammal animal = new Horse();
System.out.println(animal.breed);
}
}
// Output is:
file.java:8: error: cannot find symbol
System.out.println(animal.breed);
Super Keyword
super: super is the keyword is used to access the superclass members like attributes, methods and also the
constructors inside the subclass.
class Mammal {
String type = "animal";
void eat() {
System.out.println("Eating");
}
} c l a s s
H o r s e
e x String
t e n type
d s = "mammal";
M a void
m m display()
a l { {
System.out.println("Horse is an " + super.type);
}
v o i d e a t ( ) {
super.eat();
}
}
c l a s
s
public static void main(String[] args) {
B a s e
Horse horse = new Horse();
{
horse.display();
horse.eat();
}
}
// Output is:
Horse is an animal
Eating
class Mammal {
String name;
Mammal() {
System.out.println("Superclass constructor called");
}
} c l a s s
H o r s e
e x String
t e n breed;
d s
M a Horse(String
m m a l {
breed) {
this.breed = breed;
}
}
c l a s
s public static void main(String[] args) {
B a s eHorse horse = new Horse("Shire");
{
}
}
// Output is:
Superclass constructor called
class Mammal {
String name;
Mammal(String name) {
this.name = name;
}
} c l a s s
H o r s e
e x String
t e n breed;
d s
M a Horse(String
m m a l {
name, String breed) {
super(name);
this.breed = breed;
}
}
c l a s
s public static void main(String[] args) {
B a s e
Horse horse = new Horse("Alex", "Shire");
{
System.out.println(horse.name);
System.out.println(horse.breed);
}
}
// Output is:
Alex
Shire
Lambda Expressions
Functional Interface: A functional interface is an interface that has a single abstract method (SAM).
interface Runner {
void run();
}
Lambda Expressions: They provide a way to represent a function as an object. It is an anonymous function that can
be passed around as a value.
interface Runner {
void run();
} class BaseballPlayer
{
public static void main(String[] args) {
Runner ref = () -> System.out.println("Running");
ref.run();
}
}
// Output is:
Running
Lambda Expression with Parameters: The lambda expressions can have parameters similar to methods.
interface Sum {
int add(int a, int b);
}
class Main {
public static void main(String[] args) {
Sum ref = (a, b) -> a + b;
System.out.println(ref.add(2, 3)); // 5
}
}
Lambda body for a block of code: The lambda expression consisting a block of code is surrounded by curly braces.
interface Sum {
int add(int a, int b);
} class Main
{
public static void main(String[] args) {
Sum ref = (x, y) -> {
int numSum = x + y;
if (numSum > 0)
return numSum;
return 0;
};
System.out.println(ref.add(2, 3)); // 5
}
}
Streams
Stream: A stream is a sequence of elements that supports various operations for processing the elements. We can
perform operations on a stream without modifying the original data.
Creating an Empty Stream: The Stream interface consists of a static and default method empty() that can be
used to create an empty stream.
Creating a Stream with Collections: All the classes that implement Collection interface consists of a stream()
method that can be used for creating a stream of its corresponding type.
We can also create a stream directly using the of() method of the Stream interface.
Chaining Streams: We can combine multiple stream operations together to form a single expression.
streamSource.intermediateOperation().terminalOperation();
Intermediate Operations: An intermediate operation is a stream operation that produces a new stream as output
after performing the specified operations. A stream can have zero or multiple intermediate operation.
sorted()
stream.sorted(Comparator)used to sort the elements of the stream.
distinct()stream.distinct()
used to remove duplicate elements from the stream.
Terminal Operations: A terminal operation is a stream operation that consumes the elements of a stream and
produces a result. After a terminal operation, no other operation can be done.
findAny() stream.findAny();
Example 1:
// Output is:
Alice
Charlie
Example 2:
Optionals
Optional Class: The Optional class is designed to be used as a return type for methods that may or may not return
a value. It provides several methods to help prevent the NullPointerException.
Creating Optionals: The Optional class provides different methods to create optionals.
empty()
of()
ofNullable()
empty(): The empty() is a static method that creates an instance of Optional with no values.
of(): The of() is a static method that accepts a non-null value as an argument and returns an instance of the
Optional with the specified value.
ofNullable(): The ofNullable() is a static method that accepts a value and returns the instance of Optional with
the specified value. If null is provided as a value, it returns an empty optional.
Optional Methods:
orElse() optional.orElse(); used to get a default value (dynamic value) if the optional is empty.
orElseGet() optional.orElseGet(Supplier); used to perform specified operation on the elements in the optional.
ifPresent() optional.ifPresent(Consumer);
Method Syntax Usage
map() optional.map(Function); used to perform a specified operation on elements in an optional and
return a new optional.
filter() optional.filter(Predicate); used to filter the elements in an optional based on a specified condition
and return a new optional.
I/O Streams
Paths: In Java, the Path interface from java.nio.file package represents a path to a file or directory in the file
system.
The paths are of two types:
Relative path
Absolute path
Relative Path: A relative path is a path that specifies the location of a file or directory relative to the current working
directory.
Example: documents\file.txt
Absolute Path: An absolute path is a path that specifies the exact location of a file or directory in the file system,
starting from the root directory.
Example: /home/rahul/documents/file.txt
// Output is:
/home/rahul/documents/file.txt
BufferedWriter: The BufferedWriter buffers characters to provide for the efficient writing of single characters,
arrays, and strings. The Writer writes text to a character-output stream.
Writing Data to a File
try {
BufferedWriter buffer = new BufferedWriter(new FileWriter("destination.txt"));
buffer.write("Writing to a file using BufferedWriter");
buffer.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
BufferedReader: The BufferedReader is a class from the java.io package extends the abstract class Reader. It
uses the Reader object to read data from a file and it stores the data in an internal buffer.
Reading Data from a File: The BufferedReader provides various methods to read data from a file:
read()
readLine()
read(): The read() method reads a single character from a file. It returns int data type.
try {
BufferedReader br = new BufferedReader(new FileReader("source.txt"));
char[] arr = new char[100];
br.read(arr);
System.out.println(arr); // Hello World!
br.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
readLine(): The readLine() method is used to read a single line of text in the file. It returns String data type.
try {
BufferedReader br = new BufferedReader(new FileReader("source.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("destination.txt"));
String line = br.readLine();
bw.write(line);
br.close();
bw.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
Generics
Type Parameters: In Java, type parameter names are typically written in uppercase letters to distinguish them
from regular class or interface names. The most commonly used type parameter names are:
E - Element
K - Key
N - Number
T - Type
V - Value
Generics: Generics allows us to write a single piece of code that can work with multiple types. Generics do not work
with primitive types.
Generic Classes: In Java, ageneric class is a class that can work with multiple data types. This allows for flexibility
and reusability in programming.
class SampleClass<T> {
private T data;
public SampleClass(T data) {
this.data = data;
}
p u b l i c v o i d p r i n t D a t a T y p e ( ) {
System.out.println("Type: " + this.data.getClass().getSimpleName());
}
}
c l a s
s
public static void main(String[] args) {
M a i n
SampleClass<Integer> intObj = new SampleClass<>(3);
{
intObj.printDataType(); // Type: Integer
SampleClass<String> stringObj = new SampleClass<>("Java");
stringObj.printDataType(); // Type: String
}
}
Generic Methods: We can create a method in Java that can be used with any type of data by using generics. This
type of method is known as a generics method.
class SampleClass<T> {
private T data;
public SampleClass(T data) {
this.data = data;
}
public T getData() {
return this.data;
}
}
class Main {
public static void main(String[] args) {
SampleClass<Integer> intObj = new SampleClass<>(3);
System.out.println(intObj.getData()); // 3
SampleClass<String> stringObj = new SampleClass<>("Java");
System.out.println(stringObj.getData()); // Java
}
}
Generic Interfaces: We can create interfaces in Java that can be used with any type of data by using generics.
This type of method is known as a generics interface.
interface Processor<T> {
void process(T t);
} class Main<T> implements Processor<T>
{
public void process(T obj) {
System.out.println("The process() method is called");
}
p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s )
M a i n < S t r i n g > o b j = n e w M a i n < > ( ) ;
o b j . p r o c e s s ( " J a v a " ) ;
}
}
// Output is:
The process() method is called
Bounded Types: We can use bounded types by specifying the upper bound type parameter with the extends
keyword.
The JRE (Java Runtime Environment), JVM (Java Virtual Machine), and JDK (Java Development Kit) are the
components of the Java ecosystem, and they are often used together when developing and running Java
programs.
Java Development Kit (JDK): JDK provides the environment to develop and execute the Java program. It includes
JRE and other development tools like compilers, debugger, etc.
Java Runtime Environment (JRE): JRE is an installation package that provides an environment to only run(not
develop) the Java program onto our machine. It consists of many components like a Java class library, tools, and a
separate JVM.
Java Virtual Machine (JVM): JVM is a software program that allows us to run Java programs on a computer. It is
designed to work on any type of hardware or operating system, so we can run Java programs on any device as long
as it has a JVM installed. This makes Java programs portable, meaning they can be run on any device with a JVM.
Execution of Java Programs: The execution of a Java code series of steps that convert the source code into a form
that the computer can understand and execute.
Source code: All the Java codes we write consists of a .java as the file extension. These Java files act as the source
of our Java codes.
Compilation: We use a command-line tool called javac provided by the JDK, to compile Java programs. The Java
compiler takes the source file as input and produces a compiled file with the .class file extension. This file contains
the Java bytecode.
Interpretation: To execute the bytecode, we use the Java interpreter, which is a command-line tool called java. The
Java interpreter takes the class file as input and runs it on the JVM.
Executing Machine Code: Machine code is a low-level code that is understandable by the machine. It typically
consists of 0's and 1's. The machine code is platform-dependent. The JVM is responsible for creating the machine
code that is understandable for the specific processor or OS.
Java Archive (JAR): JAR is a file format used to package and share Java programs and libraries. It is similar to a ZIP
file but it also includes metadata about the contents of the JAR file, such as the main class of a program, the version
of the Java runtime required to run the program, and any dependencies on other libraries.
Classpath: The classpath specifies the locations where the JVM should look for class files when it is asked to load a
class.