Hello, today I’m going to share with you the basic Syntax of Java. Take out your notebook and write it down
The first Java program
- Public class uses the same class name as the file name. A Java program has only one public class.
- The class name must start with a letter and can be followed by any combination of letters or numbers.
- System.out.println () newlines after output data; System.out.print() prints data;
public class java_grammar {
public static void main(String[] args) { System.out.println("hello word!" ); System.out.print("hello"); System. The out. Print (" word!" ); }Copy the code
}
2 the identifier
Definition: The sequence of characters Java uses to name elements such as variables, methods, and classes are called identifiers
Tip: Call any place you can name yourself an identifier
Rule: consists of 26 uppercase and lowercase letters, 0-9, _ or $; Numbers may not begin; Can not use the keyword and reserved word, but can contain the keyword and reserved word; Java is case-sensitive and has unlimited length. Identifiers cannot contain Spaces.
2.1 Naming Conventions:
Package name: lowercase letters when multiple words are used: XXXYYYZZz
Class name, interface name: When multiple words are used, the first letter of all words is uppercase XxxYyyZzz
Variable name, method name: when multiple words are formed, the first word starts with lowercase letters and the second word starts with uppercase letters: xxxYyyZzz
Constant name: all letters are capitalized. For multiple words, underline each word: XXX_YYY_ZZZ
3 Variables and constants
In Java, the final keyword is used to indicate a constant. When a variable is modified by final, the variable is assigned only once
3.1 Declaration and assignment
Declare a variable
Syntax: < data type > < variable name >
For example, int var;
Assignment of a variable
Syntax: < variable name > = < value >
For example, var = 10;
Declare and assign variables
Syntax: < data type > < variable name > = < initial value >
For example, int var = 10;
3.2 Data Types
3.2.1 Integer Types
Java integer constants default to int, declare long constants must be followed by ‘l’ or ‘l’
3.2.2 Floating point type
Java floating-point constants default to double and declare float constants followed by ‘f’ or ‘f’.
3.2.3 Character Types
Char data is used to represent “characters” in the usual sense (2 bytes)
All characters in Java are encoded in Unicode, so a character can store a letter, a Chinese character, or a character of another written language
Char literal values are enclosed in single quotes. For example, ‘A’ is A character constant with an encoding value of 65
Values of type CHAR can be expressed as hexadecimal values ranging from \u0000 to \Uffff
3.2.4 Boolean type
Boolean data can be true and false only, but not NULL
Unlike C, you cannot replace false and true with 0 or non-0 integers
3.2.5 String type
String is not a basic data type; it is a reference data type
Use the same as the basic data type. For example, String STR = “abcd”;
The Java language allows the use of + signs to concatenate two strings
You can use the equals method to check whether two strings are equal; For example, s.quals (t) returns true if the string s is equal to the string t; Otherwise, return false.
The empty string “” is a string of length 0;
The String variable can also hold a special value called NULL, which means that no object is currently associated with the variable
3.2.6 Code Interpretation
3.3 Data type Conversion
Automatic conversion; Data types with small capacity are automatically converted to data types with large capacity. The data types are byte, short, char, int, long, float, and double in order of size
Cast; Convert a high-volume data type to a low-volume data type
4 operator.
4.1 Arithmetic operators
4.2 Assignment operators
= += -= *= /= %=
4.3 Comparison operators
4.4 Logical operators
&& | |!
4.5 bit operations
4.6 Ternary Operation
(conditional expression)? Expression 1: expression 2;
4.7 Operator priority
Well, that’s all for today’s article, hoping to help those of you who are confused in front of the screen