The basics of getting started with Java

Data types and operators

Variable naming

In the Java language, variables, constants, functions, and statement blocks also have names. We call them all Java identifiers. Identifiers are used to name classes, objects, methods, variables, interfaces, and custom data types.

1. Identifier naming rules

Identifiers in Java have the following four naming rules:

(1) Identifiers are composed of letters, numbers, underscores (_) or dollar signs ($),
(2) The first letter of the identifier starts with a letter, underscore or dollar sign, and cannot start with a number;
(3) Identifiers The naming cannot be the same as keywords, boolean values ​​(true, false) and null;
(4) Identifiers are case sensitive and have no length limitation. Adhere to the principle of knowing what the name means.
For example: myName, My_name, Points, $points,_sys_ta, OK, _23b, _23_;

2. Keywords

Keywords are reserved by the Java language and are fixedly defined as special identifiers. The keywords are all lowercase letters, and keywords cannot be defined as identifiers, otherwise a compilation error will occur.

The 48 commonly used keywords defined by Java are shown in Table 2-1:

**Table 2-1 Commonly used keywords in Java**
abstract class final int public this
assert continue finally interface return throw
boolean default float long short throws
break do for native static transient
byte double if new strictfp try
case else implements package super void
catch enum import privvate switch volatile
char extends instanceof protected synchronized while

Comments in java

Annotation is an important means of communication between program developers and program readers, and is an explanation and description of the code. Java provides three types of comments: single-line comments, multi-line comments, and documentation comments. The annotated content will not be compiled.

1. Single-line comment
Single-line comment refers to the comment that can only be written on one line. It is the simplest type of comment. It is used to briefly explain the code of a certain line. Single-line comments start with "//" and follow "//" The contents are considered as comments (shortcut key: Crtl+/, single-line comments can be automatically generated).

The syntax format of a single-line comment is as follows:
//Single-line comment

2. Multi-line comments

Multi-line comments are generally used to explain more complex content. When there are multiple lines of content that need to be commented, multi-line comments are generally used. Select the code block and use "/* code block*/" to comment, starting with "/*", and "*/", the content in the middle is the annotated content (shortcut key Ctrl+Shift+/).

The syntax format of multi-line comments is as follows:
/*Multi-line comments
Multi-line comments
Multi-line comments*/

3. Documentation notes

Explaining comments allow you to embed information about the program in the program. You can use the javadoc tool software to generate information and output it to an HTML file. Explain notes, make you more convenient to record the information of your program. Document comments start with "/**" and end with "*/". Each comment contains some descriptive text and several document comment tags, usually prefixed with "@".

The grammatical format of documentation notes is as follows:

/**
*Document Notes
*/

type of data

In Java, data types are used by programming languages ​​to describe different things. The data types corresponding to different programming languages ​​are all the same (generally the same classification), but they are also different (different keywords).

1. Basic data types

There are 8 basic data types in Java, which are mainly divided into numeric type, character type and Boolean type:
(1) Numerical type is divided into integer type and floating-point type: integer types are byte, short, int, long; floating-point type There are float and double;
(2) Character type: char;
(3) Boolean type: boolean.

2. Reference data types

Reference data in Java mainly includes classes, interfaces, and arrays.

constant

A constant in Java refers to an amount whose value cannot be changed while the program is running.

variable

A variable is the amount whose value can be changed during the running of the program. It is a basic storage unit of a Java program and can be used to store data.

The grammatical format of variables: [Access modifier] Variable type variable name [=initial value];
"Variable type" can be selected from data types;
"Variable name" is a defined name variable, which must follow the identifier naming rule;
square brackets The content in is the initial value and is optional.

public class H123 {
    
    
    public static void main(String[] args) {
    
    
        int age=25;
        String name="小明";
        int workTime=3;
        String way="Java";
        String favorite="篮球";
        String projectCount="5";
        System.out.println("这个同学的姓名是:"+name);
        System.out.println("年龄是:"+age);
        System.out.println("工作了"+workTime+"年了");
        System.out.println("做过"+projectCount+"个项目");
        System.out.println("技术方向是:"+way);
        System.out.println("兴趣爱好是:"+favorite);
    }
}

Insert picture description here

Data type conversion

Type conversion is required when performing operations between different basic data types. Except for the Boolean type, all basic data types must be considered for type conversion when performing operations, and they are mainly used in arithmetic operations and assignment operations.

1. In arithmetic operation, the
more the number of storage bits, the higher the level of the type. The highest priority is double, followed by float, long, int, char, short, byte.
2. During assignment operation,
there are automatic type conversion and forced type conversion.
(1) Automatic type conversion Automatic type conversion
will be performed when assigning a low-level type to a high-level type.

byte b=7;
int i=b; // byte level is lower than int, so b is automatically converted to int type

(2) Forced type conversion
When assigning a high-level type to a low-level type, a forced type conversion must be performed. In Java, a pair of parentheses are used for coercive type conversion.

int num=786;
byte by=num; //error
byte by=(byte)num;//correct, for forced type conversion
short sh=num; //error
short sh=(short)num;//correct, for Force type conversion
! Note:
When a forced type conversion is performed, data may be lost.

Common operators

Operators are symbols that tell the program to perform specific operations. Java provides 6 types of operators, which are assignment operators, arithmetic operators, relational operators, logical operators, bit operators and conditional operators.
1. Assignment operator The
assignment operator "=" is used to assign variable values ​​to variables, and can be combined with arithmetic operators to form a compound assignment operator. Compound operators mainly include: "+=", "-=", "/=", "%=".

2. Arithmetic Operators
Arithmetic operators include: "+", "-", "*", "/", "%", "++", "–". There are also two special symbols: "_" and "$".

3. Relational operators
Relational operators are sometimes called comparison operators. They are used to compare the size of two constants or variables. The result of the operation is Boolean true or false. There are six relational operators in Java, namely: "==", "!=", ">", "<", ">=", "<=".

4. Logical operators
Logical operators are used to perform operations on two Boolean operands, and the result is still a Boolean value.

5. Bit operators.
Bit operators are mainly for binary. On the surface, they seem a bit like logical operators, but logical operators are for two relational operators to perform logical operations, while bit operators are mainly for two binary numbers. Perform logic operations on bits.

6. Conditional operator
Conditional operator is the only operator in Java that requires three operands, so it is also called ternary operator or ternary operator.

Syntax format:
condition? Expression 1: Expression 2
first judges the condition. If the result is true, then the value of expression 1 is returned;
if the result is false, the value of expression 2 is returned.

Various operators in Java have their own precedence and associativity.
The so-called priority is the order of operations during expression operations. The higher the priority, the higher the order of operations in the expression.
Tuberculosis can be understood as the direction of operation. The associative type of most operators is from left to right, that is, from left to right.

Guess you like

Origin blog.csdn.net/weixin_54524431/article/details/112603098