博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java之JDK7的新语法探索
阅读量:6863 次
发布时间:2019-06-26

本文共 3851 字,大约阅读时间需要 12 分钟。

JavaJDK7的新语法探索

前言

感谢! 承蒙关照~

字面量:

各种精致的表达方式:

八进制以0开头,十六进制0X开头,二进制以0B开头.

二进制运算时,应该写成这样才直观:

&15 -> &0B1111复制代码

JDK7使用下划线(_)对数据进行分隔.

下划线不要这样做:

不要用于进制标识和数值之间,不要定义在数值开头和结尾,不要定义在小数点旁边.

// 错误0B_1111; _0X32_;  1_._223;复制代码

二进制的表示方式:

public class BinaryDemo { public static void main(String[] args){  // 定义二进制0B开头  int x=0B111100;  System.out.println(x);  int n1 = x & 0b1111;  System.out.println(n1);  // 用下划线 int y = 12_34_56; int z = 0b111_0123_234; double b = 23.223_23; }}复制代码

switch

public class Demo{ public static void main(String[] args){  int week = 1;  // 会在内存中进行存储,运算中的数值存储起来运算  if(week == 1){   System.out.println("星期一");  }else if(week == 2){    System.out.println("星期二");  }else {   System.out.println("输入错误");  } } // 字节码,标记,选择的值,是一个标识,对应的执行的代码 switch(week){  case 1:   System.out.println("星期一");   break;  case 2:   System.out.println("星期二");   break;  default:   System.out.println("输入错误"); }   public static void Demo(){   String sex="男";   if(sex.equals("男")){     System.out.println("男");   }else{    System.out.println("女");   }   // 编译器的强大   switch(sex){    case "男":     System.out.println("你好");     break;    case "女":     System.out.println("你好");     break;    default:     System.out.println("hello");   }  }}复制代码
// sex.hashCode();{ String sex = "男"; String s;  switch ((s=sex).hashCode())  {   default:    break;   case 22899:    if(!s.equals("女"))     break;    System.out.println("女孩你好");     break labe10;   case 30007:    if(s.equals("男"))    {      System.out.println("先生");      break labe10;    }    break;  }  System.out.println("你好");}复制代码

泛型

Java7简化,左边定义类型,右边不用定义类型,写<>;

for(Iterator
it = list.iterator(); it.hasNext(); ){ System.out.pritnln(it.next());}复制代码
List
list = new ArrayList<>();复制代码
public class Demo { public static void main(String[] args){  // List
list = new ArrayList
(); List
list = new ArrayList<>(); list.add("abc"); // Iterator
it = list.iterator(); }}复制代码

catch

public class Demo{ int[] arr=new int[3]; try{  int element = getElement(arr,1); }catch(throwNullPointerException){ }catch(ArrayIndexOutOfBoundsException){ } try{  int element = getElement(arr,1); }catch(throwNullPointerException || ArrayIndexOutOfBoundsException e){ }}// 添加数组,添加角标元素public static int getElement(int[] arr, int index) throws NullPointerException, ArrayIndexOutOfBoundsException{ if(arr == null){  throw new NullPointerException("数组不存在"); } if(index<0 || index>= arr.length){  throw new ArrayIndexOutOfBoundsException("数组角标不存在''); } return arr[index];}复制代码

try_with_resource

public class Demo{ public static void main(String[] args){  FileReader fr = null;  try{   fr = new FileReader ("dashu.txt");   int ch = fr.read();   System.out.println(ch);   }catch(IOException e){      }finally{      fr.close();   } }}复制代码

声明:

public static void function() throws IOException{ FileReader fr = null; try{  fr = new FileReader("dashu.txt");  int ch = fr.read();  System.out.println(ch);  }finally{   if(fr!=null)    try{     fr.close();    }catch(IOException e){     throw new RuntimeException();    }  } }}复制代码
// 自动释放,关闭的资源会在try()中定义public static void Demo() throws IOException{ try(FileReader fr = new FileReader("dashu.txt");FileWriter fw = new FileWriter("dashucoding.txt")) {  int ch = fr.read();  fw.write(ch);  System.out.println(ch); }}复制代码
public static void function() throws IOException{ Exception exception; exception = null; Object obj = null; FileReader fr = new FileReader("dashu.txt"); FileWriter fw = new FileWriter("dashucoding.txt"); int ch = fr.read();  fw.write(ch); System.out.println(ch);  if(fw != null)   fw.close(); break MISSING_BLOCK_LABFL_66; exception; ...}复制代码

达叔小生:往后余生,唯独有你 You and me, we are family ! 90后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通 简书博客: 达叔小生

结语

  • 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
  • 小礼物走一走 or 点赞

转载地址:http://pdeyl.baihongyu.com/

你可能感兴趣的文章
解读Raft(二 选举和日志复制)
查看>>
MySQL binlog
查看>>
Vertica 高可用性测试
查看>>
用500行Julia代码开始深度学习之旅 Beginning deep learning with 500 lines of Julia
查看>>
html 标签 中 的Lang 有什么用
查看>>
【CUDA学习】GPU硬件结构
查看>>
android Run模式也会出现"Waiting for debugger"的解决方法
查看>>
T-SQL查询进阶--详解公用表表达式(CTE)
查看>>
读书笔记:《搞定3--平衡工作和生活的艺术》
查看>>
MySQL 查询重复记录
查看>>
6. 简单又复杂的“运算符”,建议你看一哈
查看>>
开源Android或将收费
查看>>
阿里云服务器拼团购,拉上小伙伴立享¥234/年
查看>>
【转】浅谈php://filter的妙用
查看>>
Docker安装Mysql服务
查看>>
阿里云Redis账号
查看>>
跟我学习php文件和目录常用函数-下篇
查看>>
阿里云云盾-风险识别-增强版模式发布
查看>>
赛灵思CEO Victor Peng:中国AI市场创新速度令人振奋,但初创企业应避免扎堆做AI芯片...
查看>>
Spring Security OAuth 2开发者指南译
查看>>