Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Array with long size

  1. #1
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Array with long size

    I remember, recently here was a topic where someone was asking about why arrays can't have long sizes. Well, it's obvious, that we can't write, say, new byte[longValueGoesHere]. But what would happen if we execute the following code:
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    byte[] array = new byte[Integer.MAX_VALUE];
     
    stream.write(array);
    stream.write(array);
     
    byte[] bigArray = stream.toByteArray();
    System.out.println(bigArray.length);

    I can't do this experiment on my machine, 'cause Integer.MAX_VALUE > 2,000,000,000, and I don't have 2 GB of memory to organize sufficient heap.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    979
    Thanks
    7
    Thanked 206 Times in 182 Posts

    Default Re: Array with long size

    Quote Originally Posted by angstrem View Post
    But what would happen if we execute the following code:
    Have a look at the source code for ByteArrayOutputStream. It's located wherever you have Java installed in an archive called src.zip.

    write(byte[]) is actually an OutputStream method, but it calls the ByteArrayOutputStream method write(byte[],int,int). Then write() calls ensureCapacity(), and ensureCapacity() calls grow(). Now grow() will throw an OutOfMemoryError sometimes...

    The source code documents the simple code in the last two mentioned methods with the interesting comment "// overflow-conscious code" and it's interesting to figure out exactly what the author meant. Also ensureCapacity() does document exactly when you can expect an OutOfMemoryError, and it turns out that you'll get the error however much RAM you have.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    angstrem (June 8th, 2013)

  4. #3
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Array with long size

    Thank you. I see... So they explicitly check whether int is in range.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    979
    Thanks
    7
    Thanked 206 Times in 182 Posts

    Default Re: Array with long size

    You're welcome. Yes, grow sets newCapacity=oldCapacity<<1 and then checks that newCapacity has not "wrapped around" and become negative.

Similar Threads

  1. compile errors when creating a 2nd array the same size as 1st array
    By javaiscool in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 10:35 PM
  2. [SOLVED] Array[] Size Question
    By StephenCoyle in forum Java Theory & Questions
    Replies: 5
    Last Post: March 24th, 2013, 09:41 AM
  3. array size [10] need help!! any experts
    By asdin in forum Collections and Generics
    Replies: 7
    Last Post: February 11th, 2012, 07:17 AM
  4. Replies: 6
    Last Post: October 7th, 2011, 08:50 AM
  5. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 11:50 AM