java中Number & Math 类方法

package NumberAndMath;
/**
* Number and Math 方法
* @author Administrator
*
*/
public class NumAndMathDome {

public static void main(String[] args) {

/**
* xxxValue
* 将 Number 对象转换为xxx数据类型的值并返回
*/

Integer x =10;
System.out.println("x is byte:"+x.byteValue());
System.out.println("x is double:"+x.doubleValue());
System.out.println("x is shor:"+x.shortValue());
System.out.println("x is long:"+x.longValue());
System.out.println("x is float:"+x.floatValue());

/**
* compareTo
* number类与参数做比较,若比较相等返回0,大于返回1,小于返回-1
*/

Integer x =15;
System.out.println(x.compareTo(17));
System.out.println(x.compareTo(15));
System.out.println(x.compareTo(10));

/**
* equals()
* 判断number对象是否与参数相等,相等则返回ture,否则false
*/
Integer x =44;
Integer y =20;
Integer z =44;
System.out.println(x.equals(y));
System.out.println(x.equals(z));

/**
* round() 方法返回一个最接近的int、long型值
*/
double a =101.568;
double b = 200.4;
float c = 83f;
System.out.println(Math.round(a));//返回值为102
System.out.println(Math.round(b));//返回值为200
System.out.println(Math.round(c));//返回值为83

/**
* math.max() math.min() 返回最大值和最小值
*/
System.out.println(Math.max(23, 44));
System.out.println(Math.min(44, 3));
}

/**
* random()随机数,不接受任何参数
*/
System.out.println(Math.random());
}


}
}

posted @ 2018-06-20 17:21  大海彼岸  阅读(331)  评论(0编辑  收藏  举报