[Road to final review] JAVA (2) C

Series Article Directory

This series is quite troublesome and tiring, but it will definitely be reissued, and it will not miss everyone. Thank you for your support.


Table of contents

1. Explanation

2. Automatic type promotion

Three forced type conversion

Operation of four basic data types and string

Five Summary


foreword

In this issue, we will update the last part of our second part, the operation rules between variables of basic data types, and we will update a chapter later, which is dedicated to operators, so that our second part is completely over, and we can proceed with our flow control statement.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Explanation

In a Java program, there are eight basic data types, among which, except for the boolean type, the values ​​of variables often need to be converted to each other. One is automatic type conversion and the other is mandatory type conversion

2. Automatic type promotion

Auto-promote narrow-scoped types to wide-scoped types

int a = 'A';
double d =10;
long num 

byte/char/short<int<long<float<double

1. When assigning a value with a small storage range (constant variable...) to a variable with a large storage range

int i = '北辰'  //北辰是char类型(char类型可以是一个字母,一个汉字,或者一个字符)
                因为char类型小于int类型,所以右面的自动转化为int类型
double='10'     //因为int类型小于double类型,所以int变为double

//还有long类型记得加l

2 When the storage range and the storage range are small, it becomes a mixed operation and becomes the largest type operation

int i = 1;                //int,byte都比double小,所以运算结果为double类型
byte b1 = 1;
double c1 = 1.0;
double sum = i + b1 + c1; 

Mixing complete into maximum

3 byte short char type mixed operation becomes int

byte b1 = 1;       //byte加上byte变为int类型而不是byte
byte b2 = 1;
int b3 = b1 + b2;

The result of byte and byte operations should be int, not byte b3 = b1 + b2

char a1 = 'a';        //char和char运算,最少要int来接收
char b1 = 'b';
int  c1  =a1 +b1;
System.out.println(a+b);

Three forced type conversion

Coerces a type with a large range to a type with a small range of values

Here we have a conversion format:

Data type 1 variable name = (data type 1) is forced to convert data value

The value in () must be less than or equal to the data type of the variable value

1 When the forced change of a large data range is a small data range, precision may be lost or overflow

int i = (int)3.14;//损失精度

double d = 1.2;
int num = (int)d;//损失精度

int i = 200;
byte b = (byte)i;//溢出

2 When a value wants to promote the data type, you can also use the mandatory data type. In this case, there is no risk, and it can usually be omitted

int i = 1;
int b = 2;
double bigger = (double)(i/j);

3 For variables of type long, the suffix can be omitted, but not for float

long l1 = 123L;
long l2 = 123;            //这时可以看做为是int类型升级为long类型
long l3 = 123456789012;    //会报错,因为超过了long的范围
long l4 = 123456789012L;    
float l5 = 12.345;           //12.345会被认为是double类型,不能自动转换
float l6 = 12.3F;
float i7 = (float)12.3;

Operation of four basic data types and string

1 String type: string

string is not a basic data type, but a reference data type

Use a pair of " " to represent a string

There can be 0, one, or more characters inside

2 operation rules

         1. Any eight basic data types and string type can only be connected + operation , but the result must be String type

System.out.println("" + 1 +2);
int num = 10;
boolean b1 = true;
String s1 = "abc";
String s2 = s1 + num + b1;    //abc10true
System.out.println(s2);      
string s3 = num + b1 + s2;        // //num+b1是int运算,不能连接了
string s4 = s1 + (num +b1 ); 

 Operations from left to right        

        2. String cannot be cast () converted, converted to other types but can be converted with the help of the method of the wrapper class

(will be mentioned later)

Five Summary

This is the basic data type conversion. You can find some more questions to practice. This is our Java 2 (C) part, followed by our operators. This part is arranged in part D, which is what everyone must master. D is our last part, and we will update the second part before tomorrow, and we will open the process statement and enter our Java 3 (A).

Guess you like

Origin blog.csdn.net/m0_69520030/article/details/130098084