What is the passed to args argument?
Consider the following program
MyJvm.java
MyJvm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| public class MyJvm{ public static void main(String args[]) { try { //in case main() throws any exception ArgsDemo.main(new String[]{}); } catch(Exception e) { e.printStackTrace(); } } }class ArgsDemo { public static void main(String args[]) { System.out.println(args); System.out.println(args.length); String arr[]=null; System.out.println(arr); }} |
<strong>Output:</strong> [Ljava.lang.String;@110b053 0 null
Explaination:-
When you dont pass anything as command line argument,JVM internally creates a new String[] object,and that object is passed to our main(),
As far as array elements are concerned,nothing is passed to array elements,so the args.length gives you zero.
Had JVM had passed null,args.length would have given NullPointerException
When you dont pass anything as command line argument,JVM internally creates a new String[] object,and that object is passed to our main(),
As far as array elements are concerned,nothing is passed to array elements,so the args.length gives you zero.
Had JVM had passed null,args.length would have given NullPointerException
No comments:
Post a Comment