identifier

  • Identifier: refers to the content defined in the program, such as class name, method name, variable name, and so on.

  • Identifiers are the things we define in the program, such as class names, method names, variable names, and so on

  • Naming rules: there are hard and fast requirements

    • Identifiers can contain English letters (case sensitive), 0-9 digits, $, and _ (underscore)

    • Identifiers cannot start with a number

    • Identifiers cannot be keywords

    Keywords: Java defined words with special meanings, such as public, static, class, void, and so on

  • Naming conventions: See the name to know the meaning

    • Class name specification: Capitalize the first letter, capitalize each word after it (big camel)

    • Method name specification: The first letter is lowercase and each subsequent letter is capitalized (small hump)

    • Variable name specification: all lowercase of a word if multiple words are composed using a small hump

constant

Constant: Fixed data in a Java program.

Constants fall into six categories:

type meaning For example,
Integer constants All integers 0, 1, 123-132
The decimal constants All decimals 0.1, 1.2, 99.9
Character constants A single quote can have only one character and must have one and only one content ‘A ‘, ‘1’,’ you ‘
String constant Double quotes can have multiple characters or no characters at all “Hello “,” Hello “,”
Boolean constants There are only two values True, false,
Empty constants There’s only one value null

variable

Constants are fixed data, so the variables that can be changed in a program are called variables.

In mathematics, for example, letters can be used instead of numbers:

X = 1+1y = 1.1 +1...Copy the code

In the program, you can use letters to save numbers to continue the operation, can improve the computing capacity, can solve more problems, such as X can save 5, can also save 6, so the saved data can be changed, but pay attention to the data type of the variable.

In Java, a variable can only hold one data at a time, and the data type must be specified.

Let’s talk about data types and then we’ll move on to variables.

The data type

Java data types fall into two broad categories:

  • Basic data types: integer, floating point, character, Boolean

  • Reference data types: class, array, interface, string

The basic data types are divided into four types and eight types:

The data type The keyword Memory footprint Value range
Byte type byte 1 byte – 128 ~ 127
Short integer short 2 bytes – 32768 ~ 32767
The integer Int (default) 4 bytes Minus 2 to the 31st minus 2 to the 31st
Long integer long 8 bytes A bigger
Single-precision floating point number float 4 bytes 1.4013 e-45 ~ 3.4028 e
A double – precision floating – point number Double (default) 8 bytes E+308 e-324 4.9 ~ 1.7977
character char 2 bytes 0-65535.
Boolean type boolean 1 byte true, false

The default type is int and double

Definition of variables

The three elements of a variable: data type, variable name, and data value

Format 1: Define variables and assign values

Data type variable name = data value; int a = 10; Note: The value assigned to a variable cannot exceed the value range of the data typeCopy the code

Format 2: Define first and assign later

Data type variable name; // Define variable name = data value; // assign int b; b = 20;Copy the code

Long type: Recommended data followed by L

Float: data followed by F

Matters needing attention:

Variable name: A variable name cannot be repeated within a curly bracket

Variable assignment: A defined variable can only be used if it is assigned first, and the assignment must conform to the value range of the data type.