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 / March 2008

Tip: Looking for answers? Try searching our database.

Arrays and variables

Thread view: 
Francois - 12 Mar 2008 10:09 GMT
Hello all,

Can someone confirm this to me.

-> All the arrays when created are filled with 0, or null. Or is it random
numbers?

Thanks, Francois
Andrea Francia - 12 Mar 2008 11:08 GMT
> Hello all,
>
[quoted text clipped - 4 lines]
>
> Thanks, Francois

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#23658
>Then, if a single DimExpr appears, a single-dimensional array is
>created of the specified length, and each component of the array is
>initialized to its default value (§4.12.5).
Mike Schilling - 12 Mar 2008 23:13 GMT
> Hello all,
>
> Can someone confirm this to me.
>
> -> All the arrays when created are filled with 0, or null. Or is it
> random numbers?

Java (unlike C or C++) never initializes anything to random values.
Array members, like other object fields, are initialized to

0 for numeric types
false for booleans
null for reference types
Mark Space - 13 Mar 2008 00:23 GMT
>> -> All the arrays when created are filled with 0, or null. Or is it
>> random numbers?
[quoted text clipped - 5 lines]
> false for booleans
> null for reference types

Not quite.  Local variables (including local arrays) do not have default
values.  You are required to initialize them yourself.

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

But it's never "random."  Variables will always be initialized somehow
in Java.
Mike Schilling - 13 Mar 2008 00:40 GMT
>>> -> All the arrays when created are filled with 0, or null. Or is
>>> it
[quoted text clipped - 9 lines]
> Not quite.  Local variables (including local arrays) do not have
> default values.  You are required to initialize them yourself.

Local variables are neither array members nor object fields.  There is
no such thing in Java as a "local array".  If you mean something like:

   void method()
   {
       int[] iarr = new int[20];
       ...
   }

the 20 members of iarr are, indeed, all initialized to 0.

> <http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5>
>
> But it's never "random."  Variables will always be initialized
> somehow
> in Java.

That is "Java (unlike C or C++) never initializes anything to random
values."

I won't say that I'm always right, and I welcome corrections when I'm
wrong, but this isn't one of those times.
Mark Space - 13 Mar 2008 00:47 GMT
> I won't say that I'm always right, and I welcome corrections when I'm
> wrong, but this isn't one of those times.

You said "anything."  I'm not correcting you, per se, just clarifying
for other readers who might get confused.
Lew - 13 Mar 2008 01:04 GMT
>> I won't say that I'm always right, and I welcome corrections when I'm
>> wrong, but this isn't one of those times.
>
> You said "anything."  I'm not correcting you, per se, just clarifying
> for other readers who might get confused.

He said,
> Java (unlike C or C++) never initializes anything to random values.

That is truth.  If something is not initialized to a known value, it generates
a compiler error.
<http://java.sun.com/docs/books/jls/third_edition/html/defAssign.html>

You said,
> Local variables (including local arrays) do not have default values.

That is not truth.  Locally declared arrays are initialized to default values.

Signature

Lew

Stefan Ram - 13 Mar 2008 01:03 GMT
>That is "Java (unlike C or C++) never initializes anything
>to random values."

 In Java, in

{ final double d = java.lang.Math.random(); }

 , »d« will be initialized to a random value.

 (Run-time operations, of course, are not done by »Java«,
 but by the JVM.)

 In C, in

{ double d; }

 , »d« will not be initialized to a random value, it will hold
 an /indeterminate/ value (ISO/IEC 9899:1999 (E), 6.7.8, p10).

 An indeterminate value is not a random value. But then,
 actually, there are not random values (is »7« a random value?),
 only random sequences.
Lew - 13 Mar 2008 01:05 GMT
>> That is "Java (unlike C or C++) never initializes anything
>> to random values."
[quoted text clipped - 4 lines]
>
>   , »d« will be initialized to a random value.

Pseudo-random.

Signature

Lew

Mike Schilling - 13 Mar 2008 02:05 GMT
>> That is "Java (unlike C or C++) never initializes anything
>> to random values."
[quoted text clipped - 4 lines]
>
>  , »d« will be initialized to a random value.

Not at all; if you know the algorithm used and the value of its
inouts, you can calculate that value precisely.  It's no more random
than l in

   long l = System.currrentTimeMillis();

>  (Run-time operations, of course, are not done by »Java«,
>  but by the JVM.)
[quoted text clipped - 5 lines]
>  , »d« will not be initialized to a random value, it will hold
>  an /indeterminate/ value (ISO/IEC 9899:1999 (E), 6.7.8, p10).

It's random as far as the programmer is concerned: it could literally
be any bit pattern at all.
Roedy Green - 14 Mar 2008 04:05 GMT
>-> All the arrays when created are filled with 0, or null. Or is it random
>numbers?

see http://mindprod.com/jgloss/initialisation.html
http://mindprod.com/jgloss/array.html

one of the features of Java that initially attracted me to it was the
deterministic initialisation and in general precisely repeatable
results from running a program more than once. I went nearly insane
with C/C++ random/lack of initialisation causing bugs that appeared
and disappeared at random.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Mike Schilling - 14 Mar 2008 04:40 GMT
>> -> All the arrays when created are filled with 0, or null. Or is it
>> random numbers?
[quoted text clipped - 8 lines]
> with C/C++ random/lack of initialisation causing bugs that appeared
> and disappeared at random.

My favorite thing was that running under a debugger often perturbed
them out of existence.


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



©2008 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.