Java-libraries:
[Link] package
1. Object class
It is super most class in java.
Object class it is an inbuild class which is present inside [Link] package.
The fully qualified name of object class is [Link].
Object class it is a concrete class.
We can create an object for object class.
Example:
class Test {
private static void main(String[] args)
{
Object obj = new Object();
[Link](obj);
}
}
• Object class consist of following methods
1. public String toString()
2. public native int hashCode()
3. public boolean equals(Object o)
4. protected void finalize()
5. public final void wait(long l,int i)
6. public final void wait()
7. public final native void wait(long l)
8. public final native Class getClass()
9. protected native Object clone()
pg. 1
10. public final native void notify()
11. public final native void notifyAll()
1. public String toString()
• it is used to convert the address of the object into String format
• toString() it a non-Static method which is present Inside the object class
• toString() is called automatically with the help of object reference variable
• it returns string representation of the object
• in the below format
FULLYQUALIFIEDCLASSNAME@HEXADECIMALVALUEOFHAS HCODE.
example:
public class Demo
{
public static void main (String[] args)
{
Demo obj = new Demo();
[Link](obj);// Demo@515f550a
[Link]([Link]());// Demo@515f550a
}
}
pg. 2
Important questions:
Can we override toString () of object class?
Yes, we can override to String method of object class
Why should we override toString()?
By overriding we can create our own address or we can fetch states of the object
Below example where we are not
example:
public class Student
{
int roll_number;
String name;
public Student(String name, int roll_number)
{
this.roll_number = roll_number;
[Link] = name;
}
public static void main(String[] args)
{
Student student = new Student("Chotu", 1);
[Link](student);// Student@515f550a
[Link]([Link]());// Student@515f550a
}
}
pg. 3
overriding toString() of object class
Below example where we are overriding to string method of Object class
public class Student
{
int roll_number;
String name;
public Student(String name, int roll_number)
{
this.roll_number = roll_number;
[Link] = name;
}
public String toString()
{
return "[ name : " + name + ", roll_number" + roll_number + "]";
}
public static void main(String[] args)
{
Student student = new Student("Chotu", 1);
[Link](student);// [ name : Chotu, roll_number1]
[Link]([Link]());// [ name : Chotu, roll_number1]
}
pg. 4
2. hashCode() :
public int hashCode()
hashCode() it is used to convert address in to int format.
the return type of hashCode() is in int format.
hashCode() does not get automatically called.
we need to call hashCode() with the help of object reference variable.
Example 1:
public class Test
{
public static void main(String[] args)
{
Test test1 = new Test();
[Link]([Link]());// 1973538135
Test test2 = new Test();
[Link]([Link]());// 1365202186
}
}
In the above program we are creating two different object both the objects will have different
address so after converting address in to int format with the help of hashCode method
definitely both the int data will be in different format.
pg. 5
Example 2
public class Test
{
public static void main(String[] args)
{
Test test1 = new Test();
Test test2 = test1;
[Link]([Link]());// 1973538135
[Link]([Link]());// 1973538135
}
}
In the above program we are creating only a single object but we are creating two reference
variables both the variables is pointing to same address (both the reference variables are
holding same address)
If we are converting that address present in the both the reference variables in to int format
definitely we will have same converted int data
Example 3
public class Test
{
public int hashCode()
{
return 100;
}
public static void main(String[] args)
{
Test name = new Test();
[Link]([Link]());// 100
}
}
pg. 6
In the above program we are overriding method of the Object Class hashCode in order to
generate our own hashCode.
3. Equals Method
public Boolean equals (Object o);
• It is used to compare the address of two different object
• It is a non-static method which is present inside Object class
• We can call equals method with the help of object reference variable
• The return type of equals method is Boolean (true /false)
Example:
public class Test
{
String s;
public Test(String s)
{
super();
this.s = s;
}
public static void main(String[] args)
{
Test obj = new Test("hello");
Test obj1 = new Test("hello");
[Link](obj == obj1); // compare reference false
[Link]([Link](obj1)); // compare reference false
}
}
pg. 7
In the above program we are trying to compare properties of the object/states but with the
help of == equality operator we are comparing address/reference of the object and if we are
using equals method of the object that method is also compares reference/address of the
object to compare states/properties of the object we have to override equals method of object
class below is the example
Rules to override equals()
1. Upcasting.
2. DownCasting.
3. Comparision logic.
Example:
public class Test
{
String s;
public Test(String s)
{
super();
this.s = s;
}
public boolean equals(Object o)
{ // upcasting
Test test=(Test)o;
//downcasting
if (this.s == test.s)
{
return true;
}
return false;
}
pg. 8
public static void main(String[] args)
{
Test obj = new Test("hello");
Test obj1 = new Test("hello");
[Link](obj == obj1);
[Link]([Link](obj1));
}
}
In the above program we have created two different objects here states/properties of the
object is same and with the help of == equality operator we are comparing address/reference
of the object as we the help of new keyword every time we are creating a new object in heap
area those objects will have different address/reference so address are different so output of
== equality operator is false and in next line we are using equals method which is overridden
in such a way that it will compare states of the object so the output of equals method is true
because both the objects are having same properties/states.
pg. 9
Singleton class:
It is a design pattern where in we can create only a single instance or a single object for a
class.
Rules in order to develop singleton class/singleton design pattern
1. Declare a private constructor.
2. Have a public static helper/factory method to create a single object.
3. Declare a private static non-primitive reference variable.
Ex:
class Account
{
private static Account obj=null;
private Account()
{
[Link]("Object created");
}
public static void craeteObject()
{
if(obj==null)
{
obj= new Account();
}
else
{
[Link]("cannot create Object");
}
}
}
pg. 10
public class User {
public static void main(String[] args)
{
[Link]();//Object created
[Link]();//cannot create object
[Link]();//cannot create object
}
}
Ex:
class Marriage
{
int age=29;
private static Marriage m=null;
private Marriage()
{
[Link]("Got Married");
}
public static Marriage getInstance()
{
if(m==null)
{
m= new Marriage();
}
return m;
}
}
pg. 11
public class Solution
{
public static void main(String[] args)
{
Marriage obj = [Link]();// Got Married
[Link](“at the age of ”+[Link]);// at the age of 29
}
}
pg. 12
String:
• String is concrete class in java
• String is present in [Link] package
• Fully qualified name for String is [Link]
• We can create an object for string class
• Definition:
The String class in Java is part of the [Link] package and represents a sequence of
characters. It is one of the most commonly used classes in Java for handling text and
character data.
Types of String:
2 types
1) Immutable String
2) Mutable String
1) Immutable: Strings in Java are immutable, which means that once a String object is
created, its content cannot be changed. Any operation that appears to modify a String actually
creates a new String object.
• We can create an object for String class in two ways
using literal
using new keyword
pg. 13
example:
public class Practice
{
public static void main(String[] args)
{
String s1 = "hello";// Object Creation using assignment
// operator by passing literal
String s2 = new String("hello");
// object creation by using new keyword
}
}
• String Pool: Java maintains a special memory area called the "string pool" for string literals.
When you create a string using a literal, Java checks if an equivalent string already exists in
the pool. If it does, the new variable points to the existingstring, improving memory
efficiency.
example:
public class Practice
{
public static void main(String[] args)
{
String s1 = "hello";//s1 is pointing to object present in String Constant pool
String s2 = "hello";//s2 is pointing to same object present in String Constant pool
[Link](s1 == s2);// both variables are having same address they are equal
//output is true
}
}
pg. 14
• Constructors: The String class provides several constructors to create string objects,
including those that accept character arrays, bytes, and other strings as input.
Example 1: constructor accepts String
public class Practice
{
public static void main(String[] args)
{
String s = "hello";
String a = s + "bye"; // a new object in heap with changes is created
[Link](a);// output is hellobye
}
}
public class Practice
{
public static void main(String[] args)
{
char[] c = { 'a', 'b', 'c' };
String s = new String(c);// it will convert all characters in to String
[Link](s);// output : abc
}
}
pg. 15
• Length: You can obtain the length (number of characters) of a string using the length()
method:
public class Practice
{
public static void main(String[] args)
{
String s = "hello";
[Link]([Link]());// length method will count number of characters
//present in the String
}
}
• Character Access: You can access individual characters of a string using the
charAt()method. Character index start at 0:
public class Practice1
{
public static void main(String[] args)
{
String s = "hello";
char c = [Link](0);// at 0 index position we have character as h
[Link](c);// output is h
}
}
pg. 16
• Concatenation: You can concatenate strings using the +operator or the concat() method:
Using + operator
public class Practice2
{
public static void main(String[] args)
{
String s = "hello";
String a = s + "bye"; // a new object in heap with changes is created
[Link](a);// output is hellobye
}
}
Using concat() method
public class Practice
{
public static void main(String[] args)
{
String s = "hello";
String a = [Link]("bye"); // a new object in heap with changes is created
[Link](a);// output is hellobye
}
}
pg. 17
• Substring: You can extract substrings from a string using the substring()
method: Substring (beginning index);
public class Practice
{
public static void main(String[] args)
{
String s = "hello";
String a = [Link](2); // a new object in heap with changes is created,
[Link](a);// output is llo
}
}
public class Practice
{
public static void main(String[] args)
{
String s = "hello";
String a = [Link](2, 4); // a new object in heap with changes is created,
[Link](a);// output is ll
}
}
pg. 18
• Comparison: You can compare strings for equality using the equals() method or the
==operator. The equals()method compares the content of the strings, while ==checks if they
reference the same object in memory.
public class Practice
{
public static void main(String[] args)
{
String s1 = "hello";
[Link]([Link]("hello"));//it is overridden
//it will compare content/states
}
}
• Case Conversion: You can convert the case of a string using methods like
toUpperCase() and toLowerCase():
toLowerCase()
public class Practice
{
public static void main(String[] args)
{
String s1 = "HELLO";
String s2 = [Link]();
[Link](s2);// all characters will be converted to lowercase
}
}
pg. 19
toUpperCase()
public class Practice
{
public static void main(String[] args)
{
String s1 = "hello";
String s2 = [Link]();
[Link](s2);// all characters will be converted to uppercase
}
}
• Searching and Replacing: You can search for substrings and perform replacements using
methods like
indexOf(), lastIndexOf()
public class Practice
{
public static void main(String[] args)
{
String s1 = "hello";
[Link]([Link]("l"));
// it will provide me first occurance index of element
[Link]([Link]("l"));
// it will give me last occurance index of element
}
}
pg. 20
• Splitting: You can split a string into an array of substrings based on a delimiter using the
split() method
public class Practice
{
public static void main(String[] args)
{
String s1 = "hello my name is chotu";
String[] words = [Link](" "); //split method will split string in to arrays from
//given string
for (int i = 0; i < [Link]; i++)
{
[Link](words[i]);
}
}
}
Important
String provides us Security.
Security: Immutable strings can be more secure because sensitive data stored in strings, like
passwords, cannot be easily modified.
1. length(): Returns the length (number of characters) of the string.
2. charAt(int index): Returns the character at the specified index.
3. substring(int startIndex): Returns a substring starting from the specified index.
4. substring(int startIndex, int endIndex): Returns a substring from the start index to the
end index (exclusive).
5. concat(String str): Concatenates the specified string to the end of the current string.
6. indexOf(String str): Returns the index of the first occurrence of the specified string, or
-1 if not found.
pg. 21
7. lastIndexOf(String str): Returns the index of the last occurrence of the specified
string, or -1 if not found.
8. equals(Object obj): Compares the current string with another object for equality.
9. equalsIgnoreCase(String str): Compares the current string with another string,
ignoring case.
10. contains(CharSequence sequence): Checks if the string contains the specified
sequence of characters.
11. isEmpty(): Checks if the string is empty.
12. toCharArray(): Converts the string to a character array.
13. toUpperCase(): Converts the string to uppercase.
14. toLowerCase(): Converts the string to lowercase.
15. trim(): Removes leading and trailing white spaces from the string.
2) Mutable String:
All those String who’s value can be changed even after initialization is refered to as mutable
String.
StringBuilder and StringBuffer, which are mutable, you also have methods for modifying the
content of the string:
1. append(): Appends various data types to the end of the current sequence.
public class P1
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("hello");
[Link](“hi”);
[Link](sb);// hellohi
pg. 22
[Link]('s');
[Link](sb);// hellohis
[Link]("NA");
[Link](sb);// hellohisNA
[Link](10.25);
[Link](sb);// hellohisNA10.25
[Link](10);
[Link](sb);// hellohisNA10.2510
char[] a = { 'a', 'b' };
[Link](a);
}
}
1. insert(): Inserts various data types at a specified index within the current sequence.
public class P1
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("hello");
[Link](1, false);
[Link](sb); // hfalseello
}
}
pg. 23
2. delete(int startIndex, int endIndex): Removes characters from the string starting at the start
Index up to, but not including, the end Index.
public class P1
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("hello");
[Link](0, 2);
[Link](sb);// llo
}
}
2. deleteCharAt(int index): Removes the character at the specified index.
public class P1
{
public static void main(String[] args)
{
StringBuffer sb=new StringBuffer("hello");
[Link](0);
[Link](sb);// ello
}
}
pg. 24
3. reverse(): Reverses the characters in the string.
public class P1
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("hello");
[Link]();
[Link](sb);// olleh
}
}
pg. 25
Important:
Difference Between String, StringBuffer and StringBuilder
String StringBuffer StringBuilder
Pre-defined final class present Pre-defined final class present Pre-defined final class
in [Link] package. in [Link] package. present in [Link]
package.
Presented in JDK 1.0 version. Presented in JDK 1.0 version. Presented in JDK 1.5
version.
Immutable in nature. mutable in nature. mutable in nature.
Thread Safe Thread Safe Not Thread Safe
String class has over ridden 3 StringBuffer has Overriden StringBuilder has
methods only toString() Overriden only toString()
1)toString()
2)hashCode()
3)equals()
String Objects can be created StringBuffer Objects can be StringBuilder Objects can
with or without new operator. created only using new operator be created only using new
operator
pg. 26
pg. 27