Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  A silly question  (Read 2710 times)
0 Members and 1 Guest are viewing this topic.
Offline g666

Junior Duke





« Posted 2008-05-17 18:42:26 »

what kind of exception do you get when you concatenate 2 strings where the combined length would be more than Integer.MAX_VALUE ?


desperately seeking sanity
Offline Jackal von ÖRF

Junior Duke





« Reply #1 - Posted 2008-05-17 19:33:56 »

You'll get OutOfMemoryError long before you even get a chance of doing that, unless you can allocate over 8 GB memory. A string of Integer.MAX_VALUE characters requires at least about 4 GB memory (16 bits * (2^31 - 1) + some object overhead, assuming that the char arrays are stored optimally in memory), and you would need at least double that to keep all the three strings in memory during the copy operation.

If it were possible to allocate that much memory, I think you would get NegativeArraySizeException from java.lang.String#concat(String) (which will try to create a char[] with negative length) or IndexOutOfBoundsException from java.lang.StringBuilder#append(String) (which calls java.lang.String#getChars which calls java.lang.System#arraycopy).

Interestingly java.lang.AbstractStringBuilder#expandCapacity might truncate the length to Integer.MAX_VALUE characters (although I'm not anymore 100% sure about that), but java.lang.AbstractStringBuilder#append will not call that method because newCount will be negative. I wonder if this is by design or by chance. I also think that it's best to throw an exception instead of truncating the string and corrupting data.

EDIT: I think I got it right now. A StringIndexOutOfBoundsException will not be throw even though I at first thought that it would.

EDIT2: Would somebody know the memory limit of Sun's 64-bit JVM? Here 2 GB and 4 GB are mentioned: http://www.theserverside.com/discussions/thread.tss?thread_id=45758

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #2 - Posted 2008-05-17 20:29:19 »

It bombs with java.lang.NegativeArraySizeException at that line where it tries to create a new char buffer.

edit: That will even happen if both have a length of Integer.MAX_VALUE.
edit2: Grammar b0rkage o_O

弾幕 ☆ @mahonnaiseblog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bienator

Senior Duke




OutOfCoffeeException


« Reply #3 - Posted 2008-05-17 21:51:33 »

EDIT2: Would somebody know the memory limit of Sun's 64-bit JVM? ...
vm memory limits are os dependent, additionally the heap have to be contiguous,
see table 4.1:
http://docs.sun.com/source/819-0084/pt_tuningjava.html

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #4 - Posted 2008-05-19 14:22:07 »

what kind of exception do you get when you concatenate 2 strings where the combined length would be more than Integer.MAX_VALUE ?



Why on earth would you want to do that?  Shocked

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2008-05-19 18:35:20 »

I've also wondered what would happen if I changed the value of the instance variable for length in an array.

Ex.) int[] arr = new int[5];
int.length = Integer.MAX_VALUE;
int[999] = 1000;

Will I start overwriting my operating system?  Tongue

See my work:
OTC Software
Offline g666

Junior Duke





« Reply #6 - Posted 2008-05-19 18:40:30 »

Why on earth would you want to do that?  Shocked

heh i really dont of course, i just wondered  Smiley

I've also wondered what would happen if I changed the value of the instance variable for length in an array.

Ex.) int[] arr = new int[5];
int.length = Integer.MAX_VALUE;
int[999] = 1000;

Will I start overwriting my operating system?  Tongue

eh its final you cant change it

desperately seeking sanity
Online Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2008-05-19 19:10:49 »

There isn't a length field at all in an array - it's a nasty hack in the compiler

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #8 - Posted 2008-05-19 23:48:37 »

Oh, bummer. I thought it was a public instance variable.

See my work:
OTC Software
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #9 - Posted 2008-05-20 10:02:59 »

Oh, bummer. I thought it was a public instance variable.

Well, better a nasty compiler hack than a public non-final instance variable  Cheesy

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (26 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (52 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (76 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!