当前位置:网站首页>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 .
边栏推荐
- On merging and sorting
- How to solve the problem of flash write protection in STM32?
- ZABBIX foundation details
- Send a more awesome website, which can convert curl commands into code in any language
- VFS appears when mounting a file system from an SD card: cannot open root device "mmcblk1p2“
- Getting started and using postman
- Unable to connect to the server remotely locally using the Jupiter notebook
- Bank digital transformation layout in the beginning of the year, 6 challenges faced by financial level structure and Countermeasures
- Xshell mouse configuration
- Docker failed to remotely access 3306 after installing MySQL
猜你喜欢
Finger collar pin exclusive Medal
Rounding related calculation
Renren mall locates the file according to the route
IPv4 and IPv6 (powercert animated videos)
Writing manuals using markdown
Sqlmap for interface security testing
Mutual conversion of CString and char*
Differences between modems and routers (powercert animated videos)
Yolov4 reading notes (with mind map)! YOLOv4: Optimal Speed and Accuracy of Object Detection
Beginner to embedded development
随机推荐
Dbeaver offline installation driver
OLED driver learning based on ssd1306 (I): SSD Chinese command table (handling)
ThreadLocal class
Yolov4 reading notes (with mind map)! YOLOv4: Optimal Speed and Accuracy of Object Detection
05 configuring network parameters
Kubernetes related knowledge - surprise interview
JS__ Inheritance mode, namespace, object enumeration__ Duyi
A simple file searcher
01 network basics
Flexible scale out: from file system to distributed file system
OSI notes sorting
MySQL trigger
Finger collar pin exclusive Medal
[nail scenario capability package] hospital visitor verification
Win10 common software
lombok
Insert picture in markdown
Insert and update each database
What is API
Cvpr2019 | the latest cvpr2019 papers are the first to read!