[HD Pg 0, Sec. 5.2.2 - stringbuilder-api]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
nk2164
Posts: 21
Joined: Mon Sep 30, 2019 9:48 pm
Contact:

[HD Pg 0, Sec. 5.2.2 - stringbuilder-api]

Post by nk2164 »

About this point :

If you pass a null, the string "null" is appended to or inserted in the existing StringBuilder. No NullPointerException is thrown.
=============

Code: Select all

		StringBuilder str = new StringBuilder("Hello");
		str.append(null);
Failed with "The method append(Object) is ambiguous for the type StringBuilder."

============
Then i tried this one .

Code: Select all

		String str1 = null;
		
		StringBuilder str = new StringBuilder("Hello ");
		str.append(str1);
		
		System.out.println(str);
=======================
And it printed: Hello null
======================

Is it because if i directly pass a null , it has no way of knowing what the type of object being passed is ?

admin
Site Admin
Posts: 10062
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 0, Sec. 5.2.2 - stringbuilder-api]

Post by admin »

Yes, it should say, "if you pass a variable that is null" and not just a null literal for the reason that you mentioned. Passing null literal would be ambiguous.
Added to errata. Thank you for your feedback!
If you like our products and services, please help us by posting your review here.

nk2164
Posts: 21
Joined: Mon Sep 30, 2019 9:48 pm
Contact:

Re: [HD Pg 0, Sec. 5.2.2 - stringbuilder-api]

Post by nk2164 »

Thank you

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 87 guests