Can't call createTempDirectory in Clojure [duplicate] - java

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.

Related

Why and how does this strange method return type compile? [duplicate]

This question already has answers here:
Strange array return type
(6 answers)
Java function definition with brackets syntax [duplicate]
(1 answer)
Weird "[]" after Java method signature
(4 answers)
Closed 3 years ago.
The following method has a return type of int[][]. Note how one of the []'s are on the right hand side of the ()'s.
Why does this work and what is this behavior called?
int[] numbers () [] {
return null;
}
From the JLS
The declaration of a method that returns an array is allowed to place some or all of the bracket pairs that denote the array type after the formal parameter list. This syntax is supported for compatibility with early versions of the Java programming language. It is very strongly recommended that this syntax is not used in new code.

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?

Java - Passing and executing a function [duplicate]

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

Java / Get all arguments in reflection [duplicate]

This question already has answers here:
Can I obtain method parameter name using Java reflection?
(15 answers)
Closed 9 years ago.
I search way to get all the names of a function in java. for example:
public String doSomething(String user, boolean tree, String[] arr){
// HERE I want to get all the names of the params, meaning: user, tree and arr, and do something with them
}
How can I do it?
You can, but only if you have debugging information in the code. The names of these variables exist mainly for you, not for the processor.
Details here:
Can I obtain method parameter name using Java reflection?

Categories

Resources