JavaRush /Java Blog /Random EN /Coffee break #105. Why are Strings immutable in Java? Int...

Coffee break #105. Why are Strings immutable in Java? Interface in Java and multiple inheritance

Published in the Random EN group

Why are Strings immutable in Java?

Source: Dev.to Before understanding why Strings are immutable in Java, we need to think about why we make something immutable? Coffee break #105.  Why are Strings immutable in Java?  Interface in Java and multiple inheritance - 1Immutable means that once created we cannot change it. The only reason we can think of making something immutable is for synchronization when used together. This is the reason why strings are immutable. In Java, String objects are shared and cached in the String Pool. It is a specific location on the heap where strings are stored and shared among multiple threads if they have the same value. For example: in a string pool, if there is already a string with the value “test” and the program wants to create another string object with the same value, then it will get the same reference instead of creating a new string object. Now we know how strings are stored on the heap. Let's see why they are unchanged.
  1. The first reason for immutability is thread safety. Since the rows are shared among multiple threads in the row pool, we need to restrict any random thread to change it. Any change to a row can affect other threads accessing the same row. If a thread wants to update the value of a row, it needs to create another row and reference it.

  2. Typically we use String as the key in Map . If strings were mutable, anyone could change the value of the strings and we would lose the actual key.

Interface in Java and multiple inheritance

Source: Dev.to Let's understand what multiple inheritance is. Multiple inheritance is a feature of some object-oriented programming languages ​​in which an object or class can inherit functionality from more than one parent object or parent class. Coffee break #105.  Why are Strings immutable in Java?  Interface in Java and multiple inheritance - 1As shown in the above image, a multiple inheritance child class can have two or more base classes, but to achieve this we need an interface.

Interface

An interface in Java is the blueprint of a class. It has static constants and abstract methods. That is, an interface can only contain abstract methods and variables, it cannot have a method body. It cannot be created in the same way as an abstract class.

So where do we declare the body of these methods?

The body of the method is declared inside the class where the method is needed according to the requirements of the programmer.

How to declare an interface?

An interface can be declared using the interface keyword . Syntax:
interface interface_name {abstract methods}

Note

In order to use a declared interface in a class, we must use the implements keyword .

Implementation?

First we'll create a print interface , and inside it we'll create an abstract print() method ;
interface printgib{
void print();
}
Now we have an interface ready for classes to use, so let's create classes abc and gk and implement the interface in them.
public class abc implements printgib{
public void print(){                     //1st implementation of print
System.out.println("I love you 3000");
}
public static void main(String[] args){
abc obj = new abc();
gk obj1 = new gk();
obj.print();
obj1.print();
}
}

class gk implements printgib{
public void print(){                   //2nd implementation of print
System.out.println("I am Gk");
}
}
As shown in the above code, we have achieved multiple inheritance and implemented an interface. Now to run the code, save the file and...
javac file_name.java
java abc

Result:

Coffee break #105.  Why are Strings immutable in Java?  Interface in Java and multiple inheritance - 2
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION