当前位置:网站首页>Data type conversion
Data type conversion
2022-07-25 22:28:00 【Start】
1. Automatic type conversion : A small range of data is stored in a large range of variables
2. Cast : Cast : A large range of data is stored in a small range of variables .
Be careful : It may be stored . It is also possible that a large range of data exceeds the range of a small range of data , At this time, the accuracy of the data will be lost
ublic static void main(String[] args) {
// Automatic type conversion : A small range of data is stored in a large range of variables
byte num1 = 100;
int num2 =num1;
System.out.println(num2);//100
// Cast : A large range of data is stored in a small range of variables . Be careful : It may be stored . It is also possible that a large range of data exceeds the range of a small range of data , At this time, the accuracy of the data will be lost
int num3 =129;
byte num4 =(byte)num3;
System.out.println(num4);//120
}
If there is a strong conversion between decimal and integer, precision may be lost
package cn.bdqn.Teacherdemo1;
public class Demo3 {
public static void main(String[] args) {
int before = 20; // apple Notebook market share
double rise = 9.8; // Increased share
int now = (int)(before + rise); // Current share
System.out.println(" Current share :"+now);//29 Loss of data accuracy
}
}
3. According to the function of the operator , We divide operators into the following categories
Arithmetic operator 、 Assignment operator 、 Relational operator 、 Logical operators 、 An operator And other operators
1. Arithmetic operator
For all kinds of numerical operations . Including plus (+)、 reduce (-)、 ride (*)、 except (/)、 Seeking remainder ( Or modular operation ,%)、 Self increasing (++)、 Self reduction (--) There are seven kinds of . Compound arithmetic operators :+= -= *= /= %=
2. Relational operator
Used in comparison operations . Include greater than (>)、 Less than (=)、 Less than or equal to (<=) And are not equal to (!=) Six kinds .
The operation result of relational operator is Boolean . Either for true, Either for false.
= For the assignment operator ,== Is equal to operator
>、<、>=、<= The operator takes precedence over ==、!=
>、<、>=、<= Operator only supports data comparison of numeric type
3. Logical operators
For logical operations . Include and (&&)、 or (||)、 Not (!) Three .
4. Bit operation Operator
The amount of work involved , To operate in bits . Including bit and (&)、 Bit or (|)、 Bit non (~)、 Bit exclusive or (^)、 Move left (<>) Six kinds .
5. Assignment operator
For assignment operations , It is divided into simple assignment (=)、 Compound arithmetic assignment (+=,-=,*=,/=,%=) And compound bit operations
(&=,|=,^=,>>=,<<=) There are eleven kinds of three kinds .
4. Here we can distinguish ++ Self increasing -- The use of self subtraction
++ or -- The difference between before and after variables
For a single variable ,++ perhaps -- Whether before or after the variable , Variables will increase automatically 1 Or self reducing 1
For the variables involved in the operation ,++ or -- After the variable , Variables are involved in the operation first , After participating in the calculation , Then carry out self increase or self decrease
For the variables involved in the operation ,++ or -- Before the variable , Variables increase or decrease first , After self increase or self decrease , And then I'll take part in the calculation
public class Demo7 {
public static void main(String[] args) {
int num1 = 10;
int num2 = num1++;
System.out.println("num1=" + num1);// num1=11
System.out.println("num2=" + num2);// num2 = 10
int num3 = 10;
int num4 = ++num3;
System.out.println("num3="+num3);//num3 = 11
System.out.println("num4="+num4);//num4 = 11
}
}
5. Data exchange can be realized by assignment operator
Conditions required : Third party temporary storage
=
// Operator operator
public class Operator {
public static void main(String[] args) {
/*
* Data exchange with the help of assignment operators
* Need to bring three temporary storage
*/
int n=5;
int t =6;
int in;
in = n;
n =t;
t = in;
System.out.println(n+" "+t);//n=6,t=5
}
}
边栏推荐
- Basic principle of torque motor control
- Compile and decompile
- According to the use and configuration of data permissions in the open source framework
- 3 词法分析
- 淦,为什么 '𠮷𠮷𠮷' .length !== 3 ??
- 完啦,上班三个月,变秃了
- Get together for ten years, tell your story, millions of gifts are waiting for you
- internship:普通常用的工具类编写
- [PMP learning notes] Chapter 1 Introduction to PMP System
- LabVIEW develops PCI-1680U dual port can card
猜你喜欢

【集训DAY15】简单计算【树状数组】【数学】

Builder pattern

JVM内存区域

3dslicer introduction and installation tutorial

3dslicer import cone beam CT image

(1) Integrating two mapping frameworks of Dao

Title: give a group of arrays, arranged from large to small and from small to large.

访问者模式(visitor)模式

If it is modified according to the name of the framework module

【集训DAY13】Internet【并查集】
随机推荐
分享两个音乐播放地址
Common source code for ArcGIS development
【集训DAY13】Travel【暴力】【动态规划】
3 词法分析
Math programming classification
数据质量:数据治理的核心
3dslicer importing medical image data
Why is the integer type 128 to byte -128
Matrix of C language
LabVIEW 开发 PCI-1680U双端口CAN卡
2day
C语言逆序打印字符串的两种方法
Scratch seamless butt bron filter
Perform Jieba word segmentation on the required content and output EXCEL documents according to word frequency
关于getchar和scanf的使用示例及注意点
Share two music playing addresses
Leetcode 106. 从中序与后序遍历序列构造二叉树
What is the difference between minor GC and full GC?
VIM usage record
Wechat official account application development (I)