public class Test { public static void main(String[] args) { String s1 = "hello"; String s2 = new String("hello"); s2 = s2.intern(); System.out.println(s1 == s2); } } output: true Explanation: We know that the intern() method will return the String object reference from the string pool since we assign it back to s2 and now both s1 and s2 are having the same reference. It means that s1 and s2 references point to the same object.
public class Test { public void print(Integer i) { System.out.println("Integer"); } public void print(int i) { System.out.println("int"); } public void print(long i) { System.out.println("long"); } public static void main(String args[]) { Test test = new Test(); test.print(10); } } output: int Explanation: If Integer and long types are specified, a literal will match to int. So, the program prints int.
Using the name of the interface.
Example:-
interface Vehicle { static void blowHorn() { System.out.println("Blowing horn!!!"); } } class Car implements Vehicle { public void print() { Vehicle.blowHorn(); } }
Using the super keyword along with the interface name.
Example:-
interface Vehicle { default void print() { System.out.println("I am a vehicle!"); } } class Car implements Vehicle { public void print() { Vehicle.super.print(); } }
A Static Method is a Utility method or Helper method, which is associated with a class (or interface). It is not associated with any object.
We need Static Methods because of the following reasons:
public interface Vehicle { String getBrand(); String speedUp(); String slowDown(); default String turnAlarmOn() { return "Turning the vehice alarm on."; } default String turnAlarmOff() { return "Turning the vehicle alarm off."; } }