What Does the Arrow Operator, '->', Do in Java

What does the arrow operator, '- ', do in Java?

That's part of the syntax of the new lambda expressions, to be introduced in Java 8. There are a couple of online tutorials to get the hang of it, here's a link to one. Basically, the -> separates the parameters (left-side) from the implementation (right side).

The general syntax for using lambda expressions is

(Parameters) -> { Body } where the -> separates parameters and lambda expression body.

The parameters are enclosed in parentheses which is the same way as for methods and the lambda expression body is a block of code enclosed in braces.

Java weird operator ()- meaning and String operations

Suppose you have an interface that declares one method:

public static interface MyFunctionalInterface {
void m1();
}

And you have a method that receives an object of that type as a parameter:

public void method(MyFunctionalInterface i) { ... }

You can implement that interface and use it immediately using anonymous inner classes like this:

method( new MyFunctionalInterface() {
public void m1() {
System.out.println("Hello");
}
});

In Java 8 you can replace that with a lambda expression such as the one you showed:

method( () -> System.out.println("Hello"); );

The empty parameters represent the m1() method, with no parameters.

Suppose the functional interface you were using had a method with one parameter (if your method had the form method2(ActionListener s) { ... } for example), then you would use:

method2( e -> System.out.println("Hello"); );

which would be the same as doing this:

method2( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello");
}
});

There are many tutorials about Lambda expressions in Java 8. This one is a good quick-start.

Meaning of java operator -

This is for a lambda expression, a language feature first introduced in Java 8. Basically this is an inline anonymous function that takes container as a parameter. Usually lambdas return values but here it looks like it is just carrying out the "side effect" of calling addErrorPages to container. There is no type specified for container as Java intuits it from the context.

Lambda expressions are more than a language feature, they are also a whole area of computer science and functional programming. A good SO post describing them is here.

How does the arrow operator work internally in java 8?

When you have an -> the javac compiler adds a static method with the contents of the code. It also adds dynamic call side information to the class so the JVM can map the interface the lambda implements to the arguments and return type. The JVM generates code at runtime to bind the interface to the generated method.

A difference with lambdas and anonymous classes is that implict variables only need to be effectively final (as in could have been made final) and member variables are copied i.e. it doesn't keep a reference to the this of an outer class.

It can tell the difference between Runnable and Callable<void> even though both take no arguments. For more details http://vanillajava.blogspot.com/2014/09/lambdas-and-side-effects.html

What does the arrow - operator do in Ballerina?

The -> operator in Ballerina represents a remote interaction. According to the language specification:

A remote-method-call-action is depicted as a horizontal arrow from the
worker lifeline to the client object lifeline.

remote-method-call-action := expression -> method-name ( arg-list )

Please refer to the "Remote interaction" section of the language specification below for more information.

https://ballerina.io/spec/lang/2019R3/#section_7.9

What does the arrow (- ) operator do in Kotlin?

The -> is part of Kotlin's syntax (similar to Java's lambda expressions syntax) and can be used in 3 contexts:

  • when expressions where it separates "matching/condition" part from "result/execution" block

     val greet = when(args[0]) {
    "Apple", "Orange" -> "fruit"
    is Number -> "How many?"
    else -> "hi!"
    }
  • lambda expressions where it separates parameters from function body

      val lambda = { a:String -> "hi!" }
    items.filter { element -> element == "search" }
  • function types where it separates parameters types from result type e.g. comparator

      fun <T> sort(comparator:(T,T) -> Int){
    }

Details about Kotlin grammar are in the documentation in particular:

  • functionType
  • functionLiteral
  • whenEntry

What is the meaning of this - symbol in Java?

The -> is used to denote Lambda Expressions, which where introduced in Java 8, thus, it will not compile against Java 7.

Taken from here:

Lambda Expressions, a new language feature, has been introduced in
this release. They enable you to treat functionality as a method
argument, or code as data. Lambda expressions let you express
instances of single-method interfaces (referred to as functional
interfaces) more compactly.

To compile (against previous versions of Java 8), you would need to rewrite the code. Otherwise you would need to compile against Java 8.

Arrow (- ) operator precedence/priority is lowest, or priority of assignment/combined assignment is lowest?

Note the sentence preceding the quoted JLS text:

Precedence among operators is managed by a hierarchy of grammar productions.

The grammar of the Java language determines which constructs are possible and implicitly, the operator precedence.

Even the princeton table you’ve linked states:

There is no explicit operator precedence table in the Java Language Specification. Different tables on the web and in textbooks disagree in some minor ways.

So, the grammar of the Java language doesn’t allow lambda expressions to the left of an assignment operator and likewise, doesn’t allow assignments to the left of the ->. So there’s no ambiguity between these operators possible and the precedence rule, though explicitly stated in the JLS, becomes meaningless.

This allows to compile, e.g. such a gem, without ambiguity:

static Consumer<String> C;
static String S;
public static void main(String[] args)
{
Runnable r;
r = () -> C = s -> S = s;
}

How do I use the - (arrow) operator?

If something is not supported (yet) in jOOQ, you can always resort to using the plain SQL API. In your case, you could write a utility like:

public static Field<String> jsonAttr(Field<?> json, String attrName) {
return DSL.field("{0}->{1}", String.class, json, DSL.inline(attrName));
}

Note that DSL.inline() will escape your string literal to prevent SQL injection. Always be careful of this possibility with the plain SQL API

You can now use the above as such:

// Assuming this static import
import static org.jooq.impl.DSL.*;

// Aliasing
Users u = USERS.as("u");

// You didn't qualify all your columns, so I'm assuming DATA is in LOG
DSL.using(configuration)
.select(
coalesce(u.NAME, jsonAttr(LOG.DATA, "username")).as("username"),
u.USER_LOCATION,
u.USER_TIME)
.from(LOG)
.leftJoin(u)
.on(jsonAttr(LOG.DATA, "username").isNotNull())
.and(jsonAttr(LOG.DATA, "username").eq(u.USERNAME))
.where(jsonAttr(LOG.DATA, "code").isNull())
.or(jsonAttr(LOG.DATA, "code").ne("no"))
.fetch();


Related Topics



Leave a reply



Submit