Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8093167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:26:44+00:00 2026-06-05T20:26:44+00:00

Java noob question: Consider the following C array and initializer code: struct { int

  • 0

Java noob question:

Consider the following C array and initializer code:

struct {
  int x;
  int y;
} point_t;

point_t points[1000];

Easy. This gets created and memory allocated at load time.

Now consider the similar in Java:

public class point_t
{
   public int x;
   public int y;
}

point_t points[] = new point_t[1000];
// Without this loop, java will crash when you run this
for (int i=0; i<1000; i++)
{
   points[i] = new point_t;
}
points[0].x = 10;  // Crash would occur here without above loop
points[1].x = 10;

Initially my java program was crashing with a null pointer dereference. The problem was that, coming from C++, I was not aware that you have to create the 1000 point_t objects. Just a comment but this seems INSANE. Suppose the array size was 1 million or 1 billion. It would literally take seconds simply to “create” this array with empty entries at run time. In C++ it all happens at load time. I admit that you don’t always know what would be in the C++ array’s cells, but in embedded systems where I work, quite often the memory is auto initialized to zeros so it works.

So is there any easier, quicker, more efficient way in Java to create an array and allocate the memory when you have an array of objects? Or am I doing something wrong in the code above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:26:45+00:00Added an answer on June 5, 2026 at 8:26 pm

    Since you are coming from a C++ background, this may help. In Java, when you write

    point_t points[] = new point_t[1000];
    

    This is similar to writing, in C++,

    point_t* points[] = new point_t*[1000];
    

    That is, in Java, when you create the array, you are not creating an array of point objects, but rather and array of point references, the same as if you would have created an array of point pointers in C++.

    Java is a managed (garbage-collected) language; that is what Java programmers would expect.

    As for the second part of your question, how one would create the objects themselves, what you did is fine. Create 1000 point objects in a loop and load them up. If you want shorter code, you can write a nice method to do this work. 🙂

    You can also look into other collection libraries that might have these kind of convenience factory methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question from a Java noob. I have eclipse open (JRE 1.6),
This is probably a java noob question but here is my scenario: using selenium,
This is a java noob related question. Have been looking at Liferay portal technology,
Noob question, apologies. I'm compiling Java in Windows Vista's command-line and have so many
I'm a complete noob in java. I still really don't know how to code.
I'm new to java so forgive the noob question. I have created a swing
This is a bit of a noob question but what do I need to
I have a noob question. My java project analyzes text files. There are three
This sounds like a noob question for me but I need to ask it.
This is a complete noob question, but I gotta ask it anyway I started

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.