zoukankan      html  css  js  c++  java
  • Java笔记(一)

    1. ConcurrentModificationException 在遍历容器的同时修改容器里的成员对象可能会抛出该异常

        http://www.blogjava.net/EvanLiu/archive/2008/08/31/224453.html

      故在迭代的同时删除容器里的元素要用迭代器的iter.remove()方法,而不要使用容器自带的remove(obj)方法。例如:

    Iterator<Element> e = list.iterator();    
         while(e.hasNext()){    
        Element element = e.next();  
        if(condition){  
            e.remove();  
        }  
    }  
    foreach(element e: list){  
        if(condition){  
            list.remove(e);  //throw ConcurrentModificationException
        }  
    }; 

    2. strictfp关键字

      strictfp 的意思是FP-strict,也就是说精确浮点的意思。在Java虚拟机进行浮点运算时,如果没有指定strictfp关键字时,Java的编译器以及运行环境在对浮点运算的表达式是采取一种近似于我行我素的行为来完成这些操作,以致于得到的结果往往无法令你满意。

      而一旦使用了strictfp来声明一个类、接口或者方法时,那么所声明的范围内Java的编译器以及运行环境会完全依照浮点规范IEEE-754来执行。因此如果你想让你的浮点运算更加精确, 而且不会因为不同的硬件平台所执行的结果不一致的话,那就请用关键字strictfp。

      例如:

    public strictfp class MyClass{
        ...
    }

    3. transient关键字

      当串行化某个对象时,如果该对象的某个变量是transient,那么这个变量不会被串行化进去(或者持久化)。

      也就是说,假设某个类的成员变量是transient,那么当通过ObjectOutputStream把这个类的某个实例保存到磁盘上时,实际上transient变量的值是不会保存的。

    import java.io.File;  
    import java.io.FileInputStream;  
    import java.io.FileNotFoundException;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.io.ObjectInput;  
    import java.io.ObjectInputStream;  
    import java.io.ObjectOutput;  
    import java.io.ObjectOutputStream;  
    import java.io.Serializable;  
      
    public class TransientDemo implements Serializable{  
         
        private static final long serialVersionUID = 1L;  
        private transient String name;  
        private String password;  
          
        public String getName() {  
            return name;  
        }  
      
        public void setName(String name) {  
            this.name = name;  
        }  
      
        public String getPassword() {  
            return password;  
        }  
      
        public void setPassword(String password) {  
            this.password = password;  
        }  
      
        public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {  
            // TODO Auto-generated method stub  
            String path="D:"+File.separator+"object.txt";  
            File file=new File(path);  
            TransientDemo transientDemo=new TransientDemo();  
            transientDemo.setName("姓名");  
            transientDemo.setPassword("密码");  
            ObjectOutput output=new ObjectOutputStream(new FileOutputStream(file));  
            output.writeObject(transientDemo);  
            ObjectInput input=new ObjectInputStream(new FileInputStream(file));  
            TransientDemo demo=(TransientDemo)input.readObject();  
            System.out.println(demo.getName()+demo.getPassword());  
        }  
      
    }  

      代码输出:

    null密码

    4. native关键字(todo)

    5. class文件内部结构

    ClassFile { 
    u4 magic; 
    u2 minor_version; 
    u2 major_version; 
    u2 constant_pool_count; 
    cp_info constant_pool[constant_pool_count-1]; 
    u2 access_flags; 
    u2 this_class; 
    u2 super_class; 
    u2 interfaces_count; 
    u2 interfaces[interfaces_count]; 
    u2 fields_count; 
    field_info fields[fields_count]; 
    u2 methods_count; 
    method_info methods[methods_count]; 
    u2 attributes_count; 
    attribute_info attributes[attributes_count]; 
    }

      简要解释一下:

      U4 代表由无符号四个字节组成

      u4 magic :是一个固定的数值,java虚拟机里面称为魔数 ,主要是用来标识是否为java虚拟机所支持的文件结构,目前是0xCAFEBABE

      u2 minor_version; u2 major_version; 代表次版本号和主版本号

      u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; 这里面代表常量池个数以及常量池信息

      u2 access_flags : 代表class访问标记,例如:public protected

      u2 this_class : 代表这个类的名称 例如 java.lang.Object

      u2 super_class : 代表父类名称

      u2 interfaces_count; u2 interfaces[interfaces_count]; 实现的接口格式以及接口类名

      u2 fields_count; field_info fields[fields_count]; 字段个数以及字段信息

      u2 methods_count; method_info methods[methods_count]; 方法个数以及方法信息

      u2 attributes_count; attribute_info attributes[attributes_count]; java class文件内部属性信息

      

    6. java内存结构

      

    7. 集合框架

      http://blog.csdn.net/conquer0715/article/details/12367087

      Java的集合分为了四类:List Set Queue Map,每类都有不同的实现,有基于数组实现的,有基于链表实现的,有基于xx树实现的,不同的实现虽在功能上可以相互替代但都有各自的应用场景,如基于数组的实现擅长快速遍历,基于链表的实现擅长随机写,基于树的实现可排序等等.

      JDK1.5及以后还添加了很多实用的功能,如ConcurrentMap、BlockingQueue、BlockingDeque、CopyOnWriteArrayList、CopyOnWriteArraySet等等,另外,Collections工具类也提供了很多方便的API,如synchronizedCollection、binarySearch、checkedCollection、copy、indexOfSubList、reverse、singletonList等,这给我们提供了非常多的选择,在实际开发中一定要根据实际的情况仔细分析选择合适的数据结构可以大大方便开发过程以及大幅度提高系统性能。

      在选择集合类时候可以参考:

      多线程下使用,需要保证线程安全,读操作远大与写操作,例如缓存系统,使用CopyOnWriteArray...也许会是不错的选择,此外Concurrent...也是非常好的选择,如果在多线程间还需要协作通信等,那么阻塞队列BlockingQueue、BlockingDeque(双端队列)会是最合适的选择,如果这还不够,可以使用带优先级的PriorityBlockingQueue,更妙的地方我们还可以使用DelayQueue(延迟队列),如果你的创造力够强的话,用DelayQueue来实现一些超时管理(如Session超时处理、请求超时处理)会有非常优雅和奇妙的效果。

      如果不会涉及多线程并发访问,如方法内部、同步访问区等,如果有随机写的需求可以考虑LinkedList或LinkedHashSet,如果需要快速检索元素是否已经存在于集合内可以考虑使用HashMap、HashSet,如果我们需要非常频繁且高效的遍历则应该采用ArrayList,如果我们需要排序,那么就必需要选择TreeMap、TreeSet喽,另外,LinkedList同时实现了List、Deque、Queue接口,用其来实现Stack也是可以的。

    8. InterruptedException

      http://www.ibm.com/developerworks/cn/java/j-jtp05236.html

      http://blog.csdn.net/srzhz/article/details/6804756

     9. 检查异常和未检查异常

    public class ExceptionTypeTest {    
        public void doSomething()throws ArithmeticException{    
            System.out.println();    
        }    
        public static void main(){    
            ExceptionTypeTest ett = new ExceptionTypeTest();    
            ett.doSomething();    
        }    
    }  

    问题1:上面的程序能否编译通过?并说明理由。
    解答:能编译通过。分析:按照一般常理,定义doSomething方法是定义了ArithmeticException异常,在main方法里 里面调用了该方法。那么应当继续抛出或者捕获一下。但是ArithmeticException异常是继承RuntimeException运行时异常。 java里面异常分为两大类:checkedexception(检查异常)和unchecked exception(未检
    查异常),对于未检查异常也叫RuntimeException(运行时异常),对于运行时异常,java编译器不要求你一定要把它捕获或者一定要继续抛出,但是对checkedexception(检查异常)要求你必须要在方法里面或者捕获或者继续抛出.

    问题2:上面的程序将ArithmeticException改为IOException能否编译通过?并说明理由。
    解答:不能编译通过。分析:IOException extends Exception 是属于checked exception,必须进行处理,或者必须捕获或者必须抛出.

    总结:java中异常分为两类:checked exception(检查异常)和unchecked exception(未检查异常),对于未检查异常也叫RuntimeException(运行时异常).
    对未检查的异常(unchecked exception )的几种处理方式:
    1、捕获
    2、继续抛出
    3、不处理
    对检查的异常(checked exception,除了RuntimeException,其他的异常都是checked exception )的几种处理方式:
    1、继续抛出,消极的方法,一直可以抛到java虚拟机来处理
    2、用try...catch捕获
    注意,对于检查的异常必须处理,或者必须捕获或者必须抛出

    10. 

  • 相关阅读:
    python验证码识别——前处理
    因为应用程序正在发送一个输入同步呼叫,所以无法执行传出的呼叫 解决方法
    Basic Authentication Authentication with Python(翻译草稿)
    闭包初探
    为朋友写的一个投票功能的提交代码
    Ulipad中Script的编写
    SharePoint 2010 Error occurred in deployment step 'Activate Features': Feature Id 解决方法
    SharePoint 2010 传入电子邮件
    SharePoint2010 InfoPath2010 该表单无法显示 Server State Service配置不当 解决方法
    SharePoint 2010 客户端不支持使用windows资源管理器打开此列表 解决方法
  • 原文地址:https://www.cnblogs.com/xingyun/p/4358289.html
Copyright © 2011-2022 走看看