In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "java how to achieve numerical comparison", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "java how to achieve numerical comparison" this article.
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) / / =: compare the reference type with the basic type System.out.println (f = = 12.1f); / / unbox the reference type first float f3 = 12.1f; System.out.println (f = = f3);}} above is all the content of the article "how java achieves numerical comparison". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.