How can I initialize a double variable to infinity

纵然是瞬间 提交于 2020-02-29 19:44:59

Q. How can I initialize a double variable to infinity?

A. Java has built-in constants available for this purpose: Double.POSITIVE_INFINITY

and Double.NEGATIVE_INFINITY.

Q. Can you compare a double to an int?

A. Not without doing a type conversion, but remember that Java usually does the requisite type conversion automatically. For example, if x is an int with the value 3, then

the expression (x < 3.1)  is true—Java converts x to double (because 3.1 is a double

literal) before performing the comparison.

Q. What are the values of 1/0 and 1.0/0.0 as Java expressions?

A. The first generates a runtime exception for division by zero (which stops your program because the value is undefined); the second has the value Infinity

Q. Why do we say (a && b) and not (a & b) ?

A. The operators &, | , and ^ are bitwise logical operations for integer types that do and,

or, and exclusive or (respectively) on each bit position. Thus the value of 10&6 is 14 and

the value of 10^6 is 12. We use these operators rarely (but occasionally) in this book.

The operators && and ||  are valid only in boolean expressions are included separately

because ofshort-circuiting: an expression is evaluated left-to-right and the evaluation

stops when the value is known.


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!