Java Syntax Basics 2

Source: Internet
Author: User

I. Variables and Constants

1. Scope of Action

The valid range of a variable is the area where the program code can access the variable, and an error occurs when the variable is accessed outside of the variable's region. In a program, variables are generally divided into "member variables" and "Local variables", depending on the area to which the variable is accessible.

(1) member variables

Variables defined in the class body are called member variables, and member variables are valid throughout the class. The member variable of a class can be divided into static variable and instance variable two kinds.

If the type of the member variable is preceded by the keyword static, such a member variable is called a static variable (class variable). The valid range of static variables can be cross-class and even within the entire application. For a static variable, in addition to being able to access it within the class that defines it, it can be used in other classes directly in the "class name. Static variable" mode.

(2) Local variables

Variables defined in the method body of a class (a variable declared in code between "{" and "}") are called local variables. A local variable is valid only in the current code block, and is commonly understood to be valid within the curly braces it defines, except that the curly brace is ineffective and cannot be called in other classes.

The life cycle of a local variable depends on the method, and when the method is called, the Java Virtual machine allocates memory space for the local variable in the method, and when the call to the method ends, the memory space occupied by the local variable in the method is freed and the local variable is randomly destroyed.

Note:

The local variable can be the same as the name of the member variable, at which point the member variable is hidden, that is, the member variable is temporarily invalidated in this method. If you want to invoke a member variable, you need to use the "class name. Static variable" call.

Two. Operators

1. Assignment operators

The assignment operator, "=", is a two-tuple operator (that is, two operands are processed), and its function is to assign the value of the right-side operand to the operand to the left. The lexical format is: variable type variable name = assigned value;

The left must be a variable, and the value assigned to the right can be any number or expression, including a variable, constant, or valid expression.

2. Arithmetic operators

The arithmetic operators in Java mainly have + (plus),-(minus), * (multiplication sign),/(division sign), and% (remainder), which are all two-dollar operators.

where the "+" and "-" operators can also be used as positive and negative symbols of data.

3. Self-increment self-decrement operator

The self-increment, decrement operator is the monocular operator, which can be placed before the operand or after the action element. The operand must be an integral or floating-point variable. The self-increment, decrement operator, which is placed in front of the operand, adds 1 (minus 1) to the value of the variable before it participates in the operation of the expression, and the self-increment, decrement operator, which is placed after the operand, first causes the variable to participate in the expression's operation and then subtract the variable by 1 (minus 1).

Note:

+ + 、--operator: The precedence before the variable is higher than the variable, and the lowest priority is placed after the variable.

4. Comparison operators

A comparison operator is a two-tuple operator that is used for comparisons between variables and variables in a program, between variables and constants, and between other types of information. The result of the comparison operator is a Boolean and the result of the operation is true, otherwise the result is false. Comparison operators are often used in conditional statements as a basis for judgment.

5. Logical operators

Logical operators: Characters include && (&) (logical AND), | | (|) (logical OR) and! (logical), the return value is a Boolean type of expression, and the operand must also be a Boolean data. Logical operators can represent more complex conditions, such as linking several relational expressions, compared to comparison operators. In the logical operator, in addition to the "! "Is the unary operator, and the rest is the two-tuple operator.

6. Ternary operators

The ternary operator is the only three-mesh operator in Java, with 3 operands, the first being a conditional expression, and the remaining two values, and the first value when the conditional expression is set, and the second value when it is not established.

The format is: logical expression? Return value 1: return value 2;

7. Operator Precedence

Three. Conversion type

1. Automatic type conversion (implicit conversion)

Java 8 Basic types can be mixed operations, different types of data in the operation of the first automatically converted to the same type, in the operation. The data type is divided into high and low levels depending on the size of the storage space occupied, the lower the level of space occupied, the higher the level of space occupied, and the rules for automatic type conversion following low-to high-level conversions.

2. Forcing type conversions

When assigning the value of a variable of high precision to a variable of low precision, the display type conversion operation (also known as coercion type conversion) must be used.

When assigning integers to a bybe, short, int, long variable, the range of values for these variables cannot be exceeded, otherwise a data overflow will occur.

Four. Notes

By adding comments to the program code, you can increase the likelihood of the program, which contains information about the program that can help programmers better read and understand the program. Comment statements can be added anywhere in the Java source program file, and the text Java compiler in the note does not compile, and the comment text in all the code does not have any effect on the program. The Java language provides 3 ways to annotate, one-line comments, multiple-line comments, and document annotations.

1. Single-line Comment

"//" is a single-line comment mark, and everything from the symbol "//" until the line breaks is ignored by the compiler as a comment.

2. Multi-line annotations

"/* * * *" is a multiline comment marker, and everything between the symbol "/*" and "* *" is the content of the comment. The contents of the note can be wrapped.

Java Syntax Basics 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.