Identifying syntax errors in Java
Given this code in Java:
int i,j;
String[] names;
names[0] = new String("mary");
names[1] = "John";
i = names.length;
j = names[0].length();
I need to find the error. As far as I can tell, lines 1, 2, 4, and 5 are correct because开发者_如何学JAVA they involve simple instance variables, adding elements to arrays, and finding the length of an array. However lines 3 and 6 are weird.
Can you add a string to an array like the way done in line 3? If the array is full of strings, can you index one out and use the length() method on it?
There are no syntax errors in the above code, however:
they involve simple instance variables
They're not instance variables unless you declare them as part of a class, outside its methods. If they're declared within methods, they're called local variables to the scope of whichever code blocks they're declared.
Trying to add anything to an uninitialized array, as in
String[] names;
names[0] = new String("mary");
names[1] = "John";
Will still cause a compile-time error, however it's not due to incorrect syntax but an attempt to manipulate an uninitialized variable. You need to initialize it, so for example as others have said, use:
String[] names = new String[2];
If the array is full of strings, can you index one out and use the length() method on it?
Sure, this line is perfectly legal:
j = names[0].length();
And is equivalent to (assuming you fix the uninitialized-array error above yada yada):
String firstElement = names[0];
j = firstElement.length();
The names
variable has not been initialized to an array of two elements.
String[] names;
should be
String[] names = new String[2];
The rest of the code looks good.
Some minor remarks:
new String("mary");
is in almost all situations equivalent to just"mary"
You could also initialize the
names
-array like thisString[] names = { "mary", "John" };
Once the array is created, the size is fixed. You can't "add elements" to the array. You can only update values in it. (For an array that can grow, I suggest you have a look at
ArrayList
.)
There is no syntax error. It's a compiler error, because your array is not initialized.
A further point - when initializing the array make sure it is initialized with a size that can hold the data you are trying to put in it later.
Now sorry for asking the obvious question, but what stopped your from trying to compile this code and see what the errors are? They are descriptive enough - the eclipse compiler says:
The local variable names may not have been initialized
So to answer your question title (assuming it should be read "how to identify syntax errors in Java"):
Let the compiler do it.
The names array is never created. You need to change line 2: String[] names;
into String[] names = new String[2];
The names
variable is never initialized. Attempting to assign values to its elements will cause a compiler error. But it's not a syntax error, strictly speaking.
You'll notice that everyone says that this isn't a syntax error. And they are correct. A syntax error is an error that breaks, well, the syntax or form of the programming language. What you have here is a SEMANTIC error, an error in the meaning of the code.
Think of the two as in English, syntax has to do with grammar and spelling, whereas semantics deal with the actual meaning.
Try this:
int i,j;
String[] names = new String[2];
names[0] = "Mary";
names[1] = "John";
i = names.length;
j = names[0].length();
The reason your code wasn't working was because in saying String[] names;
you were only declaring that there was going to be an array of names but in Java you need to either explicitly say how many items your array will hold or immediately assign the values to the array as such...
String[] names = {"Mary", "John"};
in order to initialize the array.
精彩评论