Java笔试、面试中常见的题目记录
2015-10-28 21:31
666 查看
1、floor() ceil() round()函数比较
三个函数都是java.lang.Math类的静态函数static double | floor(double a) Returns the largest (closest to positive infinity) doublevalue 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) doublevalue that is greater than or equal to the argument and is equal to a mathematical integer. |
static long | round(double a) Returns the closest longto the argument, with ties rounding to positive infinity. |
static int | round(float a) Returns the closest intto the argument, with ties rounding to positive infinity. |
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
2、Integer int之间的比较
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.
3、-0.0与0.0和0的比较
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。不知道谁想出的无聊的题目。。。。
4、indexOf方法
indexOf()方法是java.lang.String类中的方法,查找字符串的子字符串是否存在,如果存在,返回第一个字符出现的位置;否,返回-1String s="abcdef"; System.out.println(s.indexOf("cd")); System.out.println(s.indexOf("cr"));结果:2,-1
5、String字符串地址的比较
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 }
6、Calendar Runtime哪个是单例模式
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是单例模式!"); } }
输出结果:Calendar不是单例模式
Runtime是单例模式!
7、父类 子类中静态代码块 构造函数 代码块{}的执行顺序
子类+父类 static{} > 父类{} + 父类 constructor > 子类{}+子类constructorclass 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...
8、同步方法与同步代码块
在Java语言中,每一个对象都有一把锁,使用synchronized关键字可以获取对象上的锁,synchronized关键字可以加在方法级别(粗粒度)和代码块级别(细粒度)同步代码块:synchronized(this){代码}
同步方法:public synchronized void fun(){代码}
9、Iterator和ListIterator的区别
a、Iterator可以遍历Set和List集合,而ListIterator只能遍历List集合b、Iterator只能前向遍历,ListIterator可以双向遍历
c、ListIterator继承了Iterator接口,并包含了其他功能,比如增加元素,替换元素
Iterator的使用:
@Test public void testIterator(){ //泛型中不可以使用基本数据类型,只能使用封装类型 List<Integer> list = new ArrayList<>(); list.add(2); list.add(5); list.add(7); list.add(6); Iterator<Integer> it = list.iterator(); //Iterator 的 hasNext()方法判断是否还有下一个数据 while(it.hasNext()){ //Iterator的next()可以返回Iterator对应的数据 Integer i = it.next(); System.out.println(i); } }ListIterator的使用与Iterator相同,只不过多了一些可用的方法。
10.Array和ArrayList的区别
相关文章推荐
- 面试心得-----转载
- 《程序员面试金典(第五版)》7.7:找出只含3/5/7因子的第K个数
- [百度面试题] S型层次遍历树
- 七个对我最好的职业建议(精简版)
- java面试之几种开发模式
- P78、面试题10:二进制中1的个数
- P73、面试题9:斐波那契数列
- 海量数据面试题
- 史上最全前端面试题(含答案)
- 华为面试题 题目:有两个数组a,b,大小都为n,数组元素的值任意,无序; 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小
- 黑马程序员——java语言基础组成(二)
- 运维技术面试积累
- SSH 面试题集锦
- 程序员的10条建议
- 黑马程序员——OC基础---Foundation之NSString
- 转:面试题:“你能不能谈谈,java GC是在什么时候,对什么东西,做了什么事情?”
- 程序员的发展方向 人生战略 个人规划 小黄人软件 每天啃书18小时未来十年supermindsoft.com
- selenium面试题
- selenium面试题总结
- 网易嵌入式面试 2015年9月2日下午网易视频二面