• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Anonymous Classes expressions in Plain English

 
Ranch Hand
Posts: 190
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us consider the following code:



Here is my rudimentary understanding of what this code does:

Hey Mr. Java compiler! Please give me a new object of type "Comparator<String>"
Oh, btw. Mr. Compiler, I will be embedding(?) a method into this object. This method will accept 2 arguments, both of type String.
I will use the Integer class's static method, "compare" to compare these 2 Strings which I pass into this method.
Now, I have this magical comparatorObject object, which can now be used to pass as an argument into methods!

This last part really confuses me. Traditionally, a method takes  parameters which are of a particular data type. The code within the method *does something* with that passed argument.
However, in the case of anonymous classes, is it a true statement that there are certain methods which are now expecting objects, which contain a particular method, which will return some value. Is this a concise statement which explains precisely what an anonymous class containing a method is doing? or am I missing something?
 
Marshal
Posts: 66635
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure I understand your last question.
Integer#compare() takes two int parameters (or unboxed Integers). You have a method String#length() which returns an int, so you can write myString.length() anywhere you are allowed an int.
 
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I don’t think that’s a true statement. An anonymous class is a convenient way to create a new object without having to declare a class first. You might want to do this for at least a couple of reasons:

1. The object you’re creating has limited use/scope.
2. The functionality the object provides is specific to the context in which it is being created.

Comparator<String> is an interface that contains one method, int compare(String, String), which is why you have to provide your implementation in the anonymous class declaration.

The comparator you’re creating there has a compare() implementation that compares string lengths. This comparator could be used to sort a list of Strings by length instead of their natural dictionary order sorting. See if you can understand what this does:

Using Integer.compare() in both these examples is also just a convenience. Without it, you’d have to do something like this:


 
Campbell Ritchie
Marshal
Posts: 66635
251
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . Integer.compare() in both these examples is also just a convenience. . . .

But the correct solution would be much more complicated for doubles, so you really want to use Double#compare(), which deals with all the corner cases and pitfalls for you.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . Integer.compare() in both these examples is also just a convenience. . . .

But the correct solution would be much more complicated for doubles, so you really want to use Double#compare(), which deals with all the corner cases and pitfalls for you.


Sure, if you're dealing with double values. In the OP's case, you're comparing string lengths, which are int values. Just want to be clear that you're not implying that the correct solution for OP's case is to use Double.compare().
 
Campbell Ritchie
Marshal
Posts: 66635
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . .you're not implying that the correct solution for OP's case is to use Double.compare().

No, I'm not, but it would work via widening conversions.
 
Sheriff
Posts: 24761
59
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want an even more convenient way to write that code as a Comparator<String>, consider this equivalent code:



I just used that style for the first time yesterday when rewriting one of my Comparators -- actually my new code also uses the thenComparing() method and the reversed method, which makes the whole mess actually readable and understandable.
 
Mark Richardson
Ranch Hand
Posts: 190
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote: See if you can understand what this does:


Yes, I believe I understand both of these implementations. Here's my understanding:

In the first one, we created an object which we assign the reference variable: byLength.
This object is of type Comparator<String>, therefore, it must override the compare method because
the Comparator interface contains the compare method.
Our custom implementation of the compare method will leverage the static method of the Integer class and compare
the lengths of the 2 strings we pass to it.

Similarly, in the second example, we will compare based on the hashCode of the String.

What eludes me is what happens to these the objects when it comes to actually using them. i.e. passing "byLength" and "byHashCode" as arguments.
In other words let's assume there exists a method called "someMethod()." How will this hypothetical method actually utilize "byLength" as an argument?

e.g.

What will the declaration of this "someMethod" look like?
 
Saloon Keeper
Posts: 10875
235
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Such a method would declare that it needs a Comparator, and it would call the compare() method in its body:
 
Always! Wait. Never. Shut up. Look at this tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!