`
joyocaowei
  • 浏览: 31325 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

查漏补缺2012

阅读更多

2012.2-2012.12

1、Notepad++中的“Show All Characters”可以查看windows和unix下的不同的换行符,windows下的换行符为CRLF,unix下的换行符为LF


2、单元素的枚举类型目前是实现Singleton的最佳方法。枚举表示简洁,并提供了序列化机制,即使在面对复杂的序列化或者反射攻击的时候,能够绝对防止多次序列化。

public enum Elvis {
	INSTANCE;

	public void leaveTheBuilding() {
		System.out.println("Whoa baby, I'm outta here!");
	}

	// This code would normally appear outside the class!
	public static void main(String[] args) {
		Elvis elvis = Elvis.INSTANCE;
		elvis.leaveTheBuilding();
	}
}














 

3、IOC(控制反转)/DI(依赖注入)

它的主要目的是实现松散耦合,以便提升整个解决方案的可维护性。 松散耦合可被概述为基于接口而不是具体实现进行编程的思想。 但是,因为接口没有构造函数,如何创建那些接口的实例马上就成了一个问题。

参考Martin Fowler 的两篇文章


first: http://martinfowler.com/bliki/InversionOfControl.html


second: http://martinfowler.com/articles/injection.html


4、Servlet技术简介: http://www.ibm.com/developerworks/cn/education/java/j-intserv/index.html

里面有一个简单的demo,可以帮助学习或者复习基本的Servlet,里面的例子稍稍修改了一点,主要是I/O操作方面的,还有这个例子的代码感觉有个别地方有点小错误,修改了一下。附件contacts.zip是代码。

 

另一篇文章:http://www.oschina.net/question/12_52027

再一篇:http://www.ibm.com/developerworks/cn/java/j-lo-servlet/


5、float和double类型不适合用于货币计算,使用BigDecimal、int、long进行货币计算。

相对于int和long类型,BigDecimal(可以处理超过18位的数字)使用起来相对麻烦,但对于完全控制舍入进行业务计算,使用它非常方便。

另外一点是,使用float和double进行比较的时候,应该使用他们包装类的compare方法,如Float.compare和Double.compare,至于为什么要这样使用,可以查看你Double和Float的API ,其中的equals方法。


6、ANT常用标签:http://ant.apache.org/manual/index.html

之后点击Ant Tasks, 然后点击Overview of Ant Tasks, 可以看到所有的标签,如果你想了解某一个,可以自行查看。


7、关于注解的基础知识: http://www.infoq.com/cn/articles/cf-java-annotation


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RequiredRoles {
    String[] value();
}















import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.security.AccessControlException;
import java.util.Arrays;

public class AccessInvocationHandler<T> implements InvocationHandler {
	final T accessObj;

	public AccessInvocationHandler(T accessObj) {
		this.accessObj = accessObj;
	}

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		RequiredRoles annotation = method.getAnnotation(RequiredRoles.class); // 通过反射API获取注解
		if (annotation != null) {
			String[] roles = annotation.value();
			// String role = AccessControl.getCurrentRole();
			String role = "user";
			if (!Arrays.asList(roles).contains(role)) {
				throw new AccessControlException(
						"The user is not allowed to invoke this method.");
			}
		}
		return method.invoke(accessObj, args);
	}
}














 

     Effective java中的item35(注解优先于命名模式)也有一些较好的解释。附件中的effective java second edition是电子书(英文版的)。


8、 上面的注解代码中出现了动态代理,所以有必要了解动态代理的相关知识,下面是IBM上的一篇文章: Java 动态代理机制分析及扩展

       关于代理的另一篇文章: http://www.iteye.com/topic/448655


9、深入理解java虚拟机(翻译):http://www.cnblogs.com/gengyulong/archive/2011/04/11/2012560.html


10、A filesystem is a higher level of abstraction. Filesystems are a particular method of arranging and interpreting data stored on a disk (or some other random-access, block-oriented device).


文件系统是更高层次的抽象,是安排、解释磁盘(或其他随机存取块设备)数据的一种独特方式。


11、深入学习java NIO,附件中有java NIO电子书。

      a、  http://tutorials.jenkov.com/java-nio/index.html

      b、 查看java编程思想的nio那一章(18.10-第十八章第十小节)

 

 

12、 pl/sql中NUMBER变量默认值是null。

 

DECLARE
    v_num1 NUMBER := 0;
    v_num2 NUMBER;
BEGIN
    IF v_num1 = v_num2 THEN
        DBMS_OUTPUT.PUT_LINE ('v_num1 = v_num2');
    ELSE
        DBMS_OUTPUT.PUT_LINE ('v_num1 != v_num2');
    END IF;
END;

  输出结果是:

                 v_num1 != v_num2
                 PL/SQL procedure successfully completed.


13、RFCRequest for Comments )是各个标准机构提交的标准文档,研究人员和开发人员可以通过这些文档跟踪新技术。

 

14、jsoup使用:http://www.ibm.com/developerworks/cn/java/j-lo-jsouphtml/index.html?ca=drs-

 

15、 shell中[ ]与[[ ]]条件测试有什么区别?
[ ]是shell内建的测试命令,[[ ]]是由外置命令/usr/bin/test进行测试,[[]]结构比Bash 的[]更加灵活,在[[]]结构中,将没有文件扩展或者是单词分离,但是会发生参数扩展和命令替换.:使用[[]],而不是[],能够阻止脚本中的许多逻 辑错误.比如,尽管在[]中将给出一个错误,但是&&,||,<>操作还是能够工作在一个[[]]test 之中。

 

16、15.18.1.2 Optimization of String Concatenation(JLS中关于字符串连接操作符‘+’的性能说明)

An implementation may choose to perform conversion and concatenation in one
step to avoid creating and then discarding an intermediate String object. To
increase the performance of repeated string concatenation, a Java compiler may
use the StringBuffer class or a similar technique to reduce the number of intermediate
String objects that are created by evaluation of an expression.
For primitive types, an implementation may also optimize away the creation
of a wrapper object by converting directly from a primitive type to a string.
 

还可以参考effective java2(items 51)

 

17、try catch finally中不执行finally的方式,在try中调用System.exit(0), The System.exit method halts the execution of the current thread and all others dead in their tracks.

try {
            System.out.println("Hello world");
            System.exit(0);
        } finally {
            System.out.println("Goodbye world");
        }

 输出的结果是: Hello World

 

18、Eclipse调试JDK源码(内容来自网络

突然发现用eclipse调试时不能跟进jdk的类,search一下有人说下jdk的debug版本,但之前我一直用非debug版本也可以进入调试啊,也有人说下完整的src,但是我的是一些核心jdk类就无法调试。

 重新检查eclipse的配置,发现原因,原来install jre配置使用的是jre home是jre安装目录,更改到jdk目录,即可以如愿跟进jdk的核心类了。<路径:preferences-->java-->install jre>

附录:   

    1、JDK自带的src.zip里源代码并不完整,不能查看相关类的源文件(当然,喜欢的话可以反编译)。譬如,以sun.开头的包就不包含在src.zip这个压缩包中。   

    解决办法:下载完整的源文件包,调试时附加给Debugger 即可。    

     2、默认的 rt.jar没有行号信息,虽然有源代码,但是不能设置断点。   

   解决办法:下载Debug版的JDK,安装以后将其设置为Eclipse的默认JVM即可。

19、Windows下硬链接、软链接和快捷方式的区别(来自网络 )

20、网络抓取 可以 httpcomponents + jsoup实现(仅jsoup也可以)

21、网络爬虫的学习可以看Apache Nutch

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics