转载至:http://blog.csdn.net/shakespeare001/article/details/51388516
作者:山代王(开心阳)
1、运算符相关
- inti = 5;
- intj = 10;
- System.out.println(i + ~j);
inti = 5;
intj = 10;
System.out.println(i + ~j);
error because”~”doesn’t operate on integers B、-5 C、-6 D、15
2、泛型相关
- List list = new ArrayList();
- list.add(18);
- list.add(”lly”);
- for(Object obj : list){
- int i = (int) obj;//此处运行后,将会报错
- }
List list = new ArrayList();
list.add(18);
list.add("lly");
for(Object obj : list){
int i = (int) obj;//此处运行后,将会报错
}
- List<Integer> list = new ArrayList<Integer>();
- list.add(18);
- list.add(”lly”); //此时,编译时就不能通过,报错!!!
List<Integer> list = new ArrayList<Integer>();
list.add(18);
list.add("lly"); //此时,编译时就不能通过,报错!!!
- class Person<T> {
- private T charac;//人物特征
- public Person(T ch){
- this.charac = ch;
- }
- public T getCharac() {
- return charac;
- }
- public void setCharac(T charac) {
- this.charac = charac;
- }
- }
class Person<T> {
private T charac;//人物特征
public Person(T ch){
this.charac = ch;
}
public T getCharac() {
return charac;
}
public void setCharac(T charac) {
this.charac = charac;
}
}
- Person<String> p1 = new Person<String>(“lly”);
- Person<Integer> p2 = new Person<Integer>(18);
- System.out.println(”p1—>”+p1.getClass());
- System.out.println(”p2—>”+p2.getClass());
Person<String> p1 = new Person<String>("lly");
Person<Integer> p2 = new Person<Integer>(18);
System.out.println("p1--->"+p1.getClass());
System.out.println("p2--->"+p2.getClass());
- public class CommonTest {
- public static void main(String[] args) {
- Person<Number> p1 = new Person<Number>(12);
- Person<Integer> p2 = new Person<Integer>(18);
- getCharac(p1);
- getCharac(p2);//报错!!!编译不能通过,提示参数类型不符合
- }
- public static void getCharac(Person<Number> person){
- System.out.println(person.getCharac());
- }
- }
public class CommonTest {
public static void main(String[] args) {
Person<Number> p1 = new Person<Number>(12);
Person<Integer> p2 = new Person<Integer>(18); getCharac(p1); getCharac(p2);//报错!!!编译不能通过,提示参数类型不符合
} public static void getCharac(Person<Number> person){
System.out.println(person.getCharac());
}
}
将上面的参数类型改成通配符的形式以后,我们调用getCharac(p2);就不会出错了。
Which three statements are true?
class C extends A {}
class D extends B {}
Which four statements are true ?
A、The type List<A>is assignable to List.
B、The type List<B>is assignable to List<A>.
C、The type List<Object>is assignable to List<?>.
D、The type List<D>is assignable to List<? extends B>.
E、The type List<?extends A>is assignable to List<A>.
F、The type List<Object>is assignable to any List reference.
G、The type List<?extends B>is assignable to List<?extends A>.
这个时候都能通过。
3、变量初始化问题
- public class VarTest {
- final int i ;
- public VarTest(){ //在构造方法中初始化了
- i = 3;
- }
- public VarTest(int n){ //有多个构造方法,必须在每个构造方法中进行初始化
- i = n;
- }
- public void doSomething() {
- int j;
- j = 1;//对于临时变量,如果这里不进行初始化,下面使用++j时编译不能通过
- System.out.println(++j + i);
- }
- public static void main(String[] args) {
- VarTest test = new VarTest();
- test.doSomething();
- }
- }
public class VarTest {
final int i ;
public VarTest(){ //在构造方法中初始化了
i = 3;
}
public VarTest(int n){ //有多个构造方法,必须在每个构造方法中进行初始化
i = n;
}
public void doSomething() {
int j;
j = 1;//对于临时变量,如果这里不进行初始化,下面使用++j时编译不能通过
System.out.println(++j + i);
} public static void main(String[] args) {
VarTest test = new VarTest();
test.doSomething();
}
}
4、suspend()和resume()方法
suspend() 和 resume() 方法:两个方法配套使用,suspend()使得线程进入阻塞状态,并且不会自动恢复,必须其对应的
resume() 被调用,才能使得线程重新进入可执行状态
5、几个需要注意的小知识点
following fragment of code:
- Boolean flag = false;
- if(flag = true){
- System.out.println(“true”);
- }else{
- System.out.println(“false”);
- }
Boolean flag = false;
if(flag = true){
System.out.println(“true”);
}else{
System.out.println(“false”);
}
A、The code fails to compile at the “if” statement. B、An
exception is thrown at run-time at the “if” statement.
text“true” is displayed. D、The text“false”is displayed. E、Nothing
is displayed.
run the following code?
- public class test{
- static{
- intx=5;
- }
- static int x,y;
- public static void main(String args[]){
- x–;
- myMethod( );
- System.out.println(x+y+ ++x);
- }
- public static void myMethod( ){
- y=x++ + ++x;
- }
- }
public class test{
static{
intx=5;
}
static int x,y;
public static void main(String args[]){
x--;
myMethod( );
System.out.println(x+y+ ++x);
}
public static void myMethod( ){
y=x++ + ++x;
}
}
A、compiletime error B、prints:1
C、prints:2 D、prints:3
E、prints:7 F、prints:8
++x,即1+0+(++x).
- public static void main (String[] args) {
- String classFile = ”com. jd. ”. replaceA11(“.”, “/”) + “MyClass.class”;
- System.out.println(classFile);
- }
public static void main (String[] args) {
String classFile = "com. jd. ". replaceA11(".", "/") + "MyClass.class";
System.out.println(classFile);
}
A、com. jd B、com/jd/MyClass.class
C、///////MyClass.class D、com.jd.MyClass
- int b = 127;
- System.out.println(b);
- b = b++;
- System.out.println(b);
int b = 127;
System.out.println(b);
b = b++;
System.out.println(b);
A、127 B、128
= ++b;b被赋予++b的结果,之后b也并不会再来自增,因此打印128。
6、自动拆箱、装箱问题
- inti=0;
- Integer j = newInteger(0);
- System.out.println(i==j);
- System.out.println(j.equals(i));
inti=0;
Integer j = newInteger(0);
System.out.println(i==j);
System.out.println(j.equals(i));
A、true,false B、true,true C、false,true D、false,false E、对于不同的环境结果不同 F、程序无法执行
如果是这两种情况创建出来的对象,那么其实只会创建一个对象,这些对象已经缓存在一个叫做IntegerCache里面了,所以==比较是相等的。如果不在-128至127这个区间,不管是通过什么方式创建出来的对象,==永远是false,也就是说他们的地址永远不会相等。
i = 1;和Integer i = Integer.valueOf(1); 这两种方式创建的对象相同
i = 1;和Integer i = Integer.valueOf(1); 这两种方式创建的对象相同
- byte b1=1,b2=2,b3,b6;
- final byteb4=4,b5=6;
- b6=b4+b5;
- b3=(b1+b2);
- System.out.println(b3+b6);
byte b1=1,b2=2,b3,b6;
final byteb4=4,b5=6;
b6=b4+b5;
b3=(b1+b2);
System.out.println(b3+b6);
A、输出结果:13 B、语句:b6=b4+b5编译出错 C、语句:b3=b1+b2编译出错 D、运行期抛出异常
7、finally语句的执行是在return前还是return后
在try的括号里面有return一个值,那在哪里执行finally里的代码?
A、不执行finally代码 B、return前执行 C、return后执行
正确答案:C
- public static void main(String[] args) {
- intk = f_test();
- System.out.println(k);
- }
- public static int f_test(){
- inta = 0;
- try{
- a = 1;
- returna;
- }
- finally{
- System.out.println(”It is in final chunk.”);
- a = 2;
- returna;
- }
- }
public static void main(String[] args) {
intk = f_test();
System.out.println(k);
} public static int f_test(){
inta = 0;
try{
a = 1;
returna;
}
finally{
System.out.println("It is in final chunk.");
a = 2;
returna;
}
}
说明,finally是在try中执行完return后再执行的。只有当finally中也有return的时候,方法将直接返回,不再执行热河代码。
8、Java1.7和Java1.8的新特性
- // 所有整数 int, short,long,byte都可以用二进制表示
- // An 8-bit ‘byte’ value:
- byte aByte = (byte) 0b00100001;
- // A 16-bit ‘short’ value:
- short aShort = (short) 0b1010000101000101;
- // Some 32-bit ‘int’ values:
- intanInt1 = 0b10100001010001011010000101000101;
- intanInt2 = 0b101;
- intanInt3 = 0B101; // The B can be upper or lower case.
- // A 64-bit ‘long’ value. Note the “L” suffix:
- long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;
- // 二进制在数组等的使用
- final int[] phases = { 0b00110001, 0b01100010, 0b11000100, 0b10001001,
- 0b00010011, 0b00100110, 0b01001100, 0b10011000 };
// 所有整数 int, short,long,byte都可以用二进制表示
// An 8-bit 'byte' value:
byte aByte = (byte) 0b00100001; // A 16-bit 'short' value:
short aShort = (short) 0b1010000101000101; // Some 32-bit 'int' values:
intanInt1 = 0b10100001010001011010000101000101;
intanInt2 = 0b101;
intanInt3 = 0B101; // The B can be upper or lower case. // A 64-bit 'long' value. Note the "L" suffix:
long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L; // 二进制在数组等的使用
final int[] phases = { 0b00110001, 0b01100010, 0b11000100, 0b10001001,
0b00010011, 0b00100110, 0b01001100, 0b10011000 };
- String str = “a”;
- switch (str) {
- case “a”:
- System.out.println(”a—”);
- break;
- case “b”:
- System.out.println(”b—”);
- break;
- }
String str = "a";
switch (str) {
case "a":
System.out.println("a---");
break;
case "b":
System.out.println("b---");
break;
}
注意:在把字符串传进Switch case之前,别忘了检查字符串是否为Null。
- long creditCardNumber = 1234_5678_9012_3456L;
- long socialSecurityNumber = 999_99_9999L;
- float pi = 3.14_15F;
- long hexBytes = 0xFF_EC_DE_5E;
- long hexWords = 0xCAFE_BABE;
- long maxLong = 0x7fff_ffff_ffff_ffffL;
- byte nybbles = 0b0010_0101;
- long bytes = 0b11010010_01101001_10010100_10010010;
- //float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent to a decimal point
- //float pi2 = 3._1415F; // Invalid; cannot put underscores adjacent to a decimal point
- //long socialSecurityNumber1= 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffix
- //int x1 = _52; // This is an identifier, not a numeric literal
- int x2 = 5_2; // OK (decimal literal)
- //int x3 = 52_; // Invalid; cannot put underscores at the end of a literal
- int x4 = 5_______2; // OK (decimal literal)
- //int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix
- //int x6 = 0x_52; // Invalid; cannot put underscores at the beginning of a number
- int x7 = 0x5_2; // OK (hexadecimal literal)
- //int x8 = 0x52_; // Invalid; cannot put underscores at the end of a number
- int x9 = 0_52; // OK (octal literal)
- int x10 = 05_2; // OK (octal literal)
- //int x11 = 052_; // Invalid; cannot put underscores at the end of a number
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
//float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent to a decimal point
//float pi2 = 3._1415F; // Invalid; cannot put underscores adjacent to a decimal point
//long socialSecurityNumber1= 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffix
//int x1 = _52; // This is an identifier, not a numeric literal
int x2 = 5_2; // OK (decimal literal)
//int x3 = 52_; // Invalid; cannot put underscores at the end of a literal
int x4 = 5_______2; // OK (decimal literal)
//int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix
//int x6 = 0x_52; // Invalid; cannot put underscores at the beginning of a number
int x7 = 0x5_2; // OK (hexadecimal literal)
//int x8 = 0x52_; // Invalid; cannot put underscores at the end of a number
int x9 = 0_52; // OK (octal literal)
int x10 = 05_2; // OK (octal literal)
//int x11 = 052_; // Invalid; cannot put underscores at the end of a number
- BufferedReader br = new BufferedReader(new FileReader(path));
- try {
- return br.readLine();
- } finally {
- br.close();
- }
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
- try (BufferedReader br = new BufferedReader(new FileReader(path)) {
- return br.readLine();
- }
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
return br.readLine();
}
Java8允许我们给接口添加一个非抽象的方法实现,只需要使用 default关键字即可,这个特征又叫做扩展方法,示例如下:
- interface Formula {
- double calculate(int a);
- default double sqrt(int a) {
- return Math.sqrt(a);
- }
- }
interface Formula {
double calculate(int a);
default double sqrt(int a) {
return Math.sqrt(a);
}
}
Formula接口在拥有calculate方法之外同时还定义了sqrt方法,实现了Formula接口的子类只需要实现一个calculate方法,默认方法sqrt将在子类上可以直接使用。
- Formula formula = new Formula() {
- @Override
- public double calculate(int a) {
- return sqrt(a * 100);
- }
- };
- formula.calculate(100); // 100.0
- formula.sqrt(16); // 4.0
Formula formula = new Formula() {
@Override
public double calculate(int a) {
return sqrt(a * 100);
}
};
formula.calculate(100); // 100.0
formula.sqrt(16); // 4.0
首先看看在老版本的Java中是如何排列字符串的:
- List<String> names = Arrays.asList(“peter”, “anna”, “mike”, “xenia”);
- Collections.sort(names, new Comparator<String>() {
- @Override
- public int compare(String a, String b) {
- return b.compareTo(a);
- }
- });
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return b.compareTo(a);
}
});
只需要给静态方法 Collections.sort 传入一个List对象以及一个比较器来按指定顺序排列。通常做法都是创建一个匿名的比较器对象然后将其传递给sort方法。
在Java 8 中你就没必要使用这种传统的匿名对象的方式了,Java 8提供了更简洁的语法,lambda表达式:
return b.compareTo(a);
});
看到了吧,代码变得更段且更具有可读性,但是实际上还可以写得更短:
对于函数体只有一行代码的,你可以去掉大括号{}以及return关键字,但是你还可以写得更短点:
Java编译器可以自动推导出参数类型,所以你可以不用再写一次类型。
Java 8 在包java.time下包含了一组全新的时间日期API。新的日期API和开源的Joda-Time库差不多,但又不完全一样,下面的例子展示了这组新API里最重要的一些部分:
Clock 时钟
Clock类提供了访问当前日期和时间的方法,Clock是时区敏感的,可以用来取代 System.currentTimeMillis() 来获取当前的微秒数。某一个特定的时间点也可以使用Instant类来表示,Instant类也可以用来创建老的java.util.Date对象。
- Clock clock = Clock.systemDefaultZone();
- long millis = clock.millis();
- Instant instant = clock.instant();
- Date legacyDate = Date.from(instant); // legacy java.util.Date
Clock clock = Clock.systemDefaultZone();
long millis = clock.millis();
Instant instant = clock.instant();
Date legacyDate = Date.from(instant); // legacy java.util.Date
9、异常抛出问题
A、throw关键字可以在方法上声明该方法要抛出的异常。 B、throws用于抛出异常对象。
C、try是用于检测被包住的语句块是否出现异常,如果有异常,则抛出异常,并执行catch语句。
D、finally语句块是不管有没有出现异常都要执行的内容。 E、在try块中不可以抛出异常