Forums Register Login

String variables

+Pie Number of slices to send: Send
when we declare a string variable as
String s = null
then the string referrence variable s does not refer to any String object.
How does the statement s+=s
produce a value of nullnull??
does this mean that when s does not refer to any string object it gets a value of null. but still any other operations on the variable using the dot operator results in nullpointerexception.
+Pie Number of slices to send: Send
A null-pointing String reference when asked to cough up its String value with the + op, returns the literal 'null'. When any method is invoked though, there has to be a valid object to the ref.
+Pie Number of slices to send: Send
This is actually related to the StringBuffer (StringBuilder) class. Normally, when you try to append an object to the string buffer, it will use the toString() method to get the string to append. However, if the object in question is not available (null), the append() method will append the string value of "null".

How is this related? The adding of strings are actually syntactic sugar for append() method calls to string buffers.

Henry
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 734 times.
Similar Threads
Why? null is not a common object,then what it is?
Difference between empty String and null String
while loop
When NullPointerException, when "null"?
What is null ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:29:37.