Highlights of common mistakes made by newcomers to JAVA

Source: Internet
Author: User

When learning program design, the most feared thing may be that errors cannot be found. Here, I will list the Common Errors of New JAVA students that I have found for reference. Other JAVA beginners can also learn from them.

1,The source program file suffix is. Java and. txt.

JAVA requires that the suffix of the source program file be. java. It may be to prevent users from Accidentally changing the file suffix. By default, Windows hides the suffix, which poses a hidden risk for beginners to make mistakes. At this point, we create a text file and change its "suffix name" to. java, as shown in:

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/12001Ca9-0.png "/>

On the surface, the full name of the file is HelloWorld. java. However, when you compile the file, the error "file not found" is reported, as shown in:

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/12001B157-1.png "/>

Run the dir command to display all directories and files under the current directory. This file will reveal its true nature. As shown in:


650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/12001C123-2.png "/>

It can be seen that the suffix of the suffix is still. txt. Windows hides the file extension name. Therefore, when we change the file name to HelloWorld. java, the extension name is not changed. java is only part of the file name, not the suffix name.

The solution is not to hide the file Suffix in Windows. Open "my computer", click "Tools"> "Folder Options" in the top menu bar, open the "Folder Options" dialog box, and click the "View" tab, in "Advanced Settings", clear the check box before "Hide extensions of known file types. As shown in:

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1200161504-3.png "/>

After confirmation, we can clearly see the true face of the file "HelloWorld. java" in Windows. As shown in:

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/12001A5B-4.png "/>

2. There is no distinction between Chinese and English

In JAVA, similar to other languages such as C), the semicolon ";" and parentheses include parentheses (). ", brackets" [] ", braces" {} "), double quotation marks", single quotation marks, etc., must all be in English. If you enter Chinese characters accidentally, a syntax error is returned. Such errors are hidden for beginners and often take a lot of time and patience. Taking the semicolon as an example, you can use the following method to check whether it is written in Chinese by mistake: press the shortcut key Ctrl + F to open the Search dialog box and enter the Chinese semicolon to search for it, check whether the searched semicolon should be written in English. To ensure that the entire program file can be searched, you can move the cursor to the beginning of the program before the search. By default, the file tail is searched from the current cursor. The content before the cursor is not searched. Or you can use some search options to search for the entire file. An example of EditPlus and Windows notepad editor is as follows:

Editplus. Check the options in the red box.

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/12001C543-5.png "/>

Notepad: for example, search downward by default. After searching down, click "up" to search up.

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/12001CV1-6.png "/>

3. parentheses and quotation marks are not paired.

In the program, Parentheses (), square brackets []), braces {}), and double quotation marks "") and single quotation marks must appear in pairs; otherwise, an error is reported. For example, the following error "Syntax Parsing has reached the end of the file" is because a right braces are missing. To avoid such errors, you can write the brackets and quotation marks before adding code to them.

4. Braces missing

For if/else and loop, if you only need to execute one statement, you do not need braces. However, when you need to add a statement, you need to use braces to include multiple statements. This is easy to ignore. once ignored, the logic error program can run but the result is incorrect ). Troubleshooting logical errors is much more difficult than syntax errors, because the compiler does not report errors, which often takes a lot of time and patience. Therefore, to avoid such errors, you should develop the habit of applying braces even if only one statement is executed.

5. The class name/variable name is incorrect or case-insensitive. 

The class name must be correct. After the variable is defined, the variable name must be consistent with the definition name. What is more prone to errors is that JAVA is a case-sensitive language. If a String is written as a string or the same variable name, an error will be reported if it is used in both upper and lower case cases. Therefore, when the compiler reports a "symbol not found" error, you should carefully check whether the class name or variable name is correct based on the compiler error message. For example, the error is that the class String is written as a string and the variable scan is defined, but the variable is written as sscan.

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/12001CB8-7.png "/>

6. The public class name should have the same name as the file name.

The classes we generally write are all public). JAVA requires that the file names of the files that save the public classes must be the same as those of the class, and the file names must be case sensitive. Otherwise, an error is reported: the class XX is public and should be declared in the file named XX. java. The "XX" here refers to a class name, and the following is also true.

7. Modified files are not saved or re-compiled

If the error changes are not saved, the changes do not take effect. Therefore, save the changes first. After saving the file, you should re-compile and generate a new bytecode file. class) because the file is actually running. If you do not re-compile, the old bytecode file is still running.

8. error: the class name "XX. JAVA" is accepted only when the explicit request annotation is processed"

This error was encountered when the student program was corrected some time ago. The program itself did not find any problems, but the file name was written as XX. JAVA. At the moment, I did not know where the error was. Later, I changed the file suffix. JAVA to. java. Because I keep writing the suffix as. java when writing a program, I have never encountered this problem or encountered it, But I forgot ). When I see an upper-case suffix, I subconsciously think that the suffix belongs to the concept of Windows, while Windows is case-insensitive, so I do not care. However, I obviously ignored one point at the time. When we use the javac command to compile a program, javac is a JAVA object. It is case sensitive for the file name to be compiled. Therefore, file names XX. JAVA and XX. java are different for them. As mentioned in the third article above, the public class name should have the same name as the file name, and be case sensitive. If we write javac as JAVAC, there is no problem. Because the javac command is called by Windows, it is case-insensitive.

9. I forgot to add the current directory when setting the classpath environment variable.

If you want to reference third-party jar packages without using IDE, You need to introduce these jar packages into the classpath environment variables. However, if classpath is set without the current directory.), even if you run the bytecode file using the java command in the directory where the bytecode file is located, java. lang. NoClassDefFoundError is reported. This is because if classpath is not set, classpath automatically contains the current directory. If classpath is set, it does not automatically contain the current directory. You need to explicitly specify include, this is different from the path environment variable.

10. Parentheses are missing when no parameters are provided for a method call.

Like C, JAVA calls methods with parentheses even if there are no parameters. This error indicates that the basic knowledge is not strong.

11. Incorrect import Statement position

For example, the following program:

 
 
  1. import java.text.DecimalFormat;  
  2.  
  3. class A  
  4. {  
  5. }  
  6.  
  7. import java.util.Scanner;  
  8. class B  
  9. {  

The following error is returned: "class, interface, or enum is required ". This is because the import statement should be written before the class definition and cannot appear between the two classes. Therefore, the above procedures should be modified as follows:

 
 
  1. import java.text.DecimalFormat;  
  2. import java.util.Scanner;  
  3.  
  4. class A  
  5. {  
  6. }  
  7.  
  8. class B  
  9. {  

To avoid such errors, we recommend that you write each class in a separate file.

If other problems are found, the update will continue)

This article is from the "Xiao fan's column" blog, please be sure to keep this source http://legend2011.blog.51cto.com/3018495/1057901

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.