class Person {
private T charac;//人物特征
public Person(T ch){
this.charac = ch;
}
public T getCharac() {
return charac;
}
public void setCharac(T charac) {
this.charac = charac;
}
}
测试:
Person p1 = new Person("lly");
Person p2 = new Person(18);
System.out.println("p1--->"+p1.getClass());
System.out.println("p2--->"+p2.getClass());
public class CommonTest {
public static void main(String[] args) {
Person p1 = new Person(12);
Person p2 = new Person(18);
getCharac(p1);
getCharac(p2);//报错!!!编译不能通过,提示参数类型不符合
}
public static void getCharac(Person person){
System.out.println(person.getCharac());
}
}
public static void getCharac(
Person extends Number> person){
System.out.println(person.getCharac());
}
测试:
Person p1 = new Person(12);
Person p2 = new Person(18);
Person p3 = new Person("lly");
getCharac(p1);
getCharac(p2);
getCharac(p3);//
报错!!!编译不能通过,提示参数类型不符合
因为String并不是Number的子类,因此上面也就不能编译通过了
类型通配符下界:可以这样定义:Person super Number>,表示我们的参数类型最小是Number类型,或者是它的超类类型。
下面我们来看一个题目:
Which three statements are true?
class A {}
class B extends A {} class C extends A {} class D extends B {} Which four statements are true ? A、The type Listis assignable to List. B、The type Listis assignable to List. C、The type List D、The type Listis assignable to List extends B>. E、The type Listis assignable to List. F、The type List G、The type Listis assignable to List.
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();
}
}
在-128至127这个区间,如果创建Integer对象的时候(1)Integer i = 1; (2) Integer i = Integer.valueOf(1); 如果是这两种情况创建出来的对象,那么其实只会创建一个对象,这些对象已经缓存在一个叫做IntegerCache里面了,所以==比较是相等的。如果不在-128至127这个区间,不管是通过什么方式创建出来的对象,==永远是false,也就是说他们的地址永远不会相等。
举例测试如下:
Integer i1 = 8;
Integer i2 = 8;
Integer i3 = 300; //超过了127这个范围
Integer i4 = 300;
Integer i5 = Integer.valueOf(8);
Integer i6 = new Integer(8);
// System.out.println(i1 == i2);//
true, 在
-128至127这个区间,Integer i = 1;和Integer i = Integer.valueOf(1); 这两种方式创建的对象相同
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;
}
}
// 所有整数 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 };
2、switch语句支持String参数
String str = "a";
switch (str) {
case "a":
System.out.println("a---");
break;
case "b":
System.out.println("b---");
break;
}
注意:在把字符串传进Switch case之前,别忘了检查字符串是否为Null。
3、数字类型的下划线表示 更友好的表示方式,不过要注意下划线添加的一些标准,可以参考下面的示例
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
Formula formula = new Formula() {
@Override
public double calculate(int a) {
return sqrt(a * 100);
}
};
formula.calculate(100); // 100.0
formula.sqrt(16); // 4.0
2、Lambda 表达式
首先看看在老版本的Java中是如何排列字符串的:
代码如下:
List names = Arrays.asList("peter", "anna", "mike", "xenia");
Collections.sort(names, new Comparator() {
@Override
public int compare(String a, String b) {
return b.compareTo(a);
}
});
java中最常用jar包的用途
jar包用途axis.jarSOAP引擎包commons-discovery-0.2.jar用来发现、查找和实现可插入式接口,提供一些一般类实例化、单件的生命周期管理的常用方法.jaxrpc.jarAxis运行所需要的组件包saaj.jar创建到端点的点到点连接的方法、创建并处理SOAP消息和附件的方法,以及接收和处理SOAP错误的方法. w
创建图表事件监听非常简单:首先是通过addEventListener('监听类型',js监听方法)添加事件监听,然后在js监听方法中定义具体监听逻辑。
以钻取操作为例,当用户点击图表某一个point的时候弹出point的name和value,代码如下:
<script>
//创建AnyChart
var chart = new AnyChart();
//添加钻取操作&quo
我们来看下面的例子:
create or replace view testview
as
select empno,ename from emp where ename like ‘M%’
with check option;
这里我们创建了一个视图,并使用了with check option来限制了视图。 然后我们来看一下视图包含的结果:
select * from testv