static double |
floor(double a)
Returns the largest (closest to positive infinity)
double value that is less than or equal to the argument and is equal to a mathematical integer.
|
static double |
ceil(double a)
Returns the smallest (closest to negative infinity)
double value that is greater than or equal to the argument and is equal to a mathematical integer.
|
static long |
round(double a)
Returns the closest
long to the argument, with ties rounding to positive infinity.
|
static int |
round(float a)
Returns the closest
int to the argument, with ties rounding to positive infinity.
|
上边是摘自javadoc里的,
floor为地板之意,一个数的“地板”也就是不大于这个数的最大整数,依据doc,函数返回的是double类型,所以要整数后边加个小数点和0
System.out.println(Math.floor(12.5)); System.out.println(Math.floor(-12.5)); System.out.println(Math.floor(12));
12.0
-13.0
12.0
ceil有天花板之意,一个数的“天花板”就是不小于这个数的最小整数,返回是double类型
System.out.println(Math.ceil(12.5)); System.out.println(Math.ceil(-12.5)); System.out.println(Math.ceil(12));
13.0
-12.0
12.0
而round就是取数的四舍五入值,返回为整形值
System.out.println(Math.round(12.5)); System.out.println(Math.round(12.4)); System.out.println(Math.round(-12.5)); System.out.println(Math.round(-12.4));
13
12
-12
-12
注意负数的四舍五入,
System.out.println(Math.round(-12.5));结果为-12,
System.out.println(Math.round(-12.6));
结果为-13,这是从数轴来看,-12.6是从-13向右0.4,故为-13.而-12.5是从-13向右0.5,故为-12
Integer in1=new Integer(10); Integer in2=new Integer(10); Integer in3=10; Integer in4=10; int in5=10;Integer与int之间比较:
System.out.println(in1==in5); System.out.println(in1.equals(in5));结果:true true
Integer之间的比较:
System.out.println(in1==in2); System.out.println(in1==in3); System.out.println(in3==in4);结果为:false,false,true。因为==之间比较的是地址,in1,in2地址不同,in3,in4地址相同,指向整形常量10.
public static void main(String[] args){ if(-0==0){ System.out.println("Yes"); } if(-0.0==0.0){ System.out.println("Yes"); } if(-0.0==0){ System.out.println("Yes"); } }输出 Yes Yes Yes。不知道谁想出的无聊的题目。。。。
indexOf()方法是java.lang.String类中的方法,查找字符串的子字符串是否存在,如果存在,返回第一个字符出现的位置;否,返回-1
String s="abcdef"; System.out.println(s.indexOf("cd")); System.out.println(s.indexOf("cr"));结果:2,-1
public static void main(String[] args){ String s1="abc"; String s2="abc"; System.out.println(s1==s2);//都指向常量字符串"abc",输出为true String s3=new String("abc"); String s4=new String("abc"); System.out.println(s3==s4);//new创建了两个字符串,在内存中有两个内存地址,false System.out.println(s1==s3);//一个常量字符串地址,一个new开辟的内存地址,false String s5=s1+s2; String s6=s1+s2; System.out.println(s5==s6);//false,原因待探究 String s7="abc"+"def"; String s8="abc"+"def"; String s9="abcd"+"ef"; System.out.println(s7==s8);//与常量字符串"abcdef"有关,返回为true System.out.println(s7==s9);//与常量字符串"abcdef"有关,返回为true String s10="abc"+new String("def"); System.out.println(s7==s10);//false,有new创建,基本就是false }
Runtime是单例模式
public static void main(String[] args){ Calendar c1=Calendar.getInstance(); Calendar c2=Calendar.getInstance(); if(c1==c2){ System.out.println("Clendar是单例模式!"); } else { System.out.println("Calendar不是单例模式"); } Runtime r1= Runtime.getRuntime(); Runtime r2= Runtime.getRuntime(); if(r1==r2){ System.out.print("Runtime是单例模式!"); } }
子类+父类 static{} > 父类{} + 父类 constructor > 子类{}+子类constructor
class Father{ public Father(){ System.out.println("Father Constructor..."); } { System.out.println("Father {} "); } static{ System.out.println("Father Static..."); } } class Son extends Father{ public Son(){ System.out.println("Son Constructor..."); } { System.out.println("Son {} "); } static{ System.out.println("Son Static..."); } } public class Test { public static void main(String[] args){ new Son(); } }输出:
Father Static...
Son Static...
Father {}
Father Constructor...
Son {}
Son Constructor...