java 新特性

1.      可变参数

方法参数的定义:参数类型 …变量,这个时候参数是数组的形式,定义形式不是数组却是按照数组的方式进行操作的。

例:

package cn.mldn.util;

public class Message {
	public static void main(String args[]){
		System.out.println(add(new int[]{1,2,3}));
		System.out.println(add(1,2,3,4,5,6));
		System.out.println(add());
	}
	public static int add(int ... data){
		int sum=0;
		for(int x:data){
			sum+=x;
		}
		return sum;
	}
}

2.foreach输出——增强型for循环

for(数据类型 变量:数组或集合){

//操作代码

}

3.静态导入

如果一个类中的全部方法都是static型的,则:

import static 包.类.*;

所谓静态导入就是将这个类中的全部静态方法都导入进来,最后好像这些方法全是主类中定义的方法一样。

4.泛型

例:

package cn.mldn.util;

class Point<T>{
	private T x;
	private T y;
	public void setX(T x){
		this.x=x;
	}
	public void setY(T y){
		this.y=y;
	}
	public T getX(){
		return x;
	}
	public T getY(){
		return y;
	}
	
}

public class Message {
	public static void main(String args[])throws Exception{
		Point<String> point=new Point<String>();
		point.setX("东经");
		point.setY("西经");
		String x=point.getX();
		String y=point.getY();
		System.out.println(x   +   y);
		
		
	}
}
如果没有指定泛型,泛型将被擦除,使用Object接收参数。

4.1 通配符?

通配符可以接收任意参数的泛型类型的测试,但不能够修改,只能输出

对于“?”表示任意类型,如果有参数返回“?”,就当成Object理解

 

4.2 泛型接口

定义泛型接口

²  在子类上继续定义泛型

例:

package cn.mldn.test;

interface Message<T>{
	public String echo(T msg);  //接口里面只能是抽象方法与全局变量
}

class MessageImpl<T> implements Message<T>{
	@Override
	public String echo(T msg){
		return "Echo:"+msg;
	}
}

public class Test{
	public static void main(String args[])throws Exception{
		Message<String> msg=new MessageImpl<String>();
		System.out.println(msg.echo("lychee"));
	}
}

²  在子类上设置具体类型

例:

package cn.mldn.test;

interface Message<T>{
	public String echo(T msg);  //接口里面只能是抽象方法与全局变量
}

class MessageImpl implements Message<String>{
	@Override
	public String echo(String msg){
		return "Echo:"+msg;
	}
}

public class Test{
	public static void main(String args[])throws Exception{
		Message<String> msg=new MessageImpl();
		System.out.println(msg.echo("lychee"));
	}
}

4.3 泛型方法

例:

package cn.mldn.test;

public class Trr {
	public static void main(String args[])throws Exception{
		Integer result[]=get(1,2,3);
		for(int x:result){
			System.out.print(x+"、");
		}
	}
	public static <T> T[] get(T ... a){
		return a;
	}

}

5. 枚举

例:

package cn.mldn.test;

enum Color{
	RED,GREEN,BLUE;
}

public class Trr {
	public static void main(String args[])throws Exception{
		Color c=Color.RED;
		System.out.println(c);
	}
	
}

Ø  取得枚举的序号:public final int ordinal();

Ø  取得枚举的名称:public final String name();

Ø  Values()方法取得枚举中的全部对象(具体定义形式没找到)

 

switch可以判断intcharenumString型数据

 每一个枚举类都是继承Enum类

5.1 枚举的其他定义

枚举属于多例设计模式,类中可以有属性、构造方法、普通方法。在枚举中构造方法绝对不能是public,必须私有化,除此之外,枚举中每一个定义的对象,必须写在枚举类的首行。

例:让枚举实现接口

package cn.mldn.test;

interface Message1{
	public String getColor();
}

enum Color implements Message1{
	RED("红色"),GREEN("绿色"),BLUE("蓝色");
	private String title;
	private Color(String title){
		this.title=title;
	}
	public String toString(){
		return this.title;
	}
	public String getColor(){
		return this.toString();
	}
}

public class Trr {
	public static void main(String args[])throws Exception{
		Message1 c=Color.RED;
		System.out.println(c);
	}
	
}

在枚举中可以直接定义抽象方法,这就要求枚举中的每一个枚举对象分别实现这个抽象方法。

 

6. Annotation注释

6.1 准确的覆写 @Override

6.2 声明过期操作 @Deprecated

6.3 压制警告信息 @SuppressWarnings


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public static String loadAFileToStringDE1(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new BufferedInputStream( new FileInputStream(f) ); long contentLength = f.length(); ByteArrayOutputStream outstream = new ByteArrayOutputStream( contentLength > 0 ? (int) contentLength : 1024); byte[] buffer = new byte[4096]; int len; while ((len = is.read(buffer)) > 0) { outstream.write(buffer, 0, len); } outstream.close(); ret = outstream.toString(); //byte[] ba = outstream.toByteArray(); //ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法1用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE2(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new FileInputStream(f) ; long contentLength = f.length(); byte[] ba = new byte[(int)contentLength]; is.read(ba); ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法2用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE3(File f) throws IOException { long beginTime = System.currentTimeMillis(); BufferedReader br = null; String ret = null; try { br = new BufferedReader(new FileReader(f)); String line = null; StringBuffer sb = new StringBuffer((int)f.length()); while( (line = br.readLine() ) != null ) { sb.append(line).append(LINE_BREAK); } ret = sb.toString(); } finally { if(br!=null) {try{br.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法3用时"+ (endTime-beginTime) + "ms"); return ret; } 3个方法去读取一个大于50M的文件,当不设置jvm参数时都OutofMemery,当设置-Xmx128M时。只有方法3 可以通过,设置到-Xmx256M时也只有方法3可以通过,干脆设置512M,都可以了,运行时间如果正常的话一般都在4~5S

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值