Basic syntax
The following points should be noted when writing Java programs:
- case-sensitive : Java is case-sensitive, which means that the identifier Hello is different from hello.
- class name : For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, such as Myfirstjavaclass.
- method Name : All method names should start with a lowercase letter. If the method name contains several words, the first letter of each subsequent word is capitalized.
- source file Name: The source file name must be the same as the class name. When saving the file, you should use the class name to save the filename (remember that Java is case-sensitive), and the suffix of the file name is. java. (a compilation error is caused if the file name and the class name are not the same).
- Main Method Entry : All Java programs are executed by the public static void Main (String args[]) method.
Source file claim rules
In the final part of this section, we will learn the claim rules for the source file. When you define multiple classes in one source file, and you also have import statements and package statements, pay particular attention to these rules.
- There can be only one public class in a source file
- A source file can have multiple non-public classes
- The name of the source file should be consistent with the class name of the public class. For example: The class name of the public class in the source file is employee, then the source file should be named Employee.java.
- If a class is defined in a package, then the packages statement should be in the first row of the source file.
- If the source file contains an import statement, it should be placed between the package statement and the class definition. If there is no package statement, the import statement should be first in the source file.
- The import statement and the package statement are valid for all classes defined in the source file. In the same source file, you cannot give different package declarations to different classes.
The class has several access levels, and the classes are divided into different types: abstract and final classes, and so on. These will be described in the Access Control section.
In addition to some of the types mentioned above, Java also has some special classes, such as: Inner class, anonymous class.
Java Package
Packages are primarily used to classify classes and interfaces. When developing Java programs, you might write hundreds or thousands of classes, so it is necessary to categorize classes and interfaces.
Import statement
In Java, if you give a full qualified name, including the package name, the class name, the Java compiler can easily navigate to the source code or class. The import statement is used to provide a reasonable path so that the compiler can find a class.
For example, the following command line will command the compiler to load all classes under the Java_installation/java/io path
Import java. Io. *;
byte, int, long, and short can all be represented in decimal, 16-binary, and 8-binary ways.
When a constant is used, the prefix o indicates 8 binary, while the prefix 0x represents 16 binary. For example:
intDecimal=+; int=0144; int=0x64;
Like other languages, a string constant in Java is also a sequence of characters that is contained between two quotation marks. The following is an example of a string literal:
"Hello World" "Two\nlines" "\" This is inquotes\ ""
Both string constants and character constants can contain any Unicode characters. For example:
char=' \u0001 '; String="\u0001";
The Java language supports some special sequences of escape characters.
symbols |
character meaning |
\ n |
Line Break (0x0a) |
\ r |
Enter (0x0d) |
\f |
Page Break (0x0c) |
\b |
BACKSPACE (0x08) |
\s |
Space (0x20) |
\ t |
Tabs |
\" |
Double quotes |
\‘ |
Single quotation marks |
\\ |
Back slash |
\ddd |
octal character (DDD) |
\uxxxx |
16 Binary Unicode characters (xxxx) |
Fast Intro to Java programming (1)