The program will throw a NullPointerException
(and usually stop running).
Notice that there is no object because the reference variable contains null
.
Since there is no object, there is no length()
to run.
substring()
There are two versions of substring
:
substring( int from ) |
Create a new object that contains the characters of the method's string
from index from to the end of the string. |
Throws an IndexOutOfBoundsException if from is negative
or larger than the length of the string. |
substring( int from, int to ) |
Create a new object that contains the characters of the method's string
from index from to index to-1 . |
Throws an IndexOutOfBoundsException if from is negative
or if from is larger than to . |
There are some tricky rules about the first version of the method:
from
is exactly equal to the length of the original string, a substring is created, but it
contains no characters (it is an empty string).from
is greater than the length of the original string,
or a negative value, a
IndexOutOfBoundsException
is thrown (and for now, your program crashes).What do the following statements create?