Java's basic grammar (B): constants, data types, variables,

4. Constant

① Definition: during program execution, it can not change the value of

. ② Category:

Integer constants: all integers

Decimal constants: All decimals

A character constant: in single quotation marks content, which can only put a single number / single letter / single symbol (can be a space character, but you can not hold anything)

String constants: content with double quotation marks (length may be 0, i.e., an empty string, "")

Boolean constants: true and false only

Empty constants: null

5. Data Type

① significance:. Java is a strongly typed language, for each data clearly defines the specific type of data in memory with a memory space allocated different sizes

. ② Category: basic data types, reference data types

Basic data types (eight kinds of class 4): integer, floating-point (decimal type), character, Boolean

Types of kind Occupies byte size Ranges
Integer byte A byte -128 ~ 127
short Two bytes -2^15 ~ 2^15-1
int Four bytes -2^31 ~ 2^31-1
long Eight bytes -2^63 ~ 2^63-1
Float float Four bytes Single-precision
double Eight bytes Double
Character char Two bytes 0 ~ 65535
Boolean boolean No definite size true and false

 

 

 

 

 

 

 

 

 

Reference data types: string, array, classes, interfaces, etc.

note:

A. the Java int type integer default, the default is decimal type double 

b. When assigned to a variable, if a float / long type, F or L need to add value to do after the variable identifier (recommended capital letters)

③. Data type conversion

Implicit conversion : data range by the Small -> Large

int A = 10 ;
 byte B = 20 is ; 
implicit conversion: A = A + B;   // calculation, the byte will default to an int type lifting

Cast : a large range of data -> Small

int A = 10 ;
 byte B = 20 is ; 
Strong transfer format: B = ( byte ) (A + B);   // calculation, a + b is int results Default 
Note: If you exceed the data type of the variable to be assigned to take value range, then the overflow data will be an integer, fractional loses precision

6. variables

① Definition: during program execution, its value may change occurs in an amount within a certain range

Format: variable name = data type variable value;

note: 

a. in the same area you can not use the same variable name

b. Local variables must be assigned before use (if only statement without using may not be assigned)

. C statement can define a plurality of variables (chestnut: int a, b, c ...;)

. D variables not exceed the scope range (chestnut: local variables declared within a block of code in which only the valid block {})

. ☆ expand:

. ① variables together and adding the constant difference:

byte A =. 3 ;
 byte B =. 4 ;
 // variables together 
byte C1 = A + B;
 // constant sum 
byte C2 = +. 4. 3 ; 
CONCLUSION: 
variables together: the stored value of the variable is varied, when compiled can not determine the particular value of the variable, the result of addition may exceed the range byte 
constants are added: Java compiler optimization mechanism with a constant, 3, and 4 after the lifting of int determines whether the result calculated in the byte range Inside

. ② mixing type conversion operations:

A . Integer, floating-point, character of the mixing operation:
 byte , Short , char -> int -> Long -> a float -> Double 
. 1) byte, without conversion between short, char, will automatically upgrade type int;
 2 .) when the mixing operation of other types of small data type is automatically elevated to a large;
2 characters and strings involved in computing: any data types + number string is connected, will generate a new string
chestnut:
'A' +. 1 : output 98 (ASCII code table, a is the value corresponding to the character int 97) ( char ) ( 'A' +. 1 ): output b characters "Hello" + 'A' +. 1 : output helloa1 'A' + +. 1 "Hello": output 98hello

③.ASCII code table

↓ ASCII code table, please stamp here: ↓

https://www.sojson.com/asciitable.html

 

Guess you like

Origin www.cnblogs.com/Baker-Street/p/11707079.html