0

Does Java have a :: operator? Please, do not close this question, I did search the docs and I'm sure it does not but I want to be completely sure.

I.e can there be something like MyClass::x or anything visually resembling that in Java.

7
  • But, dear sir, does it or does it not though? Commented Dec 13, 2013 at 9:36
  • 1
    Short answer, yes. In Java 8. Commented Dec 13, 2013 at 9:36
  • "In Java 8". So right now, no it doesn't. Commented Dec 13, 2013 at 9:38
  • Why are you asking this? It seems like a strange thing to ask; I've seen "what is this operator" questions and "what operator do I use" questions, but not "does any operator look like this". Commented Dec 13, 2013 at 9:38
  • 1
    stackoverflow.com/questions/20001427/… Commented Dec 13, 2013 at 9:39

4 Answers 4

7

In Java 8 the operator :: has been introduced as a way to refer to a method. Basically, it turns the method into a first-class object, something you can pass to other methods as an argument, store in a variable, and return from a method.

This addition to the syntax is a part of the overall orientation towards the Functional Programming paradigm which is being introduced with Java 8. The elementary feature of FP are higher-order functions—such functions which accept other functions as argument, or return functions. This paradigm allows one to eliminate much boilerplate which now pervades the Java source code.

Sign up to request clarification or add additional context in comments.

Comments

4

until and including Java 7: No!

Java 8: Yes!

simply as that.

Comments

0

In Java 8 it allows for referencing of static members of a class, similar to PHP.

public class YourClass {

    public static int comparer(String one, String two){
        return one.length() - two.length();
    }

    public static void main(String[] args) {
        Arrays.sort(args, YourClass::comparer);
        //args are now sorted
    } 

}

As stated in comments, this is Java 8 (and later) only. JDK 7 and below does not have this.

2 Comments

I'm pretty sure that's not how you use it. There's already YourClass.doSomething() for that.
@MarkoTopolnik and user2357112 You are both correct, that was my own mistake having not used it yet myself. I'll update the answer
-1

Upto Java 7 there is no double colon operator(::) in java as in C++. But Java 8 introduce double colon operator which is used to refer the methods.

Example(For Static Method)

public class TestClass {

    public void functionTest() {...}

}

We can call function 'functionTest()' by using double colon operator(::).

TestClass t=new TestClass();
t::functionTest

If 'functionTest()' is static then we can refer directly by using class name

TestClass::functionTest

There are four kinds of method references(as written in java doc)

For more information refer java doc

3 Comments

The documentation you refer to contradicts your answer, which is because your answer is wrong. So you could learn something by reading the very document you are referring OP to.
that was my mistake...now i think this is correct answer
It is still wrong because the term "escape resolution" has nothing to do with Java---or with C++. BTW the downvote is not mine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.