Home >Java >JavaBase >Common syntax errors in java

Common syntax errors in java

angryTom
angryTomOriginal
2019-11-15 11:03:526271browse

Common syntax errors in java

Common grammatical errors in java

Grammar errors are problems that do not comply with grammatical rules, such as If you multiply two strings, of course it is wrong. It is what is commonly called a bad sentence.

The following is a list of common grammatical errors in Java. I hope it will be helpful to Java beginners.

1. The variable may not be initialized

Error message:

Abc.java:9: variable i might not have been initialized
                System.out.println(i);

Chinese description: Variable i may be used without assigning a value.

Example description:

int i;
System.out.println(i);

2. Variables are repeatedly defined

Error message:

Abc.java:9: i is already defined in main(java.lang.String[])
                int i = 2;

Chinese description: Variables are repeatedly defined

Example:

 int i = 1;
 int i = 2;

3. Symbol not found:

Test6.java:26: cannot find symbol
symbol  : variable j
location: class Test6
   if(j < 0) {

4. Class not found error

Main.java:4: cannot find symbol
symbol  : class T1
location: class Main
  T1 a = new T1();

5. Method not found error

Main.java:5: cannot find symbol
symbol  : method a()
location: class T
  a.a();

6. Class not found

Error message

Test.java:1: class Test1 is public, should be declared in a file named Test1.java
public class Test1 {

Chinese description test1 is public and must be declared in the file

Example description

Create a file as Test; open it in the tool and write like this public class Test11 { }; This error will be reported

7 The class cannot be found (the class name is inconsistent with the file name)

NoClassDefFoundError: asa (wrong name: ASA)

8. Under the array Subscript out of bounds

java.lang.ArrayIndexOutOfBoundsException: 1
        at Test2.test2(Test2.java:30)
        at Test2.main(Test2.java:6)

9. String subscript out of bounds

java.lang.StringIndexOutOfBoundsException: String index out of range: 6
        at java.lang.String.charAt(String.java:558)
        at Test2.test3(Test2.java:41)
        at Test2.main(Test2.java:7)

10. Null pointer

Exception in thread "main" java.lang.NullPointerException
        at Next.main(Next.java:31)

11 Empty return value

Error message

 Test1.java:54: &#39;void&#39; type not allowed here
   System.out.println(a5.deleteOnExit());

Chinese description; void return value is not allowed here

Example description If a void method is declared, Then it cannot be output directly

  Public static void edit() {}
System.out.println(Test.edit());

12 Missing return value

asa.java:8: missing return statement
 int fan(){}
                  ^
1 error

13 There is no need to return a value in a method without a return value

asa.java:10: cannot return a value from method whose result type is void
   return a;
                        ^
1 error

14. The referenced method has incorrect parameters

Next.java:66: cannot find symbol
symbol  : method createTempFile(java.lang.String,java.lang.String,java.lang.String)
location: class java.io.File
   File ll = f.createTempFile("let","java","aaa");

15. Missing formal parameters

 del() in sms.service.Service cannot be applied to (int)

16.Invalid Method declaration (return type required)

invalid method declaration; return type required
        public byteValue(){

17. It is required to pass in an array, but a string is passed in

array required, but java.lang.String found
                    ^

18 Constructor method not found

Main.java:4: cannot find symbol
symbol  : constructor T()
location: class T
  new T();

19. Number formatting exception                                                                                        

Exception in thread "main" java.lang.NumberFormatException: null 20. .不兼容的类型
错误提示Test1.java:41: incompatible types
found   : java.lang.String[]
required: java.io.File[]
 File [] a3 = a11.list();
中文说明 不兼容的类型

20 Integer number too large Definition value (integer) is too large

21. Non-static methods cannot be referenced in a static context

non-static method cannot be referenced from a static context

22. It is not a static method but is called in a static way (class name. method )

Main.java:5: non-static method fun1() cannot be referenced from a static context
                Test.fun1();

23 Static access to non-static (variables)

Test.java:5: non-static variable a cannot be referenced from a static context
                a = 1000;

24. Static access to non-static (methods)

Test.java:6: non-static method fun1() cannot be referenced from a static context
            fun1();                    // 静态的不能调用非静

25.continue outside of loop (error report that occurs when continue is placed outside the for loop)

26.illegal start of expression expression that violates the rules (Error report that occurs when the second expression in the for loop is placed outside or inside the for loop)

27 asa.java:6: unreachable statement Unreachable statement (statement placed inside

cannot be reached after continue or break, and there cannot be statements after continue and break)

28 break is placed outside the loop statement

asa.java:8: break outside switch or loop
 break;
        ^
1 error

29- Identifier error (identifier is illegal);

asa.java:2: <identifier> expected
 int %%;
            ^
1 error

30. The method body was not found or declared abstract ( Method)

MyAbstract.java:6: missing method body, or declare abstract

31. This class is not an abstract class or there is no override. The overridden method fun1() has Abstract methods must be of abstract class

MyAbstract.java:1: MyAdstract is not abstract and does not override abstract method fun1() in MyAdstract

32. Myabstract It belongs to the abstract class and cannot generate objects.

Main.java:6: Myabstract is abstract; cannot be instantiated

33. Interface methods cannot have method bodies

MyInterface.java:2: interface methods cannot have body

34, .It belongs to an abstract class and cannot generate entities

Myabstract is abstract; cannot be instantiated

35. Interface methods cannot have method bodies

interface methods cannot have body

36. Static modification is not allowed here

asa.java:3: modifier static not allowed here
 public static void main(String []args){
     ^    ^

37—Type that cannot be changed (String type cannot be converted to Int type)

asa.java:4: inconvertible types
found   : java.lang.String
required: int
  int b=(int)a;
                           ^
1 error

38.possible loss of precision found: long ;required:byte ; var=varlong Possible loss of precision (loss will be caused when a larger integer is converted to a smaller one, but no loss will be caused when a small integer is converted to a larger one.)

39 delimiter Exception

asa.java:5: &#39;;&#39; expected

40 Bracket Exception

asa.java:8: &#39;}&#39; expected

41. The application attempted to create an array with a negative size.

 java.lang.NegativeArraySizeException

42. Abnormal operation conditions

java.lang.ArithmeticException: / by zero
        at Test2.test(Test2.java:16)
        at Test2.main(Test2.java:5)

43 Abstract methods cannot be modified by final (abstract things of abstract classes must be Inheritance)

44. Abstract methods cannot be modified by private (abstract things in abstract classes must be inherited)

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of Common syntax errors in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more