Can array size be negative? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Can array size be negative?

5th Jul 2017, 1:41 PM
Rose
Rose - avatar
25 Answers
+ 12
Index of an array starts from Shunyam 😁
5th Jul 2017, 1:48 PM
Sreenivas.KC
Sreenivas.KC - avatar
+ 8
Nope!! am from Karnataka #Bangalore 😊
6th Jul 2017, 10:19 AM
Sreenivas.KC
Sreenivas.KC - avatar
+ 8
ohh!! kk Fan of NivinPauly 😊
6th Jul 2017, 12:48 PM
Sreenivas.KC
Sreenivas.KC - avatar
+ 7
Nope. You either have something or you have nothing.
5th Jul 2017, 1:50 PM
AgentSmith
+ 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.
5th Jul 2017, 1:55 PM
AgentSmith
+ 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)
5th Jul 2017, 2:29 PM
Rose
Rose - avatar
5th Jul 2017, 2:06 PM
Rose
Rose - avatar
+ 4
👆dis s a short note i gt from net. i hope it wl help ppl wid same doubt
5th Jul 2017, 2:19 PM
Rose
Rose - avatar
+ 4
dats ol👆
5th Jul 2017, 2:30 PM
Rose
Rose - avatar
+ 4
@Sreenivas i thnk ur a mallu,ryt?🙄
6th Jul 2017, 10:18 AM
Rose
Rose - avatar
+ 4
ok lev dat
6th Jul 2017, 10:20 AM
Rose
Rose - avatar
+ 4
me too😍
6th Jul 2017, 1:36 PM
Rose
Rose - avatar
+ 3
No point. Any array has at least 0 elements... And each data type has a positive size...
5th Jul 2017, 1:46 PM
Ledio Deda
Ledio Deda - avatar
+ 3
yah of course👍😊
5th Jul 2017, 2:03 PM
Rose
Rose - avatar
+ 3
yah 🌹wil do👍
5th Jul 2017, 2:10 PM
Rose
Rose - avatar
+ 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.
5th Jul 2017, 2:18 PM
Rose
Rose - avatar
+ 3
nop. not like dat
5th Jul 2017, 2:28 PM
Rose
Rose - avatar
+ 3
yupz👍
5th Jul 2017, 3:19 PM
Rose
Rose - avatar
+ 3
i gt confused by seeing ur profile pic😋
6th Jul 2017, 10:22 AM
Rose
Rose - avatar
+ 2
if we declared like dat it wil b an exception na?
5th Jul 2017, 1:50 PM
Rose
Rose - avatar