Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "String[]::new" mean?

I'm learning how to use stream, and I get a problem with this method.

public static String[] inArray(String[] array1, String[] array2) {
   return Arrays.stream(array1)
  .filter(str -> Arrays.stream(array2).anyMatch(s -> s.contains(str)))
  .distinct().sorted().toArray(**String[]::new**);
}

I'm so confused about String[]::new, could you give me a hint?

like image 592
xshe2 Avatar asked Jun 01 '17 14:06

xshe2


People also ask

Why do we use new in String?

If we create a String using new(), then a new object is created in the heap memory even if that value is already present in the heap memory.

What is the difference between String and new String?

String(String original) : Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

What is the difference between creating String as new () and literal?

4. String Literal vs String Object. When we create a String object using the new() operator, it always creates a new object in heap memory. On the other hand, if we create an object using String literal syntax e.g. “Baeldung”, it may return an existing object from the String pool, if it already exists.

How do I return a new String?

There are two methods to return a String in Java: the “System. out. println()” method or the “return” statement.


2 Answers

This is a method reference expression see JLS 15.13. The syntax for method references is:

MethodReference:
    ExpressionName :: [TypeArguments] Identifier
    Primary :: [TypeArguments] Identifier
    ReferenceType :: [TypeArguments] Identifier
    super :: [TypeArguments] Identifier
    TypeName . super :: [TypeArguments] Identifier
    ClassType :: [TypeArguments] new
    ArrayType :: new 

The particular case you are looking at is the last one. In your example, String[] is an ArrayType which means that it consists of a type name followed by one or more [].

There shouldn't be a class named String[] which is very lame and I could not interpret what it is actually meant for.

See above: it is a type specification not a class name. From a syntactic / linguistic perspective, this usage is analogous to:

  Class<?> c = String[].class;

or

  if (a instanceof String[])

or even

  public void myMethod(String[] arg)

(You wouldn't call those "lame" ... would you?)


Now you could have a valid case for saying that it is syntactically unexpected (especially to a pre-Java 8 programmer) to be able to use the new keyword like this. But this unexpected syntax is a consequence of the strong imperative that the designers have to NOT break backwards compatibility when adding new language features to Java. And it is not unintuitive. (At least, I don't think so. When I first saw this construct, is was obvious to me what it meant.)

Now, if they were starting with a clean slate in 2018, a lot of details of the Java language design would be simpler and cleaner. But they don't have the luxury of doing that.

like image 76
Stephen C Avatar answered Sep 24 '22 03:09

Stephen C


I would say the existing answers provide some insight but none of them yet talk about IntFunction<R>.

To add to them explain, what it means in the context of Stream.toArray(String[]::new) is that it represents an IntFunction implementation such as :

new IntFunction<String[]>() {
    @Override
    public String[] apply(int value) {
        return new String[value];
    }
}

where the code creates a newly allocated String[] of size value and produces the array of that size as an output.

like image 42
Naman Avatar answered Sep 24 '22 03:09

Naman