绝对值java怎么表示_java绝对值怎么写?有什么特性?

绝对值有时候在java中十分有用,小伙伴们知道绝对值该如何编写吗?它有什么特性?下面就一起来看看吧。

一、绝对值函数

在Java中可以使用Math.abs()方法来方便的进行绝对值计算,例如:Math.abs(1.3-5.6);

自己写的话也是非常的简单的public Integer abs(Integer a){

return a>0?a:-a;

}

绝对值源码:/**

* Returns the absolute value of an {@code int} value.

* If the argument is not negative, the argument is returned.

* If the argument is negative, the negation of the argument is returned.

*

Note that if the argument is equal to the value of

* {@link Integer#MIN_VALUE}, the most negative representable

* {@code int} value, the result is that same value, which is

* negative.

*

* @param a the argument whose absolute value is to be determined

* @return the absolute value of the argument.

*/

public static int abs(int a)

{

ret


版权声明:本文为weixin_35256923原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。