5

I made a JavaClass which is making addition, sub, mult. etc.

And the numbers are like (155^199 [+,-,,/] 555^669 [+,-,,/] ..... [+,-,*,/] x^n);

each number is stored in Byte-Array and byte-Array can contain max. 66.442

example:

(byte) array = [1][0] + [9][0] = [1][0][0]

(byte) array = [9][0] * [9][0] = [1][8][0][0]

My Class file is not working if the number is bigger then (example: 999^999)

How i can solve this problem to make addition between much bigger numbers?

When the byte-Array reachs the 66.443 values, VM gives this error:

Caused by: java.lang.ClassNotFoundException. which is actually not the correct error-description.

well it means, if i have a byte-array with 66.443 values, the class cannot read correctly.

Solved: Used multidimensional-Byte Array to solve this problem.

array{array, ... nth-array} [+, -, /] nth-array{array, ... nth-array}

only few seconds to make an addition between big numbers.

Thank you!

Racooon
  • 1,468
  • 2
  • 10
  • 28

5 Answers5

5

A single method in Java is limited to 64KB of byte code. When you initialise an array in code it uses byte code to do this. This would limit the maximum size you can define an array to about this size.

If you have a large byte array of value I suggest you store it in an external file and load it at runtime. This way you can have a byte array of up to 2 GB. If you need more than this you need to have an array of arrays.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

What does your actual code look like? What error are you getting?

A Java byte array can hold up to 2^31-1 values, if there is that much contiguous memory available.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
2

Each array can hold a maximum of Integer.MAX_VALUE values. If it crashes, I guess you see an OutOfMemoryError. Fix that by starting you java vm with more heap space:

 java -Xmx1024M  <...>

(example give 1024 MByte heap space)


java.lang.ClassNotFoundException is thrown if the virtual machine needs a class and can't load it - usually because it is not on the class path (sometimes the case when we simply forget to compile a java source file..). This exception is totally unrelated to java array operations.

To continue the discussion in the comments section:

The name of the missing class is very important. At the line of code, where the exception is thrown, the VM tries to load the class ClassBigMath for the very first time and fails. The classloader can't find a file ClassBigMath.class on the classpath.

Double check first if the compiled java file is really present and double check that you don't have a typo in your source code. Typical reasons for this error:

  • We simply forget to compile a source file
  • A class file is on the classpath at compilation time but not at execution time
  • We do a Class.forName("MyClass") and have a typo in the class name
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • Probably misunderstood the question - leave the answer anyway and will delete it/change it once the question becomes clear.. *crashes*?? – Andreas Dolk Jun 09 '11 at 08:15
  • Is it possible to set it in netbeans? – Racooon Jun 09 '11 at 08:18
  • @Vural Acar - definitly yes - but it won't solve the problem as you see a `ClassNotFoundException` - which has *nothing* to do with low memory. – Andreas Dolk Jun 09 '11 at 08:21
  • @Andreas_D, When i have 66.442 values in array, it gives no error but when i have 66.443 values then it gives wrong error like ClassNotFoundException. I think, i will solve this problem making multidimensional-byte-array. – Racooon Jun 09 '11 at 08:30
  • @Vural Acar - *which* class can't be found?? Please give us the name as this is just *too* strange. – Andreas Dolk Jun 09 '11 at 08:32
  • the class name is not important, otherwise i would give it. classname : ClassBigMath(); and there is only one class. – Racooon Jun 09 '11 at 08:38
1

java.math.BigInteger is much better solution to handle large number. Is there any reason , you have choosed byte array ?

Gursel Koca
  • 20,940
  • 2
  • 24
  • 34
0

The maximum size of an array in Java is given by Integer.MAX_VALUE. This is 2^31-1 elements. You might get OOM exceptions for less if there is not enough memory free. Besides that, for what you are doing you might want to look at the BigInteger class. It seems you are doing your math in some form of decimal representation, which is not very memory efficient.

Waldheinz
  • 10,399
  • 3
  • 31
  • 61