Object References: You use a null reference to indicate that an object variable
ID: 3757788 • Letter: O
Question
Object References: You use a null reference to indicate that an object variable (reference) does not refer to any object. Of course, you cannot invoke methods on a null reference since there is no object to which the method would apply. Consider this program segment:
String str = null; System.out.println(str); str.length(); System.out.println(str);
What problem arises when executing these statements? Do the statements produce any printout beyond an error message? If so, what printout? (2 points)
Explain the difference between (2 points)
new BankAccount(5000); and
BankAccount b;
Explanation / Answer
String str = null ; //no problem will be arised
System.out.println(str);// It will successfully print null
str.length(); //Will result in NullPointerException
System.out.println(str);// It will successfully print null
If you run all of the statements together then program will throw Run Time error with the above mention exception.
new BankAccount(5000); //this is definition of the BankAccount object which has been initialized with value 5000
BankAccount b; // declaration of the b object which is of type BankAccount.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.