In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of the automatic type conversion mechanism in java, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
An overview of automatic type conversion mechanism parsing
Automatic type conversion is also called implicit type conversion.
The data type of the expression is automatically raised
All byte, char, and Byte values will be promoted to int.
If an Operand is of type long, the result is type long.
If an Operand is float, the result is float.
If an Operand is an Operand, the result is an Operand.
The data type will only be raised automatically, not reduced automatically.
Int values can be assigned to long, float, and byte variables, but not to byte, short, char variables.
The same is true for passing parameters of a function
Of course, in the case of function overloading, the java compiler will automatically select the best matching function to call
The default data type for integers in Java is the int type
All types whose length is less than int (byte, short, char) will be promoted to int after operation.
Of course, there is also the following situation, because when we perform the assignment operation, the java compiler can clearly know whether the result of the operation exceeds the range of byte or short, so byte a = 1 + 1; does not report an error. The reason for the above byte c = a + b; compilation error is that an and b are both variables, and the compiler does not know whether the added result will exceed the value range of byte, so the compiler promotes the result to int.
A brief summary:
When the compiler clearly knows that the result of the integer operation does not reach the representation range of int, the operation result of the byte, short or char type will not be automatically promoted to the int type.
When the compiler explicitly knows or does not know whether the result of the integer operation reaches the range of int, the compiler will automatically convert the result of the operation to int, even if it is originally of byte, short, or char type.
Automatic type conversion & forced type conversion when type conversion occurs
A: assignment | during operation, type conversion occurs when the data types on both sides are inconsistent.
As follows:
Public class TypeTest {public static void main (String [] args) {/ / implicit type conversion occurs when two integers are divided into an integer byte a = 3; byte b = 4; int num = a + b; System.out.println (num); / / 7 / / implicit type conversion occurs when assigning a value int ch ='0' System.out.println (ch); / / 48 / / cast during operation byte A1 = 12; byte a2 = 12; byte num1 = (byte) (A1 + a2); System.out.println (num1); / / 24 / / cast during assignment short b3 = 1234; byte A3 = (byte) b3 System.out.println (A3); / /-46}}
Run the screenshot:
Type conversion classification
Automatic type conversion
Forced type conversion
Automatic type conversion (implicit type conversion)
Rule: from small to large, low bytes are automatically raised to high bytes
Order:
Byte (1 byte)-> short (2 bytes)-> int (4 bytes)-> long (8 bytes)-> float (4 bytes)-> double (8 bytes)
Char (2 bytes)-> int (4 bytes)-> long (8 bytes)-> float (4 bytes)-> double (8 bytes)
Drawing analysis:
Code display:
Public class TypeDemo {public static void main (String [] agrs) {/ / byte-- > short byte b1 = 127; short S1 = b1; System.out.println (S1); / / 127 / / short-- > int short S2 = 30000; int I = S2; System.out.println (I); / / 30000 / / int-- > long int num = 2100000000 Long lg = num; System.out.println (num); / / 2100000000 / / long-- > float long lg1 = 2000000000000L; float F1 = lg1; System.out.println (F1); / / 200.000001 trillion / / float-> double float f2 = 3.14f; double D1 = f2; System.out.println (D1) / / 3.140000104904175 / / char-- > int char ch = 'asides; int i1 = ch; System.out.println (i1); / / 97 / / char-- > long char ch2 =' baked; long lg2 = ch2; System.out.println (lg2); / / 98 / / char-- > double char ch3 ='c' Double dou = ch3; System.out.println (dou); / / 99.0 / / char-- > float char ch4 = 'dbath; float f3 = ch4; System.out.println (f3); / / 100.0}}
Run the screenshot:
Note:
Byte, short and char cannot be converted to each other
Code display:
Public class TypeDemo2 {public static void main (String [] agrs) {/ / byte-- > char byte bt = 127; char ch = bt; System.out.println (ch); / / short-- > char short sh = 12; char ch2 = sh; System.out.println (ch2);}}
Screenshot of compilation error:
Although float is 4 bytes, float represents a larger range of data than long. Indicates that the size of the data range is not necessarily related to the size of bytes.
Code display:
Public class TypeDemo3 {public static void main (String [] agrs) {long lg = 200000000000L; float F1 = lg; System.out.println (F1); / / 19.9999997 trillion}}
Run the screenshot:
Boolean types cannot participate in type conversion
Code display:
Public class TypeDemo4 {public static void main (String [] agrs) {boolean flag = 12; int flag1 = flag; System.out.println (flag1);}}
Screenshot of compilation error:
Cast (explicit conversion)
Rule: manual conversion from large to small, from high bytes to low bytes
Order:
Double (8 bytes)-> float (4 bytes)-> long (8 bytes)-> int (4 bytes)-> short (2 bytes)-> byte (1 byte)
Double (8 bytes)-> float (4 bytes)-> long (8 bytes)-> int (4 bytes)-> char (2 bytes)
Drawing analysis:
Format: target data type variable name = (target data type) variable | constant
Code display:
Public class TypeDemo5 {public static void main (String [] agrs) {/ / float-> long / / final float PI = 3.14f; / / long num = (long) PI; / / 3 / / float little = 3.14f; / / long num = (long) little; / / 3 long num = (long) 3.14f; System.out.println (num) / / 3 / / double-- > float / / double dou = 3.14; / / float little1 = (float) dou; / / 3.14 / / float little1 = (float) 3.14d; / / 3.14 final double dou = 3.14; float little1 = (float) dou; System.out.println (little1) / / 3.14 / / long-- > int / / long num1 = 200000000L; / / int num2 = (int) num1; / /-1454759936 / / int num2 = (int) 200000000L; / /-1454759936 final long num1 = 200000000L; int num2 = (int) num1; System.out.println (num2) / /-1454759936 / / int-- > short / / int num3 = 12; / / short num4 = (short) num3; / / 12 / / short num4 = (short) 40000; / /-25536 final int num3 = 60; short num4 = (short) num3; System.out.println (num4); / / 60 / / short-- > byte final short sh = 12345 Byte bt = (byte) sh; System.out.println (bt); / / 57 short sh2 = 78; bt = (byte) sh2; System.out.println (bt); / / 78}}
Run the screenshot:
Note:
Forced type conversion has data loss, which is generally not recommended.
Code display:
Public class TypeDemo6 {public static void main (String [] agrs) {short a = 1245; byte b = (byte) a; System.out.println (b);}}
Run the screenshot:
Thank you for reading this article carefully. I hope the article "sample Analysis of automatic Type conversion Mechanism in java" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.