1、运算符相关
inti = 5;
intj = 10;
System.out.println(i + ~j);
2、泛型相关
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"); //此时,编译时就不能通过,报错!!!
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());
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());
}
}
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();
}
}
4、suspend()和resume()方法
5、几个需要注意的小知识点
Boolean flag = false;
if(flag = true){
System.out.println(“true”);
}else{
System.out.println(“false”);
}
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 static void main (String[] args) {
String classFile = "com. jd. ". replaceA11(".", "/") + "MyClass.class";
System.out.println(classFile);
}
int b = 127;
System.out.println(b);
b = b++;
System.out.println(b);
A、127 B、128
6、自动拆箱、装箱问题
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、程序无法执行
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后执行
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;
}
}
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 };
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
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();
}
Java 8允许我们给接口添加一个非抽象的方法实现,只需要使用 default关键字即可,这个特征又叫做扩展方法,示例如下:
interface Formula {
double calculate(int a);
default double sqrt(int a) {
return Math.sqrt(a);
}
}
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);
}
});
在Java 8 中你就没必要使用这种传统的匿名对象的方式了,Java 8提供了更简洁的语法,lambda表达式:
return b.compareTo(a);
});
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
9、异常抛出问题
A、throw关键字可以在方法上声明该方法要抛出的异常。 B、throws用于抛出异常对象。
C、try是用于检测被包住的语句块是否出现异常,如果有异常,则抛出异常,并执行catch语句。
D、finally语句块是不管有没有出现异常都要执行的内容。 E、在try块中不可以抛出异常