1. Automatic type conversion
1.Definition
When a Java program performs assignment or operation, it automatically converts small precision into large precision.
2. Data types are sorted by precision size
char —> int —> long —> float —> double
byte —> short —> int —> long —> float —> double
3. Usage details
When there are multiple data mixed operations, the system will first automatically convert all data into the data type with the largest capacity, and then perform calculations.
When assigning a data type with a high precision to a data type with a small precision, an error will be reported. Note that when assigning a numerical value, first determine whether it is within the range of the small-precision data type. If so, it is OK. If it is a variable assignment, it is not OK.
Byte, short and char cannot be automatically converted to each other.
byte, short and char can be calculated and converted into int type during calculation.
Boolean type does not participate in conversion.
Auto-promotion principle: The type of the expression result is automatically converted to the largest type among the operands.
2. Forced type conversion
1. Definition
The reverse process of automatic type conversion, converting data types with large capacity into data types with small capacity data type. A coercion character must be added when using it, but this may cause precision reduction or overflow.
2. Usage details
Forced type conversion is only effective for the most recent operand, and parentheses are often used to increase the priority.
The char type can save the constant value of int, but it cannot save the variable value of int and needs to be forced.
public class ForceTest{ public static void main(String []args){ int x = (int)(10*3.5+6*1.5);//正确 int y = (int)10*3.5+6*1.5;//报错 } }
3. Conversion between basic data type and String type
1. Convert basic data type to String type
Just "" the value of this type.
int n1 = 100; String str1 = n1 + "";
2. Convert String type to basic data type
Just call the parseXX function through the wrapper class of the basic data type, but make sure that the String type can be converted into valid data, for example, you can convert " 123" conversion, but "hello" cannot be converted into an integer. If the format is incorrect, 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"); //将字符串转换成字符型需要调用charAt(int a)函数 Str1.charAt(a); //表示字符串中第a+1个字符 Str1.charAt(0)='3';
The above is the detailed content of How to convert between Java basic data types. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

This article explains Java's Remote Method Invocation (RMI) for building distributed applications. It details interface definition, implementation, registry setup, and client-side invocation, addressing challenges like network issues and security.

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i

This article details creating custom Java networking protocols. It covers protocol definition (data structure, framing, error handling, versioning), implementation (using sockets), data serialization, and best practices (efficiency, security, mainta


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment