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 7713511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:53:41+00:00 2026-06-01T01:53:41+00:00

I try use a integer array in java with the code below: public static

  • 0

I try use a integer array in java with the code below:

    public static void main(String[] args) {
        int[] array = testArray(100);
        System.out.println(array.length);
        for(int i = 0; i < 100; i++)
           System.out.println(array[i]);
    }

    public static int[] testArray(int size){
        int[] array = new int[size];
        for(int i = 0; i < size; i++)
           array[i] = i;
        return array;
    }

I also test an integer array in C++ as below:

       #include<iostream>
       using namespace std;

       void getArray(int size)
       {
            int array[size];
            for(int i=0; i<size; i++)
                array[i]=i; 
      }

      int main()
      {
           getArray(10);
           return 0;
      }

And I always get the right answer with the two snippets, why? since I think that the length of an array cannot be variable for language such as java, c and c++.

  • 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-01T01:53:43+00:00Added an answer on June 1, 2026 at 1:53 am

    The three languages are different, and the features of the different languages will, well, differ. In particular, in C99 you can define arrays of a variable in the stack (for those that program mainly Java, this is commonly used to refer to a size that is not a compile time constant, not that the array will change sizes).

    In C++ you cannot declare them in the stack (GCC allows this, with a non-conforming extension to the language that mimics C99 behavior), but you can dynamically allocate memory with new of the given size and store the pointer in a variable.

    In Java you cannot create an array in the stack ever, and all you can do is dynamically allocate the array with new and store a reference.

    int size = 100;
    
    // C99
    int data[size];
    
    // C++
    // int data[size];          // error, but allowed by gcc
    int *data = new int[size]; 
    
    // Java
    int[] data = new int[size];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to use the following code: ArrayList<String> Map<String, String> Eclipse complains about both
I try to use this code for toggling text on the link: var showText=<span>Open</span>
I have some code that is working correctly in Java but when I try
import java.util.*; class AddressBook { private static final int DEFAULT_SIZE = 25; private Person[]
I try to use the domainpeople.com API and to do I need to use
I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
I try to use the winDBG to debug a dump file. When I run
I try to use PHPMailer to send registration, activation. etc mail to users: require(class.phpmailer.php);
I try to use single quotes as much as possible and I've noticed that
I try to use amazon API using PHP. If I use print_r($parsed_xml->Items->Item->ItemAttributes) it show

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.