File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Absence of NULL in string ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Absence of NULL in string ?" Watch "Absence of NULL in string ?" New topic

Absence of NULL in string ?

maninder pal singh

Joined: May 04, 2010
Posts: 1
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++ ?

Maninder Pal Singh
Rob Spoor

Joined: Oct 27, 2005
Posts: 20275

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.

How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
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.]
    I agree. Here's the link:
    subject: Absence of NULL in string ?
    It's not a secret anymore!