Java之JDK7的新语法探索
JAVA 探索 语法
2023-09-11 14:16:53 时间
Java
之JDK7
的新语法探索
前言
感谢! 承蒙关照~
字面量:
各种精致的表达方式:
八进制以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<String> it = list.iterator(); it.hasNext(); ){
System.out.pritnln(it.next());
}
List<String> list = new ArrayList<>();
public class Demo {
public static void main(String[] args){
// List<String> list = new ArrayList<String>();
List<String> list = new ArrayList<>();
list.add("abc");
// Iterator<String> 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后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通
简书博客: 达叔小生
https://www.jianshu.com/u/c785ece603d1
结语
- 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
- 小礼物走一走 or 点赞
相关文章
- 深入java面向对象五:Java的内存管理
- JAVA学习(四):Java流程控制语句(顺序结构、if条件语句、switch条件语句、循环语句与跳转语句)
- JAVA学习(三):Java基础语法(变量、常量、数据类型、运算符与数据类型转换)
- Java实现 蓝桥杯VIP 算法训练 最大质因数(暴力)
- Java实现 蓝桥杯 算法训练 My Bad(暴力)
- Java实现 LeetCode 529 扫雷游戏(DFS)
- Java实现 洛谷 P1567 统计天数
- java实现第三届蓝桥杯数量周期
- Java实现 蓝桥杯VIP 算法提高 交换Easy
- 【JAVA】基于MVC架构Java技术荟萃案例演练
- 【JAVA】MacBook安装Java环境及eclipse
- Java操作HBase 2.0.5:创建表代码示例
- Java多线程系列——原子类的实现(CAS算法)
- paip.myeclipse7 java webservice 最佳实践o228
- java 反射机制
- 华为OD机试 - 探索地块建立(Java & JS & Python)
- php与java语法的区别
- Simple Logging Facade for Java (SLF4J)作用(java日志框架)
- 解决idea出现的java.lang.OutOfMemoryError: Java heap space的问题
- 一文详解java线程池 详解Java线程池的七个参数 详解池化技术 java如何选择核心线程数 详解Java线程池的拒绝策略
- Java 异常解决之java.lang.IllegalArgumentException: Comparison method violates its general contract!
- 使用Java操作Elasticsearch(Elasticsearch的java api使用)
- Java开发技术之成为高级java工程师必须学习的三个技术
- JAVA语言之Java 中不同的并行实现的性能比较
- Java 线程池详解
- 【java】Java 封装
- 【java】Java并发编程--Java实现多线程的4种方式
- 【java】Java IO体系总览