java解惑笔记

 

一、表达式谜题

 

 

1、x=x+i并不一定等于x+=i;

 

 

  short x=0;	int i=123456;
	x=x+i; //编译出错
	x+=i; //编译通过

 

 因为i超过了short最大长度

结论:char ,short 最好不用+=这种运算符 ,还是不能偷懒啊。。对于转型的不要用+=避免出错

 

2、system.out.println(2.00-1.10)

答案0.8999999999999

结论:关于小数运算还是使用BigDecimal进行加减乘除吧。。

 

3、长整除问题

 

 

		final long MICROS_PER_DAY=24*60*60*1000*1000;
		final long MILLS_PER_DAY=24*60*60*1000;
		long a=MICROS_PER_DAY/MILLS_PER_DAY;
		System.out.println(a);

 

 这个结果居然是5..

结论:遇到大数字的,防止转Int溢出

改为

 

final long MICROS_PER_DAY=24L*60*60*1000*1000;
final long MILLS_PER_DAY=24L*60*60*1000;

 不确定是不是会溢出的问题,还是以后加个L吧。。

 

4、条件操作符

 

char x='a';
		int i=0;
		System.out.println(true ?x:0);
		System.out.println(false?i:x);
 

输出结果是: 

a

97

输出了Assll码

总结:条件后面不会一个类型的,还是别用条件操作符啦。。

 

5、byte序列转换成String的时候

如果不指定默认的字符集,将采取默认的系统字符集。

 

 

		byte bytes[]=new byte[256];
		for(int i=0;i<256;i++){
			bytes[i]=(byte)i;
		}
		String str=new String (bytes,"iso-8859-1");
		for(int i=0,n=str.length();i<n;i++){
			System.out.println((int)str.charAt(i));
		}
	}

 总结:要是碰到byte转换成字符串的时候还是指定字符集吧。。

 

6、Random

random.nextInt(2)  取到的是0,1  不是 出现2.

 

7、j=j++;

 

int tmp=j;
j=j+1;
j=tmp;

 

 这就崩溃了,答案是j=0;

 

总结:不要在单个表达式中对一个变量赋值超过一次。

 

8、谜题26,在循环中

 

public class InTheloop {
	public static final int END=Integer.MAX_VALUE;
	public static final int START=END-100;
	public static void main(String[] args) {
		int count=0;
		for(int i=START;i<=END;i++){
			count++;
		}
		System.out.println(count);
	}
}

 

 陷入无限循环中,不会打印100,或者101。。

因为对于所有的int类型都会<=MAX_VALUE恒成立。

改为for(long i=START....)

总结:涉及到边界值还是需要注意的。

 

9、谜题28

 

public class Looper {
	
		public static void main(String[] args) {
//			double i=1.0/0;
			double i=Double.POSITIVE_INFINITY;
			System.out.println(i);
			while(i==i+1)
			{
//				System.out.println(1);
			}
			
		}
}

 可以无限循环。。

 

 

10、 // TODO Auto-generated method stub final int START=2000000000;

		int count=0;
		for(float f=START;f<START+50;f++)//不要在循环中使用float
			count++;
		System.out.println(count);
	}

 11、finally 的注意

最好不要存在二个return 因为最终都会只返回false

 

try{
return true;
}finally{
return false
}
 

 

 

12、在一个类中,声明中创建该实例会造成死循环

 

//stackOverError
public class Reluctant {

	/**
	 * @param args
	 */
	private Reluctant internalInstance=new Reluctant();
	public Reluctant()  throws Exception{
//		throw new Exception("i'm not coming out ");
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			Reluctant b=new Reluctant();
			System.out.println("surprise");
		}catch(Exception ex){
			System.out.println("i told you so");
		}
	}

}
 

 

 

13、finally中抛出的异常

finally{

in.close();

out.close();

}

如果in.close()的时候抛出了异常,那么out.close()就不会继续运行了。。。。

所以可以采用

 

 

finally{
try{
in.close();
}catch{
//do nothing
}

try{
out.close();
}catch{
//do nothing
}
}
 

 

在finally中捕获异常。。。

 

使用closeable接口进行重构,使代码更加整洁。

 

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


/**
 * @author ZhangFuYU
 * @version 创建时间: 2012-10-15 上午11:07:24
 * 类说明:
 */

public class FinallyTest {
	static void copy(String src,String dest) throws IOException{
		InputStream in=null;
		OutputStream out=null;
		try{
			in=new FileInputStream(src);
			out=new FileOutputStream(dest);
			byte[] buf=new byte[1024];
			int n;
			while((n=in.read(buf))>0){
				out.write(buf);
			}
		}finally{
			closeIgnoringException(in);
			closeIgnoringException(out);
		}
	
	}
	
	private static void closeIgnoringException (Closeable c){
		if(c!=null){
			try{
				c.close();
			}catch(IOException ex){
				//we have do nothing
			}
		}
		
	}
}

 

 总结:对于任何一个在finally里中肯尼个抛出的异常的,都需要try,catch

 

 

14、

不要 使用判断异常条件来终止条件

 

&和&&的却别

&&的却别是第一个条件false的是时候就不会判断第二个条件, 而&会同时判断二个条件。

 

15、多个优先构造函数 Q46

如果存在多个构造函数匹配成功,那么优先选择精确度高的进行匹配。

 

16、 s instance String  Q50

这个貌似很有用 

就算s=null 的话也是返回false,而不是抛出NullPointerException等异常

 

17、类计数器如何编写 Q55

多线程增加,和读数据都加同步关键字。

 

package com.java.tongbu;

public class Creator {
	public static     int counter=0;
	public Creator(){
		synchronized (Creator.class) {
			counter=counter+1;
		}
			
	}
	public static	synchronized  int 	 getCounter(){
		return counter;
	}
}

  /**

 * @author ZhangFuYU
 * @version 创建时间: 2012-10-17 下午03:07:20
 * 类说明:5.0之后新版本同步问题
 */
package com.java.tongbu;

import java.util.concurrent.atomic.AtomicLong;

public class NewSyn {

	private static AtomicLong numCreated=new AtomicLong();
	public NewSyn(){
		numCreated.incrementAndGet();
	}
	public static long numCreated(){
		return numCreated.get();
	}
}
 

 

18、如果覆盖了equals方法一定要同样覆盖hasCode()方法

 

19、i3=i2-i1

通过i3 的正负来判断大小是不好的 比如x=-2000000000

z=2000000000  (x-z)打印的确实294967296 

最好这样写i2>i1

 

20、用偶数判断不用基数判断

i%2==0 

 

21、

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值