语法糖(Syntactic sugar)
是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用。通常来说使用语法糖能够增加程序的可读性,从而减少程序代码出错的机会。
功能和传统的fori相似
代码样例
public class Test {
public static void main(String[] args) {
int[] num = new int[10];
for (int i = 0; i < 10; i++) {
num[i] = i;
}
//旧版本
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+" ");
}
System.out.println();
//新版本
for (int i:num) {
System.out.print(num[i]+" ");
}
}
}
输出对比
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
特性
变量的取值只在一个有限的集合里面
代码样例
public class Test {
public static void main(String[] args) {
enum Size{
SMALL,MEDIUM,LARGE;
}
Size s1 = Size.SMALL;
Size s2 = Size.MEDIUM;
Size s3 = Size.LARGE;
Size s4 = Size.SMALL;
System.out.println(s1==s4);
System.out.println(s3==s4);
}
}
输出样例
true
false
特性
普通函数的形参列表是固定个数/类型/顺序
JDK5.0提供了不定项参数(可变参数)功能
代码样例
public class Test {
public static void main(String[] args) {
test("aaa");
test("aaa","bbb","ccc");
}
public static void test(String... args){
System.out.println(args.length);
for (String arg:args) {
System.out.println(arg);
}
}
}
输出样例
1
aaa
3
aaa
bbb
ccc
特性
import导入程序所需要的类
import static导入一个类的静态方法和静态常量(JDK5.0引入)
代码样例
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;
import static java.lang.System.out;
public class Test {
public static void main(String[] args) {
int a=3,b=4,c=0;
//静态引入
c = (int) sqrt(pow(a,2)+pow(b,2));
//原本用法
c = (int) Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
//静态引入
out.println(c);
//原本用法
System.out.println(c);
}
}
输出样例
5
5
特性
在java中,基本类型放在栈里面,对象则是在堆里开辟了内存空间,把对象的引用存入栈里面,基本类型可以包含在对象里面,所以形象的形容为装箱
从JDK5.0开始引入,简化了基本类型和对象转化的写法
基本类型:boolean/byte/char/int/short/long/float/double
对象:Boolean/Byte/Character/Integer/Short/Long/Float/Double
代码样例
public class Test {
public static void main(String[] args) {
//自动装箱
Integer obj1 = 5;
//原始用法
Integer obj2 = Integer.valueOf(5);
//自动拆箱
int a1 = obj1;
//原始用法
int a2 = obj2.intValue();
System.out.println(obj1);
System.out.println(a1);
System.out.println(obj2);
System.out.println(a2);
}
}
输出样例
5
5
5
5
特性
多个异常并列在一个catch中
从JDK7.0Y引入,简化写法
代码样例
import java.io.IOException;
import java.sql.SQLException;
public class Test {
public static void main(String[] args) {
//旧版本
try {
test();
}
catch (IOException ex){
//异常处理
}
catch (SQLException ex){
//异常处理
}
//新版本
try {
test();
}
catch (IOException | SQLException ex){
//异常处理
}
}
private static void test() {
}
}
特性
从JDK7.0开始引入
整数类型可以用二进制进行赋值
代码样例
public class Test {
public static void main(String[] args) {
byte a1 = (byte) 0b00100001;
short a2 = (short) 0b10101010101;
int a3 = 0b101;
long a4 = 0b101010101011011011010101011L;
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
}
}
输出样例
33
1365
5
89503403
特性
从JDK7.0开始引入
在数字字面量中使用下划线
代码样例
public class Test {
public static void main(String[] args) {
//二进制,0b开头
int a1 = 0b0100_1011_0001;
//八进制,0开头
int a2 = 02_014;
int a3 = 123__45;
//十六进制,0x开头
int a4 = 0x7_567;
float a5 = 3.56_78f;
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
System.out.println(a5);
}
}
输出样例
1201
1036
12345
30055
3.5678
特性
java最初的设计中,接口的方法都是没有实现的公开的
JDK8.0推出接口的默认方法/静态方法(都带实现的),为Lambda表达式提供支持
代码样例
public class Test {
public interface Animal{
public void move();
}
public interface NewAnimal{
public default void move(){
System.out.println("I can move");
};
}
}
特性
JDK8.0推出带实现的静态方法
代码样例
public class Test {
public interface StaticAnimal{
public static void move(){
System.out.println("I can move");
}
}
public interface StaticLandAnimal extends StaticAnimal{
//继承不到StaticAnimal的move方法
}
public static void main(String[] args) {
//正确引用
StaticAnimal.move();
//错误引用
StaticLandAnimal.move();
new StaticAnimal().move();
}
}
特性
JDK9.0推出接口的私有方法
代码样例
public class Test {
public interface StaticAnimal{
private void move(){
System.out.println("I can move");
}
}
public interface StaticLandAnimal extends StaticAnimal{
//继承不到StaticAnimal的move方法
}
public static void main(String[] args) {
//错误引用
StaticAnimal.move();
StaticLandAnimal.move();
new StaticAnimal().move();
}
}
特性
程序如果打开外部资源,那么使用后必须正确关闭
考虑异常因素,java提供try-catch-finally进行保证
JDK7.0提供try-with-resour,比try-catch-finally更加方便
代码样例
public class Test implements AutoCloseable {
private int age = 18;
@Override
public void close() throws Exception {
System.out.println("关闭成功");
}
public static void main(String[] args) {
try(Test11 OpenResource = new Test()){
System.out.println(OpenResource.age);
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出样例
18
关闭成功
特性
JDK8及以前,ResourceBundle默认以ISO-8859-1方式加载文件
JDK9及以后,ResourceBundle默认以UTF-8方式加载文件
java以前一直是一种强类型的语言
每个变量在定义时候就确定了类型
JDK10推出了var:局部变量推断
代码样例
public class Test {
public static void main(String[] args) {
var a1 = 5;
var a2 = 0.025f;
var a3 = "abc";
int b1 = 5;
float b2 = 0.025f;
String b3 = "abc";
System.out.println(a1==b1);
System.out.println(a2==b2);
System.out.println(a3==b3);
}
}
输出样例
true
true
true
特性
JDK12推出
代码样例
public class Test {
public static void main(String[] args) {
String month = null;
int result;
switch (month){
case "Jan","Mar","July","Aug","Oct","Dec" -> result = 31;
case "Apr","June","Sep","Nov" -> result = 30;
case "Feb" -> result = 28;
default -> result = -1;
}
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_50216270/article/details/117121810
内容来源于网络,如有侵权,请联系作者删除!