Outofmemory error Java heap space

Hi All,

As I am exploring the java Lang ,I wrote the below code but the program run to print array but then throws Outofmemoryerror, I am also including the snapshot of the error.

I belive some where the program is entring into infinite loops, Any help is highly appricated.

package javaProg.completeReferance;

publicclass VarArgs

{

publicstaticvoid main(String [] args)

{

int [] array ={1,2,3,4,5,6,7,8,9,10};

printVarArgs(array);

String [] stringArray =stringArray();

for(String store : stringArray)

System.out.println(store+" ");

}

static String []stringArray()

{

char c='0';

String [] stringArray =new String[10];

for(int i=0;i<stringArray.length;i++)

{

StringBuffer str=new StringBuffer("");

for (int j=1;j<11 ;i++,c++)

{

str.append(c);

}

stringArray[i]=str.toString();

}

return stringArray;

}

staticvoid printVarArgs(int... varArray)

{

for(int store : varArray)

System.out.println("value "+store);

}

}

Here is the snapshot of the error..

C:\Users\pravin>java javaProg.completeReferance.VarArgs

value 1

value 2

value 3

value 4

value 5

value 6

value 7

value 8

value 9

value 10

Exception in thread"main" java.lang.OutOfMemoryError: Java heap space

at java.util.Arrays.copyOf(Unknown Source)

at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)

at java.lang.AbstractStringBuilder.append(Unknown Source)

at java.lang.StringBuffer.append(Unknown Source)

at javaProg.completeReferance.VarArgs.stringArray(VarArgs.java:23)

at javaProg.completeReferance.VarArgs.main(VarArgs.java:9)

[3015 byte] By [confused_in_javaa] at [2007-11-27 10:37:39]
# 1

The problem, I believe is here:

> for (int j=1;j<11 ;i++,c++)

You probably want j++ in there, not i++. If you change it let me know if this helps.

petes1234a at 2007-7-28 18:49:35 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks but I got the bug its here

StringBuffer str=new StringBuffer("");

for (int j=1;j<11 ;i++,c++)

Insted of incrementing i it should increment j.

confused_in_javaa at 2007-7-28 18:49:35 > top of Java-index,Java Essentials,New To Java...
# 3

You are right petes thanks for your effort.

One more thing can you please tell me can we have VarArgs as return type?

if Yes How and where to store it.

If now Why this is not available?

because all the types that can be passed as argument can also be specified as return type.

Sorry if this sounds wired!!

confused_in_javaa at 2007-7-28 18:49:35 > top of Java-index,Java Essentials,New To Java...
# 4

Yes. There is not much difference between String and VarArgs. Both are classes, with methods and fields (fields are variables that are stored at the entire class-level instead of just within a method). The only difference is that you wrote VarArgs and someone else wrote String.

So if String can be a return type, then so can VarArgs. However, as is there will be no point in having any instance of a VarArgs object, let alone returning it from a method.

Note that all your methods above are static, and that there are no fields. Only when VarArgs has some non-static methods and/or fields will it be useful to create instances of these objects and pass them as parameters or output.

bheilersa at 2007-7-28 18:49:35 > top of Java-index,Java Essentials,New To Java...