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 / September 2006

Tip: Looking for answers? Try searching our database.

new byte always initialized to 0?

Thread view: 
34stunts@gmail.com - 19 Sep 2006 23:57 GMT
Consider the following code:
byte b[] = new byte[34];
for (int x = 0; x < b.length; x++)
{
    System.out.println((int) b[x] );
}

Java compiles it and runs it. This prints a bunch of "0".

So my question is this correct code? Or should/must I have a loop to
assign it to 0. Is it a rule that a newly created byte is 0 or is this
merly a coincidence?

-
Fred
Jim Korman - 20 Sep 2006 02:11 GMT
>Consider the following code:
>byte b[] = new byte[34];
[quoted text clipped - 11 lines]
>-
>Fred

<quote>
Each class variable, instance variable, or array component is
initialized with a default value when it is created
</quote>

http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5

Jim
Chris Uppal - 20 Sep 2006 09:46 GMT
> So my question is this correct code? Or should/must I have a loop to
> assign it to 0. Is it a rule that a newly created byte is 0 or is this
> merly a coincidence?

It is a rule, part of the JLS (Java Language Specification).  In Java it is
/never/ possible to "see" uninitialised memory.

   -- chris


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.