For this question, int score2[] = null, but score.length will not compile because of NullPointerException. Compare to (args.length = 0) in main method if no arguments, it is different. Does args.length is a special case? Except args case, all other normal object, if they are equal to null, the object.length does not work. Is it right?
Thank you.
Updated:
Oh, String [] is defined as [0] by default? If it is defined as [0] by default, I see the difference.
About Question enthuware.ocajp.i.v7.2.1382 :
Moderator: admin
-
- Posts: 9
- Joined: Fri Jun 26, 2015 11:15 am
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1382 :
This is a very wrong thing to say. Exceptions are thrown an run time, not at compile time. So your statement, " int score2[] = null, but score.length will not compile because of NullPointerException." makes no sense.evafang2008 wrote:For this question, int score2[] = null, but score.length will not compile because of NullPointerException.
int score2[] = null; will certainly compile fine. score2.length will throw a NPE at run time.
No, args is not a special case, args.length does not throw NPE because the JVM initializes args to a String array of length 0 if no arguments are passed. So args is not null (unlike above).Compare to (args.length = 0) in main method if no arguments, it is different. Does args.length is a special case? Except args case, all other normal object, if they are equal to null, the object.length does not work. Is it right?
Thank you.
Not sure what you mean. args is a method parameter. It is passed to the main method by the JVM. The JVM initializes it to String[] of length 0 if no arguments were specified on the command line.Updated:
Oh, String [] is defined as [0] by default? If it is defined as [0] by default, I see the difference.
If you define String[] score = null; then score is null. It is not same as String[] of length 0.
I would suggest you to write some test programs to make sure you understand this concept.
HTH,
Paul.
Who is online
Users browsing this forum: No registered users and 7 guests