Are Strings automatically null terminated?

Discussion in 'Java' started by Paul.Lee.1971@gmail.com, Jul 9, 2008.

  1. Guest

    Hi,
    I'm very new to Java and would like to ask a newbie question: are
    strings automatically null terminated? If I invoke
    a class of my own as follows;
    MyCoords location = new MyCoords("21.5 N 57.0", "Boeing 747",
    "S265W");
    are the arguments in parentheses in the constructor automatically
    appended implicitly with \0 ?

    The reason why I ask is that the first argument in the constructor can
    be either a set of points or an area,
    and I'm trying to populate a String array of size [2][2], all
    initialised to 0. If the first argument is a set of points, I need
    only populate
    the first two elements of the array. If not, all 4 must be populated.
    Ideally, I'd loop through the first argument in
    the constructor until \0 is reached, but I don't know if this is added
    to the end.

    Many thanks

    Paul
     
    , Jul 9, 2008
    #1
    1. Advertising

  2. wrote:
    > Hi,
    > I'm very new to Java and would like to ask a newbie question: are
    > strings automatically null terminated? If I invoke
    > a class of my own as follows;
    > MyCoords location = new MyCoords("21.5 N 57.0", "Boeing 747",
    > "S265W");
    > are the arguments in parentheses in the constructor automatically
    > appended implicitly with \0 ?


    No, Java strings are not \0 terminated. Instead, the String object
    remembers the length.

    ....
    > Ideally, I'd loop through the first argument in
    > the constructor until \0 is reached, but I don't know if this is added
    > to the end.


    Use String's length() method to control the loop, instead of looking for
    a termination character. See
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#length()

    Patricia
     
    Patricia Shanahan, Jul 9, 2008
    #2
    1. Advertising

  3. wrote:
    > Hi,
    > I'm very new to Java and would like to ask a newbie question: are
    > strings automatically null terminated? If I invoke
    > a class of my own as follows;
    > MyCoords location = new MyCoords("21.5 N 57.0", "Boeing 747",
    > "S265W");
    > are the arguments in parentheses in the constructor automatically
    > appended implicitly with \0 ?


    No, Java strings are more of a pascal string than a C string: they know
    their own length. In fact, they are also UTF-16 and can have null
    characters within them normally.

    > The reason why I ask is that the first argument in the constructor can
    > be either a set of points or an area,
    > and I'm trying to populate a String array of size [2][2], all
    > initialised to 0. If the first argument is a set of points, I need
    > only populate
    > the first two elements of the array. If not, all 4 must be populated.
    > Ideally, I'd loop through the first argument in
    > the constructor until \0 is reached, but I don't know if this is added
    > to the end.


    I don't know enough about what you're doing, but I'd say that passing
    around String arguments is a bad idea in favor of proper objects, e.g.:

    class Point {
    // stuff
    }
    class Area {
    // stuff
    }

    class MyCoords {
    public MyCoords(Point[] points, String plane, String heading) {
    // stuff
    }
    public MyCoords(Area area, String plane, String heading) {
    // stuff
    }
    }

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Jul 10, 2008
    #3
  4. Mark Rafn Guest

    <> wrote:
    >I'm very new to Java and would like to ask a newbie question: are
    >strings automatically null terminated?


    Nope. Java String is a different beast than a C string.

    >If I invoke
    >MyCoords location = new MyCoords("21.5 N 57.0", "Boeing 747", "S265W");
    >are the arguments in parentheses in the constructor automatically
    >appended implicitly with \0 ?


    No, and they shouldn't be. You could just as well ask "will the arguments be
    automatically appended with the letter Q?". The answer is the same: no, and
    if you add one yourself it will be a literal part of the string, not a
    terminator.

    >The reason why I ask is that the first argument in the constructor can
    >be either a set of points or an area,
    >and I'm trying to populate a String array of size [2][2], all
    >initialised to 0.


    They can be set to null, "0" or "" (the empty string)? A numeric 0 is not
    a String in Java. The default will be that all four elements in the 2x2
    array contain null.

    >If the first argument is a set of points, I need only populate
    >the first two elements of the array. If not, all 4 must be populated.
    >Ideally, I'd loop through the first argument in
    >the constructor until \0 is reached, but I don't know if this is added
    >to the end.


    You are confused. And now so am I! Are you asking if the parameter list is
    null-terminated, or if each string is null-terminated? Both answers are "no",
    by the way.

    And the reason you shouldn't care is that Java arrays and strings know their
    length. If the constructor for MyCoords is a varargs (String... args), then
    "args" will be an array, and you can check its length. If it's actually
    specified (String arg0, String arg1, String arg2), then there _IS_ no arg3 or
    any way to find a null "after" arg2. There's no "after" to look at, the args
    are assigned to the formal parameters by the VM.

    Likewise for String. "S265W".length() tells you there are 5 characters, and
    there _IS NO_ '\0' character in addition. "S265W".charAt(5) will throw
    IndexOutOfBoundsException, not give you a null.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Jul 10, 2008
    #4
  5. Arne Vajhøj Guest

    wrote:
    > I'm very new to Java and would like to ask a newbie question: are
    > strings automatically null terminated? If I invoke
    > a class of my own as follows;
    > MyCoords location = new MyCoords("21.5 N 57.0", "Boeing 747",
    > "S265W");
    > are the arguments in parentheses in the constructor automatically
    > appended implicitly with \0 ?
    >
    > The reason why I ask is that the first argument in the constructor can
    > be either a set of points or an area,
    > and I'm trying to populate a String array of size [2][2], all
    > initialised to 0. If the first argument is a set of points, I need
    > only populate
    > the first two elements of the array. If not, all 4 must be populated.
    > Ideally, I'd loop through the first argument in
    > the constructor until \0 is reached, but I don't know if this is added
    > to the end.


    A Java String is a class that has both data and length. It does not
    use C style zero termination.

    If you pass "" then you can test with if(v.equals("")) or if
    you pass null then you can test with if(v == null).

    Or you could pass a String array and have the constructor
    loop over the array (in Java you can also get the length of an
    array).

    Arne
     
    Arne Vajhøj, Jul 10, 2008
    #5
  6. Mark Space Guest

    wrote:


    > and I'm trying to populate a String array of size [2][2], all
    > initialised to 0. If the first argument is a set of points, I need
    > only populate
    > the first two elements of the array. If not, all 4 must be populated.
    > Ideally, I'd loop through the first argument in



    In addition to what the others said:

    I don't think \0 is a legal character representation in Java. Did you
    try it? It should give an illegal escape sequence error. (I didn't try
    it either, however.) \u0 I think is the null character.

    However, the idea of populating a string array as you mention really
    weirds me out. Java has the capability to overload methods. I think
    you should be using overloaded methods rather than trying to stuff
    different numbers of arguments into an array.


    So for example, in the first case, you take two points:

    void someMethod( Point p1, Point p2 ) ...

    and the second case you need "all 4" so:

    void someMethod( float x1, float y1, float x2, float y2 ) ...

    These two methods can happily co-exist in the same class as either
    static or instance methods, the compiler will figure out which one to
    call based on the number and type of arguments you supply.

    It's hard to tell with out an SSCCE though.

    *cough*

    http://mindprod.com/jgloss/sscce.html
     
    Mark Space, Jul 10, 2008
    #6
  7. Guest

    Thanks for your help everyone, I've realised that I can do everything
    I need to using regular expressions and split(...);
    the ultimate problem I had was that I might have a argument of 41.44 N
    49.57 W 41.46 N 50.14 W
    or 41.44 N 49.57 W

    all I want in my array is 41.44 49.57 41.46 50.14 and 41.44 49.57
    respectively.

    Best wishes

    Paul
     
    , Jul 10, 2008
    #7
  8. Arne Vajhøj Guest

    Mark Space wrote:
    > I don't think \0 is a legal character representation in Java. Did you
    > try it? It should give an illegal escape sequence error. (I didn't try
    > it either, however.) \u0 I think is the null character.


    I believe Java insist on \u0000.

    Arne
     
    Arne Vajhøj, Jul 10, 2008
    #8
  9. Mark Space Guest

    Arne Vajhøj wrote:
    > Mark Space wrote:
    >> I don't think \0 is a legal character representation in Java. Did you
    >> try it? It should give an illegal escape sequence error. (I didn't
    >> try it either, however.) \u0 I think is the null character.

    >
    > I believe Java insist on \u0000.


    Yeah, probably. The OP seems to have fixed his issue, turns out it was
    a parsing problem. Curiouser and curiouser.
     
    Mark Space, Jul 10, 2008
    #9
  10. Guest

    Thanks for your help everyone, I've realised that I can do everything
    I need to using regular expressions and split(...);
    the ultimate problem I had was that I might have a argument of 41.44 N
    49.57 W 41.46 N 50.14 W
    or 41.44 N 49.57 W

    all I want in my array is 41.44 49.57 41.46 50.14 and 41.44 49.57
    respectively.

    Best wishes

    Paul
     
    , Jul 10, 2008
    #10
  11. Arne Vajhøj wrote:
    > Mark Space wrote:
    >> I don't think \0 is a legal character representation in Java. Did you
    >> try it? It should give an illegal escape sequence error. (I didn't
    >> try it either, however.) \u0 I think is the null character.

    >
    > I believe Java insist on \u0000.


    Unicode translation is done before lexical analysis
    and you get a literal ASCII NUL in the String at compile time.

    Causes problems if we were talking about LF or CR.
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4209933

    For characters in the control character range
    prefer the octal escape sequence "\000" through "\37"
    over the unicode encoding "\u0000" through "\u001f".

    Well, I do anyway.
     
    Thomas Schodt, Jul 10, 2008
    #11
  12. Arne Vajhøj Guest

    Thomas Schodt wrote:
    > Arne Vajhøj wrote:
    >> Mark Space wrote:
    >>> I don't think \0 is a legal character representation in Java. Did
    >>> you try it? It should give an illegal escape sequence error. (I
    >>> didn't try it either, however.) \u0 I think is the null character.

    >>
    >> I believe Java insist on \u0000.

    >
    > Unicode translation is done before lexical analysis
    > and you get a literal ASCII NUL in the String at compile time.
    >
    > Causes problems if we were talking about LF or CR.
    > http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4209933
    >
    > For characters in the control character range
    > prefer the octal escape sequence "\000" through "\37"
    > over the unicode encoding "\u0000" through "\u001f".
    >
    > Well, I do anyway.


    I would use \r and \n for CR and LF.

    Arne
     
    Arne Vajhøj, Jul 13, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green
    Replies:
    0
    Views:
    461
    Roedy Green
    Jul 9, 2003
  2. Barry

    strncpy() and null terminated strings

    Barry, Apr 8, 2004, in forum: C Programming
    Replies:
    4
    Views:
    1,141
    Malcolm
    Apr 8, 2004
  3. Roy Smith
    Replies:
    2
    Views:
    1,910
    Peter Otten
    Mar 6, 2004
  4. Matt Helm
    Replies:
    5
    Views:
    1,600
    Matt Helm
    Feb 6, 2006
  5. ssylee
    Replies:
    4
    Views:
    505
    CBFalconer
    Aug 12, 2008
Loading...

Share This Page