• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Absence of NULL in string ?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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++ ?
 
Sheriff
Posts: 22800
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.]
     
    My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic