-2

What is exactly an arbitrary object of a particular type? I didn't understand the part of code (String::compareToIgnoreCase), How did we made this reference?

String[] stringArray = { "Barbara", "James", "Mary","John","Patricia", "Robert"}; 

Arrays.sort(stringArray, String::compareToIgnoreCase);
Ouiame
  • 21
  • 5
  • 1
    I'm having trouble understanding. What exactly are you asking? Do you have code samples which could help to illustrate the issue? – knittl Mar 01 '20 at 13:34
  • 1
    I added a code for clarification. – Ouiame Mar 01 '20 at 13:47
  • 1
    @Ouiame To get more people really spending time on your questions, you can eliminate things like background (why you're asking) or polite thank you, and please, etc... people just want to help clear succinct problems which just have the minimum essential data needed to solve it, without reading more than they need to. I'll try to edit it to discourage downvotes. – clearlight Mar 01 '20 at 13:55
  • 1
    I took your suggestions into consideration. – Ouiame Mar 01 '20 at 14:21
  • Thanks, I posted a compilable example you can experiment with. Please up vote if it helps you and click the Accept checkmark if it solves your problem.. – clearlight Mar 01 '20 at 14:24
  • 1
    I was confused by the initial write-up so missed the lambda thing. (A Java 8 feature I haven't used yet because I'm working more in C++ and the Linux kernel at the moment). So I didn't recognize what you were doing so gave you the basic Java lesson :-) That's why it's important to ask very very clearly and specifically and succinctly as possible. – clearlight Mar 01 '20 at 14:33
  • 1
    @clearlight you're right, you thought I had a problem with the basics of java but it wasn't the case. I won't make the same mistake again, thank you for your time. – Ouiame Mar 01 '20 at 14:40

2 Answers2

2

Method references are a feature introduced with Java 8. They are related to lambda expressions. Before Java 8, lamdas were not available as a language feature and if you wanted to pass a method/function to another method, you had to emulate it by passing either a named class instance or an anonymous class instance.

Semantically, these three statements are equivalent:

Arrays.sort(stringArray, String::compareToIgnoreCase);
Arrays.sort(stringArray, (s1, s2) -> s1.compareToIgnoreCase(s2));
Arrays.sort(stringArray, new Comparator<String>() {
    @Override
    public int compare(final String s1, final String s2) {
        return s1.compareToIgnoreCase(s2);
    }
});
knittl
  • 246,190
  • 53
  • 318
  • 364
  • Ah, I misunderstood the question. Thanks. I've used lambdas in Swift and other languages (Javascript? Been awhile), but not in Java (that's been awhile too). Thanks. – clearlight Mar 01 '20 at 14:30
1

By type name, you mean the Class not the Object. That is a fundamental distinction in Java (and Object Oriented Programming in general).

Objects are instantiated from classes. Class-specific definitions apply to the class methods and static data that are always available for that class in general and don't refer to any one particular instance. Class data and methods can be invoked from anywhere in your app, at anytime, by prefixing with the method or data item with the class name itself. Class-specific methods and data are easily recognized by the static keyword in front of them in the source code, meaning they never change. They are the same forever in each instance.

You'll get a compile time error if you try to invoke a class-specific method or access class-specific data without the class name prefix.

Whereas Objects are class instances. Objects are created when you use the new keyword to instantiate a class. That gives you a reference to a new object instance. With that reference, whether you assign it to a variable or not, you can reference all the non-static (non-class level) data and methods.

public class Foo {
     public  static int myStaticVarPublic  = 1;
     private static int myStaticVarPrivate = 2;

     public  int myInstanceVarPublic;
     private int myInstanceVarPrivate;

     public Foo() {
         myInstanceVarPublic  = 3;
         myInstanceVarPrivate = 4;
     }

     int getMyInstanceVarPrivate() {
        return myInstanceVarPrivate;
     }

     static int getMyStaticVarPrivate() {
        return myStaticVarPrivate;
     } 

     public static void main(String args[]) {

           System.out.println("class-specific value Foo.myStaticVarPrivate = " + 
               Foo.getMyStaticVarPrivate());

           System.out.println("class-specific value Foo.myStaticVarPublic  = " + 
               Foo.myStaticVarPublic);

           System.out.println("object-specific value myInstanceVarPrivate = " + 
               new Foo().getMyInstanceVarPrivate());

           System.out.println("object-specific value myInstanceVarPublic  = " + 
               new Foo().myInstanceVarPublic);

     }
}
clearlight
  • 12,255
  • 11
  • 57
  • 75
  • There are different thoughts about how strict you should be about accessing instance variables directly from the instance reference without going through a 'getter' method (for example how I accessed in the print statement myInstanceVarPublic). Some people say it's okay and there are reasons that's a good model, but other people say never bypass a getter. In the beginning you're probably better of just creating getter methods, because in so many cases you'll want to add some code or debugging or something anytime someone tries to access that instance variable and so a getter method lets you. – clearlight Mar 01 '20 at 14:27