I.T. Discussion Community!
-Collapse +Expand
Java
Search Java Group:

Advanced
-Collapse +Expand Java To/From
To/FromCODEGuides
-Collapse +Expand Java Store
PRESTWOODSTORE

Prestwood eMagazine

August Edition
Subscribe now! It's Free!
Enter your email:

   ► MB LobbyJava BoardJava News / Other Topic   Print This     

Arrays in Java

Arrays in Java in Java News / Other topic (part of our Java group).

Quick Search: Arrays Java  
Maureen
-- USA

Can someone please help - I need to find out if arrays in Java of fixed size, with the size determined at:-

Compile time or Run time.....?

 Posted 14 years ago (Thread Starter)
Comment Quote
About Maureen -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.
Old Account!
If this is your account, sign in to activate web presence data (sign in quarterly to keep active). Alternatively, you can subscribe to our monthly eMag with a valid email address.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #11109, 7 replies
Thread Started 6/8/2005 7:18:00 PM
View Counter=6489
Last Reply Posted 6/28/2019 3:45:38 AM)
Location=-- USA 
Joined=18 years ago   MB Posts=3  
Moderator
Mike Prestwood
Prestwood IT
Prestwood IT office in Citrus Heights, CA

Hi Maureen,
Java arrays are not created when you declare them. Arrays declared as members are initialized to null but array references declared in methods are not initialized. Either way, you allocate memory for them at runtime. For example:

int arrSize = 100;
String[] myArray = new String[arrSize];
and
String[] oneDimArray = { "abc","def","xyz" };

--
Mike Prestwood
Prestwood IT Solutions

 Posted 14 years ago
Comment Quote
About Mike Prestwood -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.
About Mike Prestwood

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Web Presence
Facebook, Prestwood IT Facebook page -- fan page. (Visit Me)
Twitter, Follow Prestwood IT on Twitter. (Visit Me)
LinkedIn, Prestwood IT company page on LinkedIn. (Visit Me)
YouTube, Prestwood IT YouTube Channel (Visit My Channel)
Website, My drum website where I sell my drum books. (http://www.play-drums.com)

Post ID #11119 (Level 1.1)  Reply to 11109
Thread Started 6/12/2005 8:45:00 AM
View Counter=2
Last Reply Posted 11/4/2009 9:06:28 AM)
Location=Prestwood IT office in Citrus Heights, CA 
Joined=19 years ago   MB Posts=1410   KB Posts=1805   KB Comments=73   BLOG, Topics=4  
PS_1832
-- --
 Posted 26 months ago
Comment Quote
About PS_1832 -Collapse +Expand
Visit Profile
Membership pending.
Member subscribes to this thread with a verified email.
Member pending approval.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #16691 (Level 2) and Parent is 11109
Reply Posted 6/3/2017 7:15:12 AM
Location=-- -- 
Joined=10 months ago   MB Posts=1  
PS_1832
-- --

The java.util.Arrays <a href="https://www.fjackets.com/">class</a> contains <a href="https://www.fjackets.com/">various</a> static <a href="https://www.fjackets.com/">methods</a> for <a href="https://www.fjackets.com/">sorting</a> and <a href="https://www.fjackets.com/">searching arrays,</a> comparing <a href="https://www.fjackets.com/">arrays, and filling array elements.</a> These methods are <a href="https://www.fjackets.com/">overloaded for all primitive types. Searches</a> the <a href="https://www.fjackets.com/">specified array of Object ( Byte, Int , double, etc.) for the specified value using the binary</a> search <a href="https://www.fjackets.com/">algorithm.</a>

 Posted 26 months ago
Comment Quote
About PS_1832 -Collapse +Expand
Visit Profile
Membership pending.
Member subscribes to this thread with a verified email.
Member pending approval.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #16692 (Level 3) and Parent is 11109
Reply Posted 6/3/2017 7:19:31 AM
Location=-- -- 
Joined=10 months ago   MB Posts=1  
Phil Harbison
Alpharetta, GA USA

Mike is correct in that space for an array is always allocated at runtime. What might be a bit confusing is how and when the size of the array is determined, and when (at runtime) the allocation occurs. First let's take a static example.

public class MyWidget {
  private final static String[] messages = new String[] {
    "message 1",
    "message 2",
    "message 3"
  };
  // ... rest of the class ...
}

In this example, the array messages is allocated immediately after class loading before any instances of MyWidget are created. The array is also initialized, and the size of the array is determined at compile time by the number of initializers present. So the space is allocated at runtime but the amount of space is determined at compile time.

If for some reason mesages was not static (in this case, ill-advised) then the space would be allocated at the time of instantiating an instance of MyWidget, but the size would still be determined at compile time based on the number of initializers.

Suppose we have instance variables with no initializers like:

  private String[] someMessages = new String[SOME_SIZE];
  private String[] otherStuff;

In these examples, someMessages is allocated at the time of instantiation with size SOME_SIZE (known at compile time), but the elements of the array are null; while otherStuff is uninitialized so space will have to be created either in a constructor or instance method.

Finally let's assume we have a method "buildWidget" with a local variable someMessages.

  public MyWidget buildWidget(final int count, ... ) {
    String[] moreMessages = new String[count];
    // ...rest of the method...
  }

In this case, the array moreMessages is allocated at runtime upon entry to buildWidget. The size of the array is dynamic based on the value of count.

 Posted 26 months ago
Comment Quote
About Phil Harbison -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.

Post ID #16700 (Level 4) and Parent is 11109
Reply Posted 6/3/2017 10:08:31 AM
Location=Alpharetta, GA USA 
Joined=10 years ago   MB Posts=2   KB Comments=3  
Phil Harbison
Alpharetta, GA USA

By the way, in the example above the array messages is allocated at runtime, but the elements are initialized to string literals (e.g. "message 1") which are created at compile time and loaded into memory when the class is loaded. I suppose you can still say they are allocated on the heap at runtime, but as part of class loading.

 Posted 26 months ago
Comment Quote
About Phil Harbison -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.

Post ID #16702 (Level 5) and Parent is 11109
Reply Posted 6/3/2017 10:25:44 AM
Location=Alpharetta, GA USA 
Joined=10 years ago   MB Posts=2   KB Comments=3  
Anonymous Post
Good Post !!
 Posted 9 months ago
Comment Quote

Post ID #17333 (Level 6) and Parent is 11109
Reply Posted 11/28/2018 11:19:22 AM
Most Recent Post
PS_2136
exclusiveinn
-- USA

https://www.exclusiveinn.com/ An array is a group of like-typed variables that are stated by way of a common name.Arrays in Java paintings in a different way than they do in C/C++. Following are some critical factor about Java arrays. 

 Posted 56 days ago
Comment Quote
About PS_2136 -Collapse +Expand
Visit Profile
Membership pending.
Member subscribes to this thread but email is NOT VERIFIED.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #17596 (Level 7) and Parent is 11109
Reply Posted 6/28/2019 3:44:00 AM
Location=-- USA 
Joined=56 days ago   MB Posts=1  
Write a Comment...
Full Editor
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1236A1
Enter key:
Icon: A Post    Thread    Idea    Important!    Cool    Sad    No    Yes    Includes a Link...   
Thread #11109 Counter
6489
Since 4/2/2008
Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech