how to copy an Arrays
Array跟C语言差不多;后续需要更深入的应用实例。
//input
class ArrayDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}
}
//output
caffein
self-adding 自加自减
主要是先后问题,把它翻译成一个句子就不会错了。
//input
class ConcatDemo {
public static void main(String[] args) {
int i = 3;
i++;
System.out.println(i); // i = 4
++i;
System.out.println(i); //i=5
System.out.println(++i); //first i = i+1 = 6, then print i =6
System.out.println(i++); //first print i = 6, then i = i +1 =7
System.out.println(i); //i = 7
}
}
//output
4
5
6
6
7
Instances of and relations with extends and implements
主要是看extends , implements ,和其关联的class 的关系。也是会写会懂..
//input
class InstanceofDemo {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println("obj1 instanceof Parent: "
+ (obj1 instanceof Parent));
System.out.println("obj1 instanceof Child: "
+ (obj1 instanceof Child));
System.out.println("obj1 instanceof MyInterface: "
+ (obj1 instanceof MyInterface));
System.out.println("obj2 instanceof Parent: "
+ (obj2 instanceof Parent));
System.out.println("obj2 instanceof Child: "
+ (obj2 instanceof Child));
System.out.println("obj2 instanceof MyInterface: "
+ (obj2 instanceof MyInterface));
}
}
class Parent {}
class Child extends Parent implements MyInterface {}
interface MyInterface {}
//output
obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true
2018.6.20
源代码依旧来自java8官方文档,这里做个调试和存档
网友评论