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

What are the methods of data comparison in Java

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you what are the relevant knowledge points about the data comparison method in Java, the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

1. Description

Compare the basic type = =, compare object values and recommend equals or compareTo.

First of all, the data in Java is stored in JVM, while the basic type of data is stored in JVM's local variable table, which can also be understood as the so-called "stack".

You can compare the equality of values of type int by = =, but note that int and Integer are completely different, one is a basic type, and the other is an object.

2. Examples

Compare basic type values

Int I = 11111111 int j = 11111111 * * system. Out.println (I = = j)

Instance expansion:

Java numerical comparison

Import org.junit.Test;public class NumberCompare {/ * numerical comparison, take Float as an example * / @ Test public void testName1 () throws Exception {/ / Float packing Float f = 12.1f; / / equivalent: Float f = new Float (12.1); Float f2 = 12.1f / * * the results are all 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) / * * Why are all the results false? *-* 1. Equals () method * if it is a comparison between reference types: * the parameters of the equals () method can be passed to any object. However, if the data type of the incoming parameter is inconsistent with the type of the object to which the method belongs, false will be returned directly. * the comparison of values will continue only if [the data type of the input parameter] and [the type of the object to which the method belongs] are the same. * * if the reference type is compared with the basic type: * if the parameter is the basic type, the parameter will be boxed automatically and become a comparison between reference types. * * 2. = is used for comparison of reference types and comparison of base types. * if it is a comparison between reference types: * compare memory addresses directly, if different, return false. Specific values are not compared. * * if it is a comparison between basic types: * the comparison will only be made if the type is the same. When the type is different, false is returned, or the compilation fails directly. * * if the reference type is compared with the basic type: * the reference type will be unboxed and converted to the basic type, and the comparison between the basic types will be made. * * Summary: * only values with the same data type are meaningful for comparison. * do not compare values with different data types. If you have to compare, convert it to the same type before comparing. * / / * * to put it another way, the result is all true * / equals (): comparison between reference types System.out.println (f.equals (f2)); System.out.println (f.equals (new Float (12.1) / / equals (): comparison between reference types and base types System.out.println (f.equals (12.1f)); / / basic types are boxed first / / =: comparison between basic types System.out.println (f.floatValue () = = 12.1f) / / = =: comparison of reference types and basic types System.out.println (f = = 12.1f); / / will first unbox reference types float f3 = 12.1f; System.out.println (f = = f3);}} these are all the contents of this article entitled "what are the methods of data comparison in Java?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please 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