Java coding specification

Author: Han Ru

Company: Program Ka (Beijing) Technology Co., LTD

Program Cafe: IT professional skills evaluation platform

Website: www.chengxuka.com

task

1. Writing format 2. Comment 3. Identifier 4Copy the code

First, writing format

  • Code levels must be locked (Tab: a Tab stop)

  • Write one line of code

  • Each line of code should end with a semicolon

  • All symbols must be in English

  • Java code is strictly case sensitive

  • {},() and so on are used in pairs

Second, the annotation

Purpose: Used to explain programs: classes, methods, variables, a line of code…

For the programmer, the JVM doesn’t care and doesn’t perform comments.

2.1 Single-line comments

The most commonly used type of comment.

// Applies to a comment line
Copy the code
// Get drunk and fix the bug later
/ / magic. Do not touch
// Bubble sort
When I wrote this code, only God and I understood what I was doing, but now only God knows
/ /...
Copy the code

2.2 Multi-line comments

Usually used to explain a piece of code.

/* Multi-line comments are useful for commenting many lines at once */
Copy the code
/* * I dedicate all of this code to my husband, Wang Ergou, * for his constant support of my work, * taking care of our three children, one dog and two cats, and * looking forward to the software being released to the public */
Copy the code

2.3 Document Comments

 /** ** ** ** ** ** ** ** ** ** ** ** *
// Generate help documentation: javadoc -d directory source file name
Copy the code
/** * The first Java program in my life *@author hanru
 *
 */
public class HelloWorld {
	/** * here is the main function, the program entry *@param*/ is not yet used for args
	public static void main(String[] args) {
		System.out.println("Hello World!"); }}Copy the code

Finally, to emphasize that comments are very important for a program, a project, we need to get used to adding comments.

Identifiers

3.1 What is an Identifier

The sequences of characters Java uses to name elements such as packages, classes, methods, parameters, and variables are called identifiers.Copy the code

3.2 Naming rules for identifiers

1. The value can contain letters, digits, underscores (_), and dollar signs (dollar sound). 2. Do not start with a number. 3. Case sensitive. 4. Unlimited length. The value contains a maximum of 15 characters. 5. The value cannot be a reserved word or keyword in Java.Copy the code

3.3 Naming Conventions of Identifiers

1. Naming conventions for identifiers: They are known by their names. (English words) 2. Variable name, method name, parameter name: lowercase first letter, if multiple words, the first letter of the first word lowercase, the rest of the word uppercase (camel case nomenclature) 3. Class name: capitalize words, multiple words capitalize each word (PASCAL nomenclature)Copy the code

Keywords and reserved words

Keywords: In Java, there are strings that are assigned a specific meaning and have a specific purpose called keywords. All lowercase reserved words: not defined for use, but reserved for use. Goto, const