By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,368 Members | 1,215 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,368 IT Pros & Developers. It's quick & easy.

java array and string

P: 2
array not created like in C++

int a[5];
a[0]=5;
Main.java:13: error:']' expected
int a[5];
Main.java:13: error:illegal start of expression
int a[5]:

Why pool used for string? why memory is not used like other objects?

i read other sites, please easy explan.
Feb 5 '20 #1

✓ answered by dev7060

array not created like in C++

int a[5];
a[0]=5;
Main.java:13: error:']' expected
int a[5];
Main.java:13: error:illegal start of expression
int a[5]:
"new" keyword needs to be used. Arrays are created dynamically in Java (unlike static arrays in C++) and hence memory is allocated at run time. There is no concept of generic declaration.
Some possible ways to create an array:
-int a[]={1, 3, 5, 7, 9};
-int a[]=new int[5];
-int a[];
a = new int[5];

-int a[5]=new int[]; //incorrect way, since memory is allocated at object creation and hence "new" needs to know the size.

The above text answers the question. If the OP is interested to know some behind the scenes stuff:

As mentioned above, the memory for arrays is allocated dynamically. And guess what gets memory at runtime? an object. And that's why arrays are represented by objects in Java. Now you may ask since an object belongs to a class, to which class does an array belong to? And the answer to that would be that those classes don't exist at compile time. Classes that are created at runtime are called proxy classes. And there's an obvious reason for choosing proxy classes because of the unavailability of names of the user-defined blueprints.

Here's the possible blueprint of the runtime class:
class xxxxx extends Object implements Serializable
{ ...
long length;
...
}

Why pool used for string? why memory is not used like other objects?
Pool memory is a part of the heap. This area is used to achieve the concept of object sharing. This is the reason why a String is immutable. Garbage collector usually has no entry in this area.

Share this Question
Share on Google+
4 Replies

100+
P: 200
A single syntax error anywhere can cause multiple "illegal start of expression" errors elsewhere (perhaps the previous line).
The "illegal start of expression" error message is not very useful.
For an "illegal start of expression" error, check the line before the error for missing ')' or '}' or missing semicolon.
Feb 6 '20 #2

dev7060
Expert 100+
P: 333
array not created like in C++

int a[5];
a[0]=5;
Main.java:13: error:']' expected
int a[5];
Main.java:13: error:illegal start of expression
int a[5]:
"new" keyword needs to be used. Arrays are created dynamically in Java (unlike static arrays in C++) and hence memory is allocated at run time. There is no concept of generic declaration.
Some possible ways to create an array:
-int a[]={1, 3, 5, 7, 9};
-int a[]=new int[5];
-int a[];
a = new int[5];

-int a[5]=new int[]; //incorrect way, since memory is allocated at object creation and hence "new" needs to know the size.

The above text answers the question. If the OP is interested to know some behind the scenes stuff:

As mentioned above, the memory for arrays is allocated dynamically. And guess what gets memory at runtime? an object. And that's why arrays are represented by objects in Java. Now you may ask since an object belongs to a class, to which class does an array belong to? And the answer to that would be that those classes don't exist at compile time. Classes that are created at runtime are called proxy classes. And there's an obvious reason for choosing proxy classes because of the unavailability of names of the user-defined blueprints.

Here's the possible blueprint of the runtime class:
class xxxxx extends Object implements Serializable
{ ...
long length;
...
}

Why pool used for string? why memory is not used like other objects?
Pool memory is a part of the heap. This area is used to achieve the concept of object sharing. This is the reason why a String is immutable. Garbage collector usually has no entry in this area.
Feb 6 '20 #3

P: 2
thanks so much well explain
Feb 6 '20 #4

P: 3
for size 5 int name[]=new int[5];
Jul 22 '20 #5

Post your reply

Sign in to post your reply or Sign up for a free account.