java numerical comparison
import org.junit.Test; public class NumberCompare { /** * 数值比较,以Float为例 */ @Test public void testName1() throws Exception { // Float装箱 Float f = 12.1f; // 等同:Float f = new Float(12.1); Float f2 = 12.1f; /* * 结果全是false */ System.out.println(f.equals(12.1)); System.out.println(f == 12.1); System.out.println(f.floatValue() == 12.1); System.out.println(f == f2); /* * 为什么结果全是false? * -------------------------------- * 1. equals()方法 * 如果是引用类型间的比较: * equals()方法的参数可以传入任何对象。但是如果[传入参数的数据类型]和[方法所属对象的类型]不一致,就会直接返回false。 * 只有[传入参数的数据类型]和[方法所属对象的类型]一致时,才会继续对值进行比较。 * * 如果是引用类型和基本类型进行比较: * 如果传入的参数时基本类型,会对参数自动装箱,变成引用类型间的比较。 * * 2. == 用于引用类型的比较和基本类型的比较。 * 如果是引用类型间的比较: * 直接比较内存地址,不同就返回false.不会对具体数值进行比较。 * * 如果是基本类型间的比较: * 类型相同时,才会比较。类型不同时,会返回false,或直接编译不通过。 * * 如果是引用类型和基本类型进行比较: * 会将引用类型拆箱,转成基本类型后,在进行基本类型间的比较。 * * 总结: * 数据类型相同的值,才有比较的意义。 * 数据类型不同的值,不要放在一起比较。如果一定要比较,先转换成相同的类型再比较。 */ /* * 换个写法,结果全是true */ // equals() : 引用类型间的比较 System.out.println(f.equals(f2)); System.out.println(f.equals(new Float(12.1))); // equals() : 引用类型和基本类型的比较 System.out.println(f.equals(12.1f)); // 会先将基本类型装箱 // == : 基本类型间的比较 System.out.println(f.floatValue() == 12.1f); // == : 引用类型和基本类型的比较 System.out.println(f == 12.1f); // 会先将引用类型拆箱 float f3 = 12.1f; System.out.println(f == f3); } }
The above is the detailed content of How to implement numerical comparison in java. 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


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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment