forked from rootusercop/IMP-Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
When is 'return null' valid
84 lines (75 loc) · 3.22 KB
/
When is 'return null' valid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
ANOTHER IMP CONCEPT IN JAVA: When is "return null;" valid ?
Answer:
1. Valid: "return null;" is valid only for complex datatypes and complex datastructures such as Wrapper Classes,
Object, List, Arrays etc.
2. Invalid: "return null;" is invalid for Primitive datatypes such as int,double,char.
3. If the return type is void, the instead of 'return null;' we should always write 'return;'. Otherwise ERROR will be thrown.
The following program clearly displays 2 things.
1. 'return null' is invalid for primitive datatypes.
2. 'string' does not exist. But 'String' wrapper class exist. This means that 'string' is not primitive datatype in Java.
3. If the return type is void, the instead of 'return null;' we should always write 'return;'. Otherwise ERROR will be thrown.
Program:
package forTesting;
import java.util.List;
public class CheckingNullForAllReturnTypes {
public static void main(String[] args) {
System.out.println(returnList());
System.out.println(returnDouble());
System.out.println(returndoubleArray());
System.out.println(returnDoubleArray());
System.out.println(returnObject());
System.out.println(returnString());
System.out.println(returndouble()); // VERY IMP: CANNOT RETURN NULL for Primitive Datatypes
}
public static List<Integer> returnList(){ // CAN RETURN NULL for return type of complex datatype
return null;
}
public static Double returnDouble(){ // CAN RETURN NULL for return type of Wrapper Classes
return null;
}
public static double[] returndoubleArray(){ // CAN RETURN NULL for complex DS such as array of primitive datatype
return null;
}
public static Double[] returnDoubleArray(){ // CAN RETURN NULL for complex DS such as array of Wrapper datatype
return null;
}
public static Object returnObject(){
return null; // CAN RETURN NULL for return type of complex datatype such as Object
}
public static String returnString(){
return null; // CAN RETURN NULL for Wrapper class such as String, since there is no such thing called 'string'
// The primitive datatype of String Wrapper Class is 'char' and NOT 'string'
}
public static double returndouble(){
return null; // CANNOT RETURN NULL for Primitive Datatype
}
/*ANOTHER IMPORTANT THING:
* public static string returnstring(){ // Throws ERROR, since there is no such thing called as 'string'
// since 'string' is not Primitive datatype. 'String' exists which is Wrapper class
// and not 'string'. The primitive type of 'String' Wrapper Class can be said to be 'char'.
// Also the primitive type of 'Character' wrapper class is also 'char'
return null;
}
ONE MORE IMP THING:
public static void returnVoid(){
return null; // This gives ERROR since the return type is void
}
public static void returnVoid(){
return; // This DOES NOT gives ERROR since the return type is void
}
*/
}
/*
*
Output:
null
null
null
null
null
null
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from null to double
at forTesting.CheckingNullForAllReturnTypes.returndouble(CheckingNullForAllReturnTypes.java:36)
at forTesting.CheckingNullForAllReturnTypes.main(CheckingNullForAllReturnTypes.java:13)
*/