+ 6
Can array size be negative?
25 Answers
+ 12
Index of an array starts from Shunyam ๐
+ 8
Nope!! am from Karnataka #Bangalore ๐
+ 8
ohh!! kk
Fan of NivinPauly ๐
+ 7
Nope. You either have something or you have nothing.
+ 5
@RozeS Think of an array like a basket. You can't hold negative amount of items in a basket, you simply have nothing in the basket even though the basket still exists.
+ 5
import java.util.*;
import java.io.*;
public class Stacktest
{
public static void main(String args[])throws IOException
{
int c[]=new int[-2];
Scanner in=new Scanner(new InputStreamReader(System.in));
int b=in.nextInt();
int a[]=new int[b];
}
}
o/p
Exception in thread "main" java.lang.NegativeArraySizeException at Stacktest.main(Stacktest.java:10)
+ 4
Thanku ol ๐ธ Got from dis too -> https://stackoverflow.com/questions/24797013/how-to-catch-negativearraysizeexception
+ 4
๐dis s a short note i gt from net. i hope it wl help ppl wid same doubt
+ 4
dats ol๐
+ 4
@Sreenivas i thnk ur a mallu,ryt?๐
+ 4
ok lev dat
+ 4
me too๐
+ 3
No point. Any array has at least 0 elements...
And each data type has a positive size...
+ 3
yah of course๐๐
+ 3
yah ๐นwil do๐
+ 3
The Negative Array size exception is one of the rarely occurring exception in java programming.
We have studied that,when defining an array in java(and almost in all the programming languages ) we should give a positive value for the size of the array.As we have read we used to give only positive values.
Even if you specify a negative value,the compiler will accept it and it will not show any error.The fact is that the compiler is designed to verify whether the value given for the size is an integer or not.
It will not check whether the given value is positive or not at the compile time and throws this Negative Array Size Exception.
+ 3
nop. not like dat
+ 3
yupz๐
+ 3
i gt confused by seeing ur profile pic๐
+ 2
if we declared like dat it wil b an exception na?