String from char[] with null termination.

Discussion in 'Java' started by Kevin Podsiadlik, Jan 12, 2006.

  1. I'm relatively new to Java programming, and stumbled across this by
    accident. Consider this code fragment:

    char[] ch = { 'A', 'B', 'C', '\0' };
    String s = new String(ch);
    System.out.println(s + "DEF");

    Should this print out "ABC" or "ABCDEF"?

    When I run it from the command line, the output is "ABCDEF". When I
    run it via the Eclipse 3.1.0 SDK, however, the output is "ABC". The
    java compiler is the same both times, version j2sdk1.4.2_10 on a Red
    Hat Linux system. I can understand how either output might logically
    result, but not how different results can come from the same compiler.
    How might that be happening?
    Kevin Podsiadlik, Jan 12, 2006
    #1
    1. Advertising

  2. Kevin Podsiadlik

    Chris Smith Guest

    Kevin Podsiadlik <> wrote:
    > I'm relatively new to Java programming, and stumbled across this by
    > accident. Consider this code fragment:
    >
    > char[] ch = { 'A', 'B', 'C', '\0' };
    > String s = new String(ch);
    > System.out.println(s + "DEF");
    >
    > Should this print out "ABC" or "ABCDEF"?
    >
    > When I run it from the command line, the output is "ABCDEF". When I
    > run it via the Eclipse 3.1.0 SDK, however, the output is "ABC". The
    > java compiler is the same both times, version j2sdk1.4.2_10 on a Red
    > Hat Linux system. I can understand how either output might logically
    > result, but not how different results can come from the same compiler.
    > How might that be happening?


    It's not. The two compilers are outputting the same or equivalent code.
    What you're seeing is different consoles. The actual result is
    "ABC\0DEF". When you test the code from the command line, the console
    handles the unprintable character by simply omitting it. Eclipse's
    console view handles it by considering it the end of the string and not
    printing any more.

    There exists no Java specification that defines how the console should
    behave in response to bytes sent to stdout. Therefore, either is
    equally correct.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jan 12, 2006
    #2
    1. Advertising

  3. Kevin Podsiadlik

    Roedy Green Guest

    On 12 Jan 2006 11:53:36 -0800, "Kevin Podsiadlik" <>
    wrote, quoted or indirectly quoted someone who said :

    >When I run it from the command line, the output is "ABCDEF". When I
    >run it via the Eclipse 3.1.0 SDK, however, the output is "ABC". The
    >java compiler is the same both times, version j2sdk1.4.2_10 on a Red
    >Hat Linux system. I can understand how either output might logically
    >result, but not how different results can come from the same compiler.
    >How might that be happening?


    My guess is Java is sending ABC0DEF to the OS to display. It is the
    OS that is being fooled by the 0, since it is written in C.

    Whenever you send non-printing chars to the console, all bets are off
    on what it will do.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 12, 2006
    #3
    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. Replies:
    5
    Views:
    26,616
    Mike Schilling
    Mar 29, 2006
  2. wwj
    Replies:
    7
    Views:
    549
  3. wwj
    Replies:
    24
    Views:
    2,507
    Mike Wahler
    Nov 7, 2003
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
  5. Lauri Alanko

    snprintf without null-termination?

    Lauri Alanko, Jun 22, 2011, in forum: C Programming
    Replies:
    11
    Views:
    1,741
    Joe Pfeiffer
    Jun 23, 2011
Loading...

Share This Page