Java – Difference between equals() method and == operator

"==" operator compares 2 objects memory reference. Now, lets have a look at the simple example below:

Example 1:

String str1 = "test";

String str2 = "test";

if (str1 == str2) {
System.out.println("str1 == str2 is TRUE");
} else {
System.out.println("str1 == str2 is FALSE");
}

Output:

str1 == str2 is TRUE

Example 2:

String str1 = new String("test");

String str2 = new String("test");

if (str1 == str2) {
System.out.println("str1 == str2 is TRUE");
} else {
System.out.println("str1 == str2 is FALSE");
}

Output:

str1 == str2 is FALSE

In the code above, Outputs of Example 1 and Example 2 are different. Because, when you create String, JVM searches that literal in String pool, if it matches, same reference will be given to that new String. So, Example 1 output is TRUE.

In Example 2, the output is FALSE. Because, 2 objects refer to different memory location.

Continue reading

Java Method Reference

Method reference is supported in Java 8. A method reference is described using :: (double colon) symbol.

Types of Method Reference
There are four types of method references:

  1. A method reference to a static method.
  2. A method reference to an instance method of an object of a particular type.
  3. A method reference to an instance method of an existing object.
  4. A method reference to a constructor.

Static Method Reference

Notice that below code between System.out and println, the :: operator is used instead of the . operator. And we don't pass arguments to the method reference. So, you can refer to static method defined in the class with interfaces that contain only one abstract method in addition to one or more default or static methods.

Consumer<?> is a functional interface. Lambda expression and Static method references that implement Consumer<String> functional interface are passed to the accept() method to be executed. Actually, Lambda expression  can be replaced with Method reference.

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class Program {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("name1", "name2", "name3", "name4");


        //1. way: using Anonymous Inner Class
        Consumer<String> action1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };

        names.forEach(action1);

        System.out.println("*******************");

        //2. way: using Lambda Expression
        Consumer<String> action2 = (x) -> System.out.println(x);

        names.forEach(action2);

        System.out.println("*******************");


        //3. way: using Static Method Reference
        Consumer<String> action3 = System.out::println;

        names.forEach(action3);

        System.out.println("*******************");


        //4. way: pass Lambda Expression as argument
        names.forEach((x) -> System.out.println(x));


        System.out.println("*******************");


        //5. way: pass Static Method Reference as argument
        names.forEach(System.out::println);

    }
}

Output:

name1
name2
name3
name4
*******************
name1
name2
name3
name4
*******************
name1
name2
name3
name4
*******************
name1
name2
name3
name4

Continue reading