473,320 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

java array and string

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.

4 3173
SioSio
272 256MB
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
630 Expert 512MB
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
gstry
2
thanks so much well explain
Feb 6 '20 #4
hocan90
3 2Bits
for size 5 int name[]=new int[5];
Jul 22 '20 #5

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

Similar topics

1
by: Min-Koo Seo | last post by:
Hi. I have a java stored procedure whose spec is as follows: CREATE OR REPLACE FUNCTION DECODE_SEQUENCE(SEQUENCE VARCHAR2) RETURN VARCHAR2 DETERMINISTIC AS LANGUAGE JAVA NAME...
1
by: Eugene | last post by:
Hi All, I am having a hard time figuring out how to pass a ("true") java array, like int, from a jdbc application in to a DB2 stored procedure or UDF (can be a java or SQL proc or a table UDF)....
1
by: vbdotnetmania | last post by:
Hi, I'm trying to port java code to c#, I am not too familiar with either so forgive me for what might be a stupid question, I wish to use the java string type rather than the c# one, when I try to...
1
by: spud379 | last post by:
i am doing an assignment in vb.net. i have to select at random from and array what i have is dim country(29) as string dim random as new random txtcountry.text = random.next(country) I...
4
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates...
1
by: pompeez | last post by:
Hi, I need to write a Java Array/ArrayList/List to a text file in Jython. I used the .write() function, but it is letting me write only a string. Thanks! Pompeez
5
by: creative1 | last post by:
hi, I am new at writing JSP and Servlets. When I compile my serverlet I get follwoing error cannot find mymbol method parseDate(java.lang.String) I have follwoing code: Code: ( text )...
1
by: ahmee | last post by:
this my program code these are my errors below please help me out....guys operater % cannot be applied to java.lang.String.int operater / cannot be applied to java.lang.String.int operater +...
1
by: sensure | last post by:
why java array starts with zero and why pascal array stars with one?
1
by: capablanca | last post by:
Hello, I just i would like to know how to record a string from a label to an array string, if anybody can show me an example, thanks. ? string stringArray = labelone.Text
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.