Java Notes
[Link] – Library for all things.
Access Modifier:
Modifier Package Subclass World
public Yes Yes Yes
protected Yes Yes No
Default (no modifier) Yes No No
private No No No
Feature Static Method Instance Method
Association Class-level Object-level
[Link]
Invocation [Link]()
me()
Access to Variables Only static variables and methods Both instance and static variables
Overriding Not applicable (can be hidden) Can be overridden
Dependency Does not depend on object state Depends on object state
Usage Context Utility or shared functionality Object-specific behavior
Methods type:
1. Static Method
2. Instance Method
3. Predefined Methods
Calling Predefined Method:
Method hashCode() in Java - > hashCode Methods Link
Static methods consume memory in the method area, and their memory usage is independent
of the number of objects created.
Instance methods also reside in the method area, but their execution depends on an object,
with memory allocated in the heap (for instance data) and stack (for execution context).
Static variables and their values (primitives or references) defined in the class are stored in PermGen
space of memory.
Static Object Creation
Behind the Class and object Creation inside JVM, JRE, JDK
JDK: To compile the Java code we need tool that is JDK. Convert Java Code into Byte Code.
JVM: Java Virtual Machine. Compile the code with help of JDK and run the code on JVM.
Stack And Heap Memory in Java
Every Methods has its own stack.
String Class:
Methods:
1. Using Literal (Static Memory)
eg. String name= “Rishi”;
2. Using new Keyword (Heap Memory)
Object Class Methods : The Object class provides multiple methods which are as follows:
1. toString() method: The toString() method returns a string representation of the object. By default, it
returns the class name followed by the "@" character and the object's hashcode in hexadecimal.
Eg.
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
Output:
Person{name='Alice', age=30}
2. hashCode() method: The hashCode() method returns an integer hash code value for the object. It's
used in hashing-based collections like HashMap and HashSet.
Eg.
String name;
Person(String name) {
[Link] = name;
}
@Override
public int hashCode() {
return [Link]() + age;
}
Output:
63383733
3. equals(Object obj) method: The equals() method checks if two objects are equal. By default, it
compares memory addresses.
Eg.
class Person {
String name;
int age;
Person(String name, int age) {
[Link] = name;
[Link] = age;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != [Link]()) return false;
Person person = (Person) obj;
return age == [Link] && [Link]([Link]);
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person("Alice", 30);
Person person2 = new Person("Alice", 30);
[Link]([Link](person2));
}
}
Output:
true
4. finalize() method: The finalize() method is called by the garbage collector before an object is
destroyed.
Eg.
class Resource {
@Override
protected void finalize() throws Throwable {
[Link]("Resource is being garbage collected");
}
}
public class Main {
public static void main(String[] args) {
Resource resource = new Resource();
resource = null;
[Link](); // Suggests garbage collection
}
}
Output:
Resource is being garbage collected
5. getClass() method: The getClass() method returns the runtime class of the object. It's useful for
reflection and obtaining metadata about the class.
Eg.
class Person {
String name;
Person(String name) {
[Link] = name;
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice");
[Link]([Link]().getName());
}
}
Output:
Person
6. clone() method: The clone() method creates and returns a copy of the object. To use it, the class
must implement the Cloneable interface, and the clone() method should be overridden.
Eg.
class Person implements Cloneable {
String name;
int age;
Person(String name, int age) {
[Link] = name;
[Link] = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return [Link]();
}
}
public class Main {
public static void main(String[] args) {
try {
Person person1 = new Person("Alice", 30);
Person person2 = (Person) [Link]();
[Link](person1 == person2); // false
[Link]([Link](person2)); // true
} catch (CloneNotSupportedException e) {
[Link]();
}
}
}
Output:
false
true
7. wait()
8. notify()
9. notifyAll() (Concurrency methods)
These methods are used for thread communication in synchronized contexts. They must be called
within synchronized blocks or methods.
Static Method:
A static method can created using the static keyword. It can be called without creating an object of the
class, referenced by the class name itself or reference to the Object of that class.
When we are talking about non -primitive data type we have concept of upcasting and downcasting
condition for upcasting and downcasting:
-- for that two class should have some parents child relationship
-- if non-primitive data type have no any relationship so, upcasting and downcasting is not possible.
class A{
public void show1(){
[Link]("In show A)
}
}
class B extends A{
public void show2(){
[Link]("In show B");
}
}
class Demo{
public static void main(String []args){
A obj =(A) new B(); //upcasting
obj.show1();
A obj1 =new B(); // upcasting
// obj1.show2(); // you get error and you are not able to call show2() because with parennts reference
specialised method of child is not visible
here we downcastig to use show2() method
((B)obj).show2(); //downcasting
}
}
Threads State:
Eg:
class GFG implements Runnable
{
public void run()
{
// implementing try-catch Block to set sleep state
// for inactive thread
try {
[Link](102);
} catch (InterruptedException i1) {
[Link]();
}
[Link]("The state for t1 after it invoked join method() on thread t2"
+ " " + [Link]());
// implementing try-catch block
try {
[Link](202);
} catch (InterruptedException i2) {
[Link]();
}
}
}
// Creation of ThreadState class
public class ThreadState implements Runnable
{
// t1 static to access it in other classes
public static Thread t1;
public static ThreadState o1;
public void run() {
GFG gfg = new GFG();
Thread t2 = new Thread(gfg);
// Thread is created and its in new state
[Link]();
// Now t2 is moved to runnable state
[Link]("state of t2 Thread, post-calling of start() method is" " " + [Link]());
[Link]("State of Thread t2 after invoking to method sleep() is" " " + [Link]());
try {
[Link]();
[Link]("State of Thread t2 after join() is " + [Link]());
}
catch (InterruptedException i3) {
[Link]();
}
[Link]("state of Thread t1 after completing the execution is" " " + [Link]());
}
public static void main(String args[]){
o1 = new ThreadState();
t1 = new Thread(o1);
[Link]("post-spanning, state of t1 is " + [Link]());
// lets invoke start() method on t1
[Link]();
// Now, Thread t1 is moved to runnable state
[Link]("post invoking of start() method, state of t1 is"
+ " " + [Link]());
}
}