• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Passing a Method as an Argument

 
Ranch Hand
Posts: 53
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a Coding problem from an Online IDE :

Given a string, reverse defined segments and return the new string.

EG : Given Input s1 = "abcd\nefgh\nijkl\nmnop", Return output : s2 = "dcba\nhgfe\nlkji\nponm"

The Question itself is easy, but I cant figure out the default code in the IDE :



And the test case for this code :



I cant understand the lines :

or


Why would anyone try to pass the method to be tested, along with the parameter that the same Method is going to use for testing to a second completely unrelated method.
 
Marshal
Posts: 76453
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method reference is one way to pass a λ expression. Are you not familiar with method references? If you go through the Java™ Tutorials, you will find out that you are not passing the method (as you might in C), but that is part of the way you can define an object that uses that method.

Adding discussion to what used to be the Java8 forum.
 
Kevin Mckeon
Ranch Hand
Posts: 53
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The method reference is one way to pass a λ expression. Are you not familiar with method references? If you go through the Java™ Tutorials, you will find out that you are not passing the method (as you might in C), but that is part of the way you can define an object that uses that method.

Adding discussion to what used to be the Java8 forum.



Is there a way of doing this without using λ expression ?

Ive tried to pass/call the method by :




And by changing the method Declaration :









But No luck
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can pass an instance of an object implementing some interface (Function, UnaryOperator).
 
Campbell Ritchie
Marshal
Posts: 76453
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin Mckeon wrote:. . . Is there a way of doing this without using λ expression ? . . .

Most probably yes. Let's start with something simpler that I can actually understand
I presume you have worked out how a method reference is a sort of cut‑down λ from the Java™ Tutorials link I gave you yesterday.

Let's imagine you have a λ all of what it does being to call a method. Let's make it a one‑parameter method add(int) returning an int.Pretty useless except that you can easily understand the problem. Let's have a method foo() taking an IntUnaryOperator as a parameter. The old way you could have created such an object would be with an anonymous class (see the same Java™ Tutorials section):-As you will have seeen from the link, and you could have guessed because it is in the java.base/java.util.function package, IntUnaryOperator is a functional interface, so you can reduce that anonymous class to a λ:-I presume you are familiar with the process of converting an anonymous class to a λ; I have written about it several times before. All you are doing in that λ is calling a method. If you are using a Stream<Integer> or an IntStream, that will provide the argument i, and you will have a reference to myUslessInstance already, which constitutes the first argument. (If that reference is a local variable/method parameter, it must be effectively final.) So all you are doing is to pass the i to a method and collect the result. You do that by retaining the two unknown parts of the λ, namely the object the method is called on and the method name: myUslessInstance::add. No () and the dot is changed to :: and Bob's your uncle.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aside the lambda problem. The other issue is how to break multi-line string. There is new feature in java 11, I think.
In the above code, the int i should be inside bracket (int i)
 
Campbell Ritchie
Marshal
Posts: 76453
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kiros haregewoine wrote:Aside the lambda problem. The other issue is how to break multi-line string. There is new feature in java 11, I think.

I can't see any multiline Strings in the current threa‍d. There have been several attempts to write multiline text, I think in Java12/13, but I am not sure that the final version has been implemented yet.

In the above code, the int i should be inside bracket (int i)

Where is there an i missing ()?
 
kiros haregewoine
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I misinterpreted string with multiple line terminators with multi-lines. I saw java 11 String has got lines()  method which changes to collection of strings.
About bracket:
foo(int i -> myUslessInstance.add(i))
should be changed to
for ((int i) ->myUselessInstance.add(i))
 
Campbell Ritchie
Marshal
Posts: 76453
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kiros haregewoine wrote: . . .
foo(int i -> myUslessInstance.add(i))
should be changed to
for ((int i) ->myUselessInstance.add(i))

Yes, you are right there. Sorry for my mistake. It would probably run happily without the type:- foo(i -> myUslessInstance.add(i)) The String#lines() method returns a Stream<String> handling the separate lines the String can be divided into (I got an empty Stream from an empty String).
 
Saloon Keeper
Posts: 26042
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Passing pointers to functions was a "C" thing. It was much more important before object-oriented programming, although it's still used in C. C++ as well, but as I said, you can do much the same in C++ using virtual object functions.

Java is not a pointer-based environment, so the traditional equivalent was the callback. You'd pass an interface to a method and the method would callback a method defined in the interface.

A much messier way would be to use introspection on an object and wiggle out the method to call from there.

But note that in all cases, you're going to be referencing a class or an instance of a class. Java calls them "methods" and not "procedures" or "functions", because Java isn't merely object-oriented, it's object-based. You cannot have free-floating code in Java as it was originally designed.

With the advent of lambdas recently, more options have been added and one of the other kludges (anonymous inner classes) that were used to "call methods as arguments" is no longer as necessary. But that's about as far as it goes.
 
Campbell Ritchie
Marshal
Posts: 76453
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim is right to call introspection messy. Avoid it if at all possible.
Even if you write a method reference or a λ, you are still creating an object. It is an object of some class or other implementing a particular interface. The λ or method reference code is incorporated into that object. So there is still no free‑floating code, and Java® remains object based, as Tim says.
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic