Java program question!
If I pass null to a method, how can I use the passed value to compare.
Here is example code, it might be wrong, please make it could run on eclipse.
Public class hw{
public static void main (String args[]) {
method1();
}
private static void method1() {
System.out.println(method(null)); }
public static String method(int[] a) { return null; }
What I want to get in the output is just "()".
It is not just to type the string.
I want the program could check if the passed value is null.
I tried if a == null. It is not working.
Could you please tell me what is the mistake I made?
//do comment if any problem arises
//So you want () as output if method receives null and "" ie empty
string if argument is not null
class hw {
public static void main(String args[]) {
method1();
}
private static void method1() {
System.out.println(method(null));
int[] array=new int[1];
System.out.println(method(array));
}
public static String method(int[] a) {
if (a == null)
return "()";
else
return "";
}
}
Output:
Here in first function call the argument is null and in second function call argument is integer array of size 1
So in null output is () and second output is "" ie empty string
Get Answers For Free
Most questions answered within 1 hours.