java百问总结(一)

1.what is  reflection and introspeaction ?  //关于反射

    ①.Introspection is a subset of reflection,introspection is the ability of a program to examine the type or properties of an object at runtime.

    ②.Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.

    ③.why do we need reflection?

        |--examine an object's class at runtime

            construct an object for a class at runtime

            examine a class's field and method at runtime

            invoke any method of an object at runtime

            change accessibility flag of Constructor,Method and Field

2. how to convert Array to ArrayList in java?

    ①. ArrayList<Object> arrayList  = new ArrayList<Object>( Arrays.asList( array ));  // the best method

    ②. List<Object> list = Arrays.asList( array );  

            It is not best, because the size of the list returned form asList() is fixed. We know ArrayList is essentially implemented as an array, and the list returned from asList() is a fixed-size list backed by the original array. In this way,  if add or remove elements from the returned list,  an UnsupportedOperationException will be thrown.

Object[] array = new Object[]{"ambition",12,true};
ArrayList<Object> arrayList = new ArrayList<Object>(Arrays.asList(array));
System.out.println(arrayList.size());  //3
arrayList.add("Kevin");  
System.out.println(arrayList.size()); //4
System.out.println(arrayList);
		
List<Object> list = Arrays.asList(array);
System.out.println(list.size()); //3
list.add("Jhon"); //UnsupportedOperationException

3.The Introduction of Memory Leaks  //内存泄露

http://www.cnblogs.com/qq78292959/archive/2011/07/25/2116123.html  // Java内存泄露的理解与解决

    |--Why Memory leaks happend?

        Let's take a look at the following example and see why memory leaks happen. In the example below, object A refers to object B. A's lifetime(t1-t4) is much longer than B's(t2-t3). When B is no longer being used in the application, A still holds a reference to it. In this way, Garbage Collector can not remove B from memory. This would possibly cause out of memory problem, because if A dose the same thing for more objects, then there would be a lot of objects that are uncollected and consume memory space.

        It is also possible that B hold a bunch of references of other objects. Those objects referenced by B will not get collected either. All those unused objects will consume precious memory space.

 |--How to prevent Memory Leaks?

        ①. Pay attention to Collection classes, such as HashMap, ArrayList, etc... as they are common palces to find memory leaks. When they are declared static, their life time is the same as the life time of the application.

Vector v = new Vector(10);
for(int i=1; i<100; i++){
    Object o = new Object();
    v.add(o);
    o = null; // o引用置空,但是Object对象仍然被v引用者,是可以被访问的,所以GC无法释放掉
}

        another case is about Object's hashCode,while object's value is seted, we best not change values.

Set<Student> set = new HashSet<Student>();
Student s1 = new Student("Jhon",20);
Student s2 = new Student("Kevin",24);
Student s3 = new Student("ambition",24);
		
set.add(s1);
set.add(s2);
set.add(s3);
System.out.println(set.size()); //3
		
//System.out.println(set.remove(s1)); //true
s1.setAge(24);
System.out.println(set.remove(s1));//false,因为重新设置了s1的属性age,导致是s1的hashCode()改变,导致s1没有移除
System.out.println(set);
System.out.println(set.size());//3

        ②. Pay attention to event listeners and callbacks. A memory leak may occur if a listener is registered but not unregistered when the class is not being used any longer.

        ③. “If a class manages its own memory, the programer should be alter for memory leaks.”  Often times member variables of an object that point  to other objects need to be null out.

4.Iterator vs Recursion in java  //迭代和递归

    ①.linear recursion and tree recursion

        Recursion can be further categorized into linear and tree recursion. When the amount of information needed to keep track of the chain of operations grows linearly with the input, the recursion is called linear recursion. The computation of n! is such a case, because the time required grows linearly with n. Another type of recursion, tree recursion, happens when the amount of information grows exponentially with the input.

//the factorial function: n!=n*(n-1)*(n-2)*...*1
//recursion
int factorial(int n){
    if(n==1){
        return 1;
    }else{
        return n*factorail(n-1);
    }
}
//会记录factuorail(n),factuorail(n-1)...1一系列的值
//iteration
int factorail( int n ){
    int result = 1;
    for(int i=2; i<n; i++){
        result *=i;
    }
    return result;
}
//只需要让result等于当前值
    In the iterative case, the program variables provide a complete description of the state. If we stopped the computation in the middle, to resume it only need to supply the computer with all variables. However, in the recursive process, information is maintained by the computer, therefore "hidden" to the program. This makes it almost impossible to resume the program after stopping it.    

    ②.Tree Recursion happens when the amount of information grows exponentially with the input. For instance, consider the sequence of Fibonacci numbers defined as follows:

recursion-iteration-java

//recursion more straightforward,but less efficient
int fib (int n) {
    if (n == 0) {
        return 0;
    } else if (n == 1) {
        return 1;
    } else {
        return fib(n-1) + fib(n-2);
    }
}
//another
int fib (int n) {
    int fib = 0;
    int a = 1;
    for(int i=0; i<n; i++) {
       fib = fib + a;
       a = fib;
    }
    return fib;
}
5.Java passes by values or reference?

    |--java is pass-by-value

        Pass by value: make a copy in memory of the actual parameter's value that is passed in.

        Pass by reference: pass a copy of the address of the actual parameter.

class Apple {
	public String color="red";
}
 
public class Main {
	public static void main(String[] args) {
		Apple apple = new Apple();
		System.out.println(apple.color); //red
 
		changeApple(apple);
		System.out.println(apple.color);  //green
	}
 
	public static void changeApple(Apple apple){
		apple.color = "green";
	}
}
    since the orignal and copied reference refer the same object, the member value gets changed.

资料网址:
http://www.programcreek.com/2013/04/java百问/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值