Java笔记照片

编程入门 行业动态 更新时间:2024-10-19 01:17:29

Java<a href=https://www.elefans.com/category/jswz/34/1770047.html style=笔记照片"/>

Java笔记照片

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

Iterator 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 classMyClass{

...

}

3. transient关键字

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

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

importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.ObjectInput;importjava.io.ObjectInputStream;importjava.io.ObjectOutput;importjava.io.ObjectOutputStream;importjava.io.Serializable;public class TransientDemo implementsSerializable{

private static final long serialVersionUID = 1L;private transientString name;privateString password;publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicString getPassword() {returnpassword;

}public voidsetPassword(String password) {this.password =password;

}public static void main(String[] args) throwsFileNotFoundException, IOException, ClassNotFoundException {//TODO Auto-generated method stub

String path="D:"+File.separator+"object.txt";

File file=newFile(path);

TransientDemo transientDemo=newTransientDemo();

transientDemo.setName("姓名");

transientDemo.setPassword("密码");

ObjectOutput output=new ObjectOutputStream(newFileOutputStream(file));

output.writeObject(transientDemo);

ObjectInput input=new ObjectInputStream(newFileInputStream(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. 集合框架

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

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

public classExceptionTypeTest {public void doSomething()throwsArithmeticException{

System.out.println();

}public static voidmain(){

ExceptionTypeTest ett= newExceptionTypeTest();

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.

更多推荐

Java笔记照片

本文发布于:2024-03-10 08:12:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1727439.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:笔记   照片   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!