• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

String variables

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic