Re: String from char[] with null termination.



Kevin Podsiadlik <kjp@xxxxxxxxx> 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
.



Relevant Pages

  • Re: How do you inherit DOS console in Win32 application?
    ... > console in a Win32 assembly console application? ... ByVal lpFile As String, ByVal lpParameters As String, _ ... If your app is calling the command prompt, ... Dim hWnd As Long, iDummy As Long, sCmd As String ...
    (alt.lang.asm)
  • Re: Finding the source files in the binary
    ... Suppose if I have used the command some thing like below:- ... And what if this string is optimised away? ... if a compiler would _not_ optimise such a string away if it ...
    (comp.lang.c)
  • Re: Finding the source files in the binary
    ... Suppose if I have used the command some thing like below:- ... And what if this string is optimised away? ... if a compiler would _not_ optimise such a string away if it ...
    (comp.lang.c)
  • Re: pesky Pointers !!
    ... |> has to look like you pass to systemis dependent on the command shell ... |> interpreting that string. ... compiler does not support the former, ... As Karl pointed out, the 'system' function is standard, just not ...
    (alt.comp.lang.learn.c-cpp)
  • Event Messages arent being directed to the console port
    ... In addition, I can type an event message at the command line, and the Console Works application, then gets the string and alarms on it. ...
    (Tru64-UNIX-Managers)