Java - Passing and executing a function [duplicate] - java

This question already has answers here:
Java Pass Method as Parameter
(17 answers)
How to pass a function as a parameter in Java? [duplicate]
(8 answers)
Closed 7 years ago.
Is there a way of passing a function to another function and then executing it?
functionCaller(functionToBeCalled());

In java 8 you can use a method reference or lambda
functionCaller(this::functionToBeCalled);
or
functionCaller(() -> functionToBeCalled());

I don't know if I understand very well your question, but effectively you can call a function in param of another function.
You can do this (I suppose your current language is Java):
// if write(...) and getValue() are static method of Writer class
Writer.write(getValue());
// if write(...) and getValue() can just be used by instanciate an object
Writer writer = new Writer();
String val = writer.getValue();
writer.write(val);
There are basic Java programming lesson.
Thanks

Related

Java 8 method references for multiple statements in streams [duplicate]

This question already has answers here:
Multiple lambda method references
(3 answers)
Closed 5 years ago.
list.stream().forEach(e -> method(e)) can be converted to list.stream().forEach(this::method)
Similarly can we convert list.stream().forEach(e -> { method1(e); method2(e);}); using method references expressions. Big apologies if you don't understand question. I am using mobile app first time.
No you cannot.
The point of Method references in Java is to abstract (syntaxically) a lambda expression. Since forEach consumes a function that takes 1 element of type specified by the parent stream, there is no syntax sugar for double application using method references.
Even I'm not sure that this answer is wanted by you,
How about changing the method to static one in that class?

What is the difference between passing arguments in Java and Python? [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
How do I pass a variable by reference?
(39 answers)
Python and Java parameter passing [duplicate]
(1 answer)
Closed 5 years ago.
I come from Java and C++, and now I'm learning Python with this tutorial.
To the best of my knowledge, in Java arguments are passed by value. It seems that they are passed by reference, because when the argument is an object, we pass the reference of the object. So, if inside the called function we change the object state, the object will result modified even after the function call. But, if inside the function we reassign the function parameter we are changing the referenced object, which means that from that point the function's argument will not be affected anymore.
A simple example:
void foo(List<Integer> l){
l.append(1); //affecting list_argument
l = new ArrayList<int>();
l.append(2); //not affecting list_argument
}
public static void main(String[] args){
List<Integer> list_argument = new ArrayList<Integer>();
foo(list_argument); //passing list_argument's reference by value
//list_argument contains 1 only
}
Now, I'm reading this article about passing arguments in Python. In the article, it refers to a fancy name strategy called "passing by object", but to me it seems exactly the mechanism that I described above.
So, my question is: is there any difference the passing argument strategy between Python and Java?

Store a method's name in a variable and call that method using that variable [duplicate]

This question already has answers here:
How do I invoke a Java method when given the method name as a string?
(23 answers)
Closed 6 years ago.
I want to call a method whose name (with or without parentheses and parameters) is stored in a string variable and call this method using this variable.
Until now I have found that in Java if you do this:
String str = "func();";
System.out.println(str);
This would call method func(). But I don't think this is recommended.
So, my question is:
Is it okay to do this?
Is there any other way to do this?
Thanks.
Method m = YourClass.class.getMethod("methodName", parametersOfYourMethods);

Can't call createTempDirectory in Clojure [duplicate]

This question already has answers here:
How to handle java variable length arguments in clojure?
(3 answers)
Closed 8 years ago.
Code in clojure:
(import '(java.nio.file Files))
(Files/createTempDirectory "Test")
There is error:
CompilerException java.lang.IllegalArgumentException: No matching method: createTempDirectory, compiling:xxxx
But in java's doc http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempDirectory(java.lang.String,%20java.nio.file.attribute.FileAttribute...)
There is an String parameter for createTempDirectory, I'm using java 1.7.0
Try this code:
(java.nio.file.Files/createTempDirectory "Test"
(into-array java.nio.file.attribute.FileAttribute []))
As #ymonad mentioned, you cannot omit the variable argument when calling java method with variable arguments. If you don't want to specify the FileAttribute, just pass the empty array of the type.

Passing params in java by reference [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
How to return multiple objects from a Java method?
(25 answers)
Closed 9 years ago.
I'm a new comer form C#, and I know clearly that "Java is always pass-by-value."
But pass-by-reference is useful when we want to get multiple outputs from one method.
How can we get multiple outputs from one method in java, as in C#.
I know one way to do this -- use a generic wrapper class, and get value from the field.
class Wrapper<T> {
public Wrapper(T value) {
Value = value;
}
public T Value;
}
Is there another way to realize this effect?
No, Java does not have out parameters. You can pass an object reference that the method is to modify to pretend that it has out parameters, but this isn't usually the best design and runs into other issues (multithreading and mutable state for one).
The best way to achieve a method that returns multiple values is to have the method return a type that contains multiple values.
Another way to simulate call by reference in Java is to pass a one-element array as a parameter.

Categories

Resources