Variables and basic data types for Java Syntax basics

Source: Internet
Author: User

First, the concept of variables

A variable is a basic storage unit in a Java program. A variable is a combination definition of an identifier, a type, and an optional initial value. All variables have a scope, that is, the variable is valid in a region.

The basic variables are declared in the following way:

Type identifier [=value];

Where type is the data type of Java, identifier is the name of the variable, specifying a symbol or a value for the initialization variable. The initialization expression must produce a value that is the same as or compatible with the specified variable type. When declaring multiple variables of a 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;

II. Basic data types

There are 8 basic data types in Java: Byte (Byte), short, Integer (int), long, character (char), floating-point (float), double-type (double), Boolean (bool). These types can be divided into 4 groups.

Integers: Includes byte type (byte), short Integer, Integer (int), and long Integer. They are all signed integers.

Floating-point type: Includes floating-point (float), double-precision type (double). The table has a number of decimal precision requirements.

Character: includes the character type (char). A symbol that represents a character set.

Boolean: Includes the Boolean type (Boolean). A special type with only two values, true and false, respectively.

1, Integer

The range of values for the 4 integers is shown in the following table:

Type name Size/bit Range of values
Byte 8 -128~127
Short 16 -32,768~32767
Int 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

These data are signed and all integer variables are not reliably stored outside their range of values, so be careful when selecting data types.

2. Floating point number

The value range of 2 floating-point numbers is as follows table

Type name Size, bit Describe Range of values
Float 32 Single precision 3.4e-038~3.4e+038
Double 64 Double precision 1.7e-308~1.7e+308

3, Character type

Char types are used to store single characters such as letters, numbers, and other symbols. The character type can only represent a single character, and the value of the character type is a single quotation mark at both ends of the character, such as ' H '.

Common escape characters in Java:

Escape character Meaning
\ n Wrap, move the cursor to the beginning of the next line
\ t Horizontal tab (Tab key) to move the cursor to the next tab position
\b The cursor is returned in a grid, equivalent to the BACKSPACE key
\ r Carriage return, moving the cursor to the beginning of the current line without moving to the next line
\\ Back slash
\‘ Single quote '
\" Double quotes "

4. Boolean type

Boolean is used to store Boolean values, with only two Boolean values in Java, either True or false.

Iii. initialization and scope of variables

1. Initialization

Variables can be initialized at the time of declaration, or they can be initialized by an assignment statement after declaration. The initialized variable can still be re-assigned through the assignment statement.

2. Scope

    • Scope (scope) determines the range that a variable can use.

    • Global variables: Variables can be accessed throughout the class.

    • Local variables: Variables can only be accessed in the code snippet in which they are defined.

    • Scope rule: A variable defined in a code snippet can only be visible in that code snippet or in the child Code snippet of that code snippet.

Iv. type conversion of variables

1. Automatic type conversion (implicit conversion)

Multiple mutually compatible data types are automatically converted to a wide range of data types when they are operated in an expression.

Conditions in which automatic type conversions occur:

    • Both types are compatible with each other.

    • The destination type value range is greater than the source type.

2. Forcing type conversions

When two types are incompatible with each other, or if the target type has a range of small source types, the automatic conversion cannot be performed, which requires coercion of type conversions. The mandatory type conversion format is as follows:

Target type variable = (target type) value

Such as:

int a = 3;

byte B = (byte) A;

Five, constant

Constants are constant values in the program, and they are data that cannot be changed, and the variables that use the final modifier are constants. Such as:

Final double PI = 3.14;

The content of this article is more, but basically is a conceptual thing, the next one will introduce operator-related knowledge points.

This article is from the "Jianggujin blog" blog, make sure to keep this source http://jianggujin.blog.51cto.com/6808292/1715706

Variables and basic data types for Java Syntax basics

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.