This question already has answers here:
Java Pass Method as Parameter
(17 answers)
Closed 4 years ago.
I'm new to Java and I'm confused about actual parameters in Java. Since it's just parameters, I assume that literals, constants,variables, expressions can be actual parameters during function calling.
But I'm confused about a function call. Is it possible to have a function call being an actual parameter in Java?
If your question is "are functions first-class citizens in Java, as they are in C" then the question is "no, not really".
But with Java 8's lambdas, you can get pretty close:
import java.util.function.IntFunction;
public class Main {
public static void main(String[] args) {
final IntFunction square = x -> x * x;
printFunctionResult(square, 2);
}
public static void printFunctionResult(final IntFunction function, final int value) {
System.out.println(function.apply(value));
}
}
Related
This question already has answers here:
What is variable shadowing used for in a Java class?
(5 answers)
Is "this" shadowing a good idea?
(10 answers)
Closed 3 years ago.
It seems like its using local variable of the method!!!! I thought it would give an error on call non static variable on static method but it did not.
public class foo{
int x = 12;
public static void go(final int x){
System.out.println(x);
}
}
Actually it doesn't have a error which is interesting..
System.out.println(x)
will print the parameter of the method go
If you want to access field of the class, use this keyworld (if your method not static):
System.out.println(this.x)
In your case, you need to have an instance of foo class and use
foo f = new foo();
System.out.println(f.x);
This question already has answers here:
How do I pass a primitive data type by reference?
(8 answers)
Closed 4 years ago.
I have the following code
private static void modifyX(int x) {
if (x!=0) {
modifyX(--x);
}
}
I want the value of my variable to be updated after the recursive call, so I wrote the following code:
public static void main(String... args) {
int x = 5;
modifyX(x);
System.out.println("Modified value:\t" + x);
}
However, the value remains the same (5).
Why is my variable not updating?
You are not passing the same instance of the value 5. Instead, the JVM is creating a new int with a value of 5, and passing that to your method.
See this thread for more information.
This question already has answers here:
Why does the compiler prefer an int overload to a varargs char overload for a char?
(4 answers)
Closed 6 years ago.
Today I was studying for an incoming Java exam and I ran into this question:
Let A be a class defined as follows:
class A {
public void f(Double x) { System.out.println("A.f(Double)"); }
public void f(double... x) { System.out.println("A.f(double...)"); }
}
What is the output produced by the instruction A a = new A(); a.f(1.0);?
The answer seems to be A.f(Double) but I can't understand why. Could someone give me a proper explanation?
Overload resolution always favours a function with an explicit number of arguments over a function with a variable argument list, even if that means that 1.0 is auto-boxed.
In a little more detail, a function is chosen with this precedence according to JLS 15.12.2:
Type widening
Auto-boxing
Variable arguments
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I'm learning Java, and from what I learned, is that you need to specify the value that a function return.. If it doesn't return a value than it is void..
However in the below program, I'm able to change the values of an array from a void function. Can anybody explain this to me please?
public class ArraysInMethods {
public static void main(String args[]){
int rd[] = {2,3,4,5,6};
change(rd);
for(int y: rd){
System.out.println(y);
}
}
public static void change(int x[]){
for(int counter = 0; counter<x.length;counter++){
x[counter]+=5;
}
}
}
I'm learning Java, and from what I learned, is that you need to specify the value that a function return.
That is correct only for methods returning values, i.e. methods other than void. These methods define expressions, while void methods define statements.
Calling your change method is a statement, in the sense that it lacks return values. It does not mean, however, that it cannot change the state of your running program - for example, give different values to variables.
However in the below program, I'm able to change the values of an array from a void function.
You are not returning a value from your void function; all you do is modifying the array in place. This is allowed, because arrays are passed by reference.
This question already has answers here:
How does Java choose which constructor to use?
(2 answers)
Closed 6 years ago.
I have a Java class where there are two parameterized constructors
public class TestApplication {
TestApplication(Object o)
{
System.out.println("Object");
}
TestApplication(double[] darray)
{
System.out.println("Array");
}
public static void main(String[] args)
{
new TestApplication(null);
}
}
When I run this program I get output as "Array". Why does the Object constructor not run?
Constructor overloading resolution behaves the same as method overloading resolution. When two constructors match the passed parameter, the one with the more specific argument types is chosen.
null can be assigned to both a double[] and an Object, since both are reference types, but an array is a more specific type than Object (since arrays are sub-classes of the Object class), so new TestApplication(null) triggers the TestApplication(double[] darray) constructor.