当前位置:网站首页>Type conversion basis

Type conversion basis

2022-06-25 21:22:00 vancomycin

Automatic type conversion :

1. The two types are compatible

2. The target type is greater than the original converted type It's like filling a small cup with water and pouring it into a large one

short s = 123;

int i = s; int The value range of is relative to short Much bigger , So when you convert a type, you automatically convert it

2. Cast

     1.   The two types are compatible        
     1.   The target type is smaller than the converted type       Pour the water in the big cup into the small one    If it is full, the water will overflow 

short s = 123;

byte b = (byte)s; Bold font Cast No error will be reported during compilation But the result of compilation may not be correct

3. Cast

The integer length is sufficient , Data integrity . example :int i = 100; byte b = (byte)i; //b = 100 Insufficient integer length , Data truncation . example :int i = 10000; byte b = (byte)i; //b = 16( Sign bit change , May become negative )

int It's four bytes , So we can see from the following pictures stay int When displayed in hexadecimal ( The second line of the picture is yellow ) It can be seen that

byte When it is a hexadecimal byte, you can see from the first line of the picture

When converting large data types to small data types , The first one is cut off , Only the last eight bits are calculated

The following operation 0 Is a positive number 1 It's a negative number

 

Decimals are forced to integers , Data truncation . example :double d = 2.5; int i = (int)d; //i = 2( The decimal places are rounded off ) Characters and integers are converted to each other , Data integrity . example :char c = 65; int i = c; //i = 65

3. Automatic type promotion

When calculating : One of the two operands is double, The calculation result is improved to double. If there is no... In the operand double, There is one for float, The calculation result is improved to float. If there is no... In the operand float, There is one for long, The calculation result is improved to long. If there is no... In the operand long, There is one for int, The calculation result is improved to int. If there is no... In the operand int, Are all short or byte, The calculation result is still improved to int.

special : Any type and String Add up (+) when , It's actually splicing , The result is automatically promoted to String.

2. Arithmetic operator Operate on two operands

+ Add 、 Sum up
- reduce 、 Subtraction
* ride 、 quadrature
/ except 、 Seeking quotient
% model 、 Seeking remainder

The arithmetic operator is right Simple addition, subtraction, multiplication, division and remainder of data

++ Increasing , A variable's value +1
- - Decline , A variable's value -1

 

a++ a Self increasing of

a++ Express a = a+1

a++ ++ before ++ It's different to do the operation after

If you output a single statement, there is no problem

If it's not a separate statement

a++ First perform other operations before proceeding ++ operation

++a Progressiveness self increasing operation Doing other operations

a-- a Self subtraction of

a-- = a-1

  • Before means before ++ perhaps -- Then perform other

  • After means to execute other first Re execution ++ perhaps --

    3. Assignment operator

    1.

    = assignment
    += Assignment after summation
    -= Assign value after difference
    *= Assign value after quadrature
    /= Assignment after division
    %= Assign a value after taking the remainder

    There is only one thing to note Every time the assignment operator takes the remainder after addition, subtraction, multiplication, division, etc In assignment

    4. Relational operator

    1.

    > Greater than
    < Less than
    >= Greater than or equal to
    <= Less than or equal to
    == be equal to
    != It's not equal to

    Only one thing to be sure of is The result of each relational operator is boolea type The results are True or false

    5. Logical operators

    1

    Logical operators Two boolea Logical comparison of operands or expressions of type

    && And ( also ) Two operands , It's true at the same time , The result is true
    || or ( perhaps ) Two operands , One is true , The result is true
    ! Not ( Take the opposite ) Meaning for “ No ”, True is false , False is true

Ternary operator Assign the result of the judgment to the variable

summary :

Variable : A piece of storage space in computer memory , Is the basic unit for storing data . data type : Basic data type (8 Kind of )、 Reference data type (String、 Array 、 object ). Operator : Arithmetic operator 、 Assignment operator 、 Relational operator 、 Logical operators . Type conversion : Automatic type conversion 、 Cast . Type promotion : Regular type promotion between numbers , Special type promotion of characters . Console entry : Introduce the toolkit 、 Statement Scanner、 Call the corresponding function to receive the console input data .

原网站

版权声明
本文为[vancomycin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202181329516161.html