• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Absence of NULL in string ?

 
maninder pal singh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please justify why null character < as a termination signal>is absent in String in Java where it is present in other languages like C / C++ ?
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because java.lang.String has other ways of specifying where it ends. The length() method is the main one.

The reason C uses the NULL character* is because there is no other way to indicate how large an array is. Arrays in C have no hard bounds like arrays in Java have. You can easily try to read "elements" beyond its border. These are usually the cause for many (security) bugs in programs (buffer overruns). Java has solved this using the bounds checking (with an ArrayIndexOutOfBounds being thrown if the index is invalid) and the length field all arrays have.


* The NULL character is actually still available in Java. It's the character '\0', or (char)0. It just isn't necessary for ending strings.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

The C programming language does not even have a real string data type. It only has characters (the char data type) and to represent strings, arrays of characters are used. The convention in C is that a string (actually a character array) ends with a null character to indicate the end of the string.

The way that strings are represented in C is not the only possible way that you can represent strings. Different programming languages, such as Java, have a different way to deal with strings. There's really no reason to assume that in Java it needs to be the same as in C.

I remember from long ago, when I was programming in Pascal that it was different there too: strings were not null-terminated; instead the first byte of the string contained the length of the string, and after that were the N bytes containing the characters of the string.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49797
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob has hinted how Strings are implemented in Java. There are three well-known ways to implement Strings:
  • 1. as in Pascal, starting with a count byte, which is sometimes called a "Pascal String."
  • 2. as in C, the null-terminated String
  • 3. as in Java.
  • Java maintains an array of chars, each representing a character, and the array has a length field, which tells the string class how many letters/characters there are.

    You will find a bit more in this Joel Spolsky article.

    [There are some characters which are represented by two chars in Java, but let's forget about them for now.]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic