Variables and basic data types based on JAVA syntax

Source: Internet
Author: User

Variables and basic data types based on JAVA syntax
1. The conceptual variable of a variable is a basic unit of storage in a Java program. A variable is a combination of identifiers, types, and optional initial values. All variables have a scope, that is, the variables are valid in a region. The basic variable declaration method is as follows: type identifier [= value]; where type is the Java data type, identifier is the variable name, and a symbol or value is specified as the initialization variable. The initialization expression must generate the same or compatible value as the specified variable type. When declaring multiple variables of the specified type, use commas to separate the variables. For example, the following methods are correct: int a; int B = 12; char c = 'C'; int d = 5, e; 2. There are eight basic data types in Java: byte, short, int, and long) char, float, double, and boolean ). These types can be divided into four groups. Integer: including byte, short, int, and long ). They are all signed integers. Float: Includes float and double ). This table has numbers with decimal precision requirements. Character: including character type (char ). Represents the character set symbol. Boolean: including boolean ). A special type with only two values, true and false. 1. The value ranges of four integer types are as follows: Type name size/bit value range: byte 8-128 ~ 127 short 16-32,768 ~ 32767int 32-2,147,483,648 ~ 2,147,483,647 long 64-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 the data is signed, and All Integer Variables cannot store data values beyond the valid range reliably. Be careful when selecting the data type. 2. The value range of two floating point numbers is as follows: Table type name size, bit description value range: float 32 single precision 3.4E-038 ~ 3.4E + 038 double 64 dual precision 1.7E-308 ~ The 1.7E + 3083 and character char types are used to store single characters such as letters, numbers, and other symbols. The character type can only represent a single character, indicating that the value of the character type is enclosed by single quotation marks at both ends of the character, such as 'H '. Common escape characters in Java: Meaning of the Escape Character \ n line feed, move the cursor to the start of the next line \ t horizontal tab (tab key ), move the cursor to the next tab position \ B move the cursor to the next position, which is equivalent to the backspace key \ r press enter and move the cursor to the beginning of the current row, do not move to the next line \ backslash \ 'single quote '\ "Double quotation marks" 4. boolean type is used to store boolean values. In Java, there are only two boolean values, or true, or false. Iii. Initialization and scope of variables 1. Initialization of variables can be performed during declaration, or after declaration, initialization can be performed through the value assignment statement. After initialization, the variable can still be assigned a value through the value assignment statement. 2. Scope determines the Scope of a variable. Global variables: variables can be accessed throughout the class. Local variables: variables can only be accessed in the code segment that defines them. Scope rules: variables defined in a code segment can only be visible in the code segment or the child code segment of the code segment. Iv. type conversion of variables 1. Automatic type conversion (implicit conversion) when multiple compatible data types are computed in one expression, will automatically convert to a wide range of data types. Condition for automatic type conversion: the two types are compatible with each other. The value range of the target type is greater than that of the source type. 2. Forced type conversion when the two types are not compatible with each other, or the value range of the target type is smaller than the source type, automatic conversion cannot be performed. This requires forced type conversion. The forced type conversion format is as follows: Target type variable = (target type) value such as: int a = 3; byte B = (byte); 5. A constant is a constant value in a program. It cannot be changed. A variable modified using final is a constant. For example: final double PI = 3.14; this article has many contents, but it is basically conceptual. The next article will introduce the knowledge points related to operators.

Related Article

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.