In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces what will be output of System.out.println (1.0 System.out.println 0) in java, which has certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article. Let's take a look at it.
Let's first try package erdan.demo;public class TestDouble {public static void main (String [] args) {System.out.println (1.0 / 0);}}
What you think, what I think, it should throw an ArithmeticException exception.
But it now outputs Infinity.
Why?
The Double data type supports infinity. Are there any other types that support it?
Yes, and Float. Let's take a look at the Double source code. We can see / * a constant that maintains the positive infinity of the type * / public static final double POSITIVE_INFINITY = 1.0 / 0.0X infinity * * a constant, and the negative infinity of the type * / public static final double NEGATIVE_INFINITY =-1.0 / 0.0 / * A constant, non-numeric type * / public static final double NaN = 0.0d / 0.0
Let's experiment with 0.0Univer and-1.0ppm.
Double positive infinity = 1.0 / 0 * * double negative infinity =-1.0 / 0
System.out.println ("positive infinity:" + positive infinity); System.out.println ("negative infinity:" + negative infinity); Double non-numeric value = 0.0amp 0; System.out.println ("non-numeric 0.0infinity 0->" + non-numeric value); output:
Positive infinity: Infinity negative infinity:-Infinity non-numeric 0.0Universe 0-> NaN
Operate on infinity next to test infinity of Float type and Doubloe type public static void testFloatInfinity () {Float infFloat = Float.POSITIVE_INFINITY; Double infDouble = Double.POSITIVE_INFINITY; System.out.println ("infFloat + 5 =" + (infFloat + 5)); System.out.println ("infFloat-infDouble =" + (infFloat-infDouble) System.out.println ("infFloat *-1 =" + (infFloat *-1));} output:
InfFloat + 5 = InfinityinfFloat-infDouble = NaNinfFloat *-1 =-Infinity can notice that the three-line operation is consistent with our expected results.
Ps: the result of Infinity- Infinity is not a numeric type
Judge these values
Public static void checkFloatInfinity () {Double positive infinity = 1.0 / 0; Double negative infinity =-1.0 / 0; Double non-numeric value = 0.0 / 0; System.out.println ("judge positive infinity:" + Double.isInfinite (positive infinity); System.out.println ("judge negative infinity:" + (Double.NEGATIVE_INFINITY = = negative infinity)) System.out.println ("determine non-numeric value (= =):" + (Double.NaN = = non-numeric value); System.out.println ("determine non-numerical value (isNaN):" + Double.isNaN (non-numeric value)) } output: judge positive infinity: true judge negative infinity: true judge non-numerical value (= =): false judge non-numerical value (isNaN): true thank you for reading this article carefully. I hope the article "what will be output of System.out.println (1.0cm 0) in java" shared by the editor will be helpful to you. At the same time, I hope you will support it. Pay attention to the industry information channel, more related knowledge is waiting for you to learn!
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.