The Declaration and use of day01_09 variables in Java basic syntax

Source: Internet
Author: User
Tags ranges

Concept:

A storage area in memory.

The zone has its own name (variable name) and type (data type).

Java is a strongly typed language, and each variable in Java must be declared before it is used.

The data in this area can be constantly changed within the same type.

Attention:

Scope of the variable: valid between a pair of {}.

Initialize value

Define the format of the variable: data type variable name = Initialize value

Variables are accessed through the variable name to the area.

Variables are categorized by scope (declared position):

1. Member Variable: A variable (also called a global variable) that is defined inside the outside of the method and within the class. can be accessed anywhere in a class.

Local variable: A variable defined inside a method or a block of statements.

2. Points to note: Outside of the class, the outside of a pair of curly braces cannot have a variable declaration.

Variables are divided by the data type they belong to:

1. Basic Data type variables

2. Reference data type variables

Categories of variables:

1. Classification by data type

Integer type: Byte, short, int, long

Java integer types have a fixed number of table ranges and field lengths that are not affected by the specific OS to ensure portability of Java programs. The integer constants in Java are by default int, declaring long constants to be followed by ' l ' or ' l '

Float type: float, double

Like integer types, Java floating-point types also have a fixed number of table ranges and field lengths that are not affected by the specific OS. The Java floating-point constant defaults to type double, declaring a float constant, followed by ' F ' or ' F '.

There are two types of expressions for floating-point constants:

1, decimal number form: such as: 5.12 512.0f. 512 (must have decimal points)

2, scientific counting method: such as: 5.12e2 512E2 100E-2

Character type: Char

Char data is used to denote "characters" in the usual sense.

Three representations of character constants:

1. The character constant is a single character enclosed in single quotation marks ("'), covering all the written words of the world. For example: char c1 = ' a '; char C2 = ' Medium '; char C3 = ' 9 ';

2. Java also allows the use of the escape character ' \ ' to convert subsequent characters into special character constants. For example: char c3 = ' \ n ', ' \ n ' indicates a newline character

3. Use Unicode values directly to represent the character type constants: ' \uxxxx '. where xxxx represents a hexadecimal integer. such as: \u000a means \ n.

The char type is available for operation. Because it should have Unicode values.

Boolean Type: Boolean

Boolean types are suitable for logical operations and are generally used for program flow control:

If condition control statement;

While loop control statement;

Do-while loop control statement;

For loop control statement;

Boolean data only allows values of true and false, and cannot replace true and false with integers of 0 or not 0, which differs from C language.

Basic Data type Conversions

Automatic type conversions: small-capacity types are automatically converted to large-capacity data types. The data types are sorted by capacity size:

When there are multiple types of data blending operations, the system first automatically converts all data into the type of data with the largest capacity before it is calculated. Byte,short,char do not convert to each other, and the three of them are first converted to the int type when calculating. When the value of any base type is concatenated with a string value (+), the value of the base type is automatically converted to the string type.

Forcing type conversions

The inverse process of automatic type conversion converts a large-capacity data type to a small-capacity data type. Use the cast character (()), but it may result in a decrease in accuracy or overflow, especially to be aware. In general, strings cannot be converted directly to basic types, but wrapper classes that correspond to basic types can be implemented to convert strings to basic types. such as: String a = "43"; int i = Integer.parseint (a); The Boolean type cannot be converted to another data type.

The Declaration and use of day01_09 variables in Java basic syntax

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.