search
HomeJavajavaTutorialHow to convert between Java basic data types

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

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

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

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

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

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

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

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]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

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

How can I use Java's RMI (Remote Method Invocation) for distributed computing?How can I use Java's RMI (Remote Method Invocation) for distributed computing?Mar 11, 2025 pm 05:53 PM

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.

How do I use Java's sockets API for network communication?How do I use Java's sockets API for network communication?Mar 11, 2025 pm 05:53 PM

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

How can I create custom networking protocols in Java?How can I create custom networking protocols in Java?Mar 11, 2025 pm 05:52 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

mPDF

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

Zend Studio 13.0.1

Powerful PHP integrated development environment