• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Strings and StringBuilder

 
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




on line 5, even though sb has nul characters after 12345, 6 is not added at the end of these nul characters, but immediately after 5. so is it replacing the nul character ? what exactly is nul ? when nul is beng counted for the length of the stringbuilder then why isn't it being displayed on line 6 , like how the the blanks are displayed in strings on line 7.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I understand null is the absence of a value, but still a value.  You can decisively check for null, but you cannot decisively check for the absence of value.
In this case the StringBuilder is set to a length of 10 on line 4, but only the first 5 spaces are used. So you end up with a StringBuilder which is half empty.
On line 5 you append the string "6" to the StringBuilder object, which still has 5 empty (null) spaces.

Maybe if you thought of this like an array:
First create an array and set it equal to "12345789"

Next set the size of the array to 5, thus giving you:

Next set the size of the array to 10, thus giving you:

Finally add an value to the first element in the array that doesn't have a value yet:

The size of the array is still 10, but only the first six elements are used.

Two things of note:
StringBuilder will automatically expand to fit it's capacity, it's the values that you are trying to add exceed it's current capacity and as long as your system has enough RAM/memory.
I'm pretty sure that StringBuilder does use an array in the background.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I missed you first line, sorry about that.
Strings which are kind of like StringBuilders but not exactly.
You line 1 has:

You should note that null is not the same as a blank character. A blank character still has a value.
You can test this out if you choose to:


While StringBuilder using an array behind the scenes that array is not final.
String on the other hand uses a final array behind the scenes.

Arrays and final values will most likely be asked about on the exam you plan on taking.
 
Ranch Hand
Posts: 231
12
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The API for StringBuilder's setLength(int newLength) method says:

If the newLength argument is greater than or equal to the current length, sufficient null characters ('\u0000') are appended so that length becomes the newLength argument.


The null character '\u0000' (Java's default value of a char and the Unicode representation of null) is not a printable character i.e. it is a Unicode character that does not have a written symbol - so you cannot print it via System.out.println('\u0000').

Unicode has a bunch of printable and non-printable characters.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lilou Laure wrote:so is it replacing the nul character ? what exactly is nul ? when nul is beng counted for the length of the stringbuilder then why isn't it being displayed on line 6 , like how the the blanks are displayed in strings on line 7.


Daniel already provided a very nice explanation about the null character. Have a cow!

Here's a code snippet which actually proves that the 6 is not added immediately after 5Output: 12345-----6
 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just another example with index...

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic