Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert Java basic data types to each other

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article is a detailed introduction to "how to convert Java basic data types to each other". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how to convert Java basic data types to each other" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.

1. Automatic type conversion 1. Definition

Java programs automatically convert low precision to high precision when assigning or calculating.

2. Data types sorted by precision

char -> int -> long -> float -> double

byte -> short -> int -> long -> float -> double

3. use details

When there are multiple data mixed operations, the system automatically converts all data into the largest data type at first, and then calculates.

An error is reported when assigning a data type with precision large enough to be small. Note that when assigning values, first determine whether it is within the range of the small-precision data type. If yes, it is OK. If variable assignment is performed, it is not allowed.

Byte, short and char cannot be automatically converted to each other.

Byte, short and char can be computed and converted to int type when computed.

boolean Type does not participate in conversion.

Auto-promotion principle: The type of the result of an expression is automatically converted to the largest type of operand.

2. Forced type conversion 1. Definition

The inverse of automatic type conversion, replacing large data types with small data types. When used with a cast character, but may cause precision degradation or overflow.

2. use details

Coercive type casting works only on the nearest operand, often using parentheses to raise precedence.

char type can hold int constant value, but can not hold int variable value, need strong conversion.

public class ForceTest{ public static void main(String []args){ int x = (int)(10*3.5+6*1.5);//Correct int y = (int)10*3.5+6*1.5; }}3. Conversion between basic data type and String type 1. Conversion from basic data type to String type

The value of this type + "" is enough.

int n1 = 100; String str1 = n1 + "";2.String type to primitive data type

The parseXX function can be called through the wrapper class of the basic data type, but it is necessary to ensure that the String type can be converted to valid data, for example,"123" can be converted, but "hello" cannot be converted to an integer. If the format is not correct, an exception will be thrown and the program will be terminated.

String Str1 = "345";Integer.parseInt("123");Double.parseDouble ("123.5");Float.parseFloat ("123.3");Short.parseShort("123");Boolean.parseBoolean("true");Long.parseLong ("123456");Byte.parseByte("12");//The charAt(int a) function Str1.charAt(a) is called to convert a string to a character type;//indicates the a+1th character in the string Str1.charAt(0)='3'; Read here, this article "Java basic data types how to convert each other" article has been introduced, want to master the knowledge points of this article also need to practice to understand, if you want to know more related content of the article, welcome to pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report