Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

String from char[] with null termination.

Thread view: 
Kevin Podsiadlik - 12 Jan 2006 19:53 GMT
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?
Chris Smith - 12 Jan 2006 20:08 GMT
> I'm relatively new to Java programming, and stumbled across this by
> accident.  Consider this code fragment:
[quoted text clipped - 11 lines]
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 12 Jan 2006 21:47 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.