首页 > 编程语言 >从头学Java17-今天的Kotlin更香吗

从头学Java17-今天的Kotlin更香吗

时间:2023-07-15 16:34:22浏览次数:50  
标签:Java17 Java String val 更香 Kotlin length println

出于各种限制,很多公司依然停留在Java8,部分小伙伴转向了Kotlin。Kotlin作为静态编译语言,提供大量语法糖,而且编译后的字节码跟Java一致。

当时,Java8于2014年发布,Kotlin于2016年,很多宣称的语法糖都是对比的Java8。不禁要问,相对今天的Java17,Kotlin优势还在吗?

现在就用最新的Kotlin1.9.0,对前三篇文章里的lambda、StreamAPI依次改造,实践出真知!

编写lambda、调用

Java

import Java.util.*;
import Java.util.function.*;

/**
 *
 * @author 烧哥burn.red
 */
public class Test1 {
    
    public static void main(String[] args) {
        
        Predicate<String> predicate = s -> s.length() == 3;
        Consumer<String> consumer = s -> System.out.println(s);
        Supplier<String> supplier = () -> "Hello Duke!";
        Function<String, Integer> function = s -> s.length();
        
        IntSupplier intSupplier = () -> 1;
        IntConsumer intConsumer = s -> System.out.println(s);
        IntPredicate intPredicate = i -> i > 10;
        ToIntFunction<String> toIntFunction = s -> s.length();
        UnaryOperator<String> unaryOperator = s -> s.toUpperCase();
        
        BiConsumer<String, Integer> biConsumer = (s, number) -> s.indexOf(number);
        ObjIntConsumer<String> objIntConsumer = (s, value) -> System.out.printf("%s,%d\n", s, value);
        BiPredicate<String, Integer> biPredicate = (word, length) -> word.length() == length;
        BiFunction<String, String, Integer> biFunction = (word, sentence) -> sentence.indexOf(word);
        ToIntBiFunction<String, String> toIntBiFunction = (word, sentence) -> sentence.indexOf(word);
        
        String a = "aaa";
        
        if (predicate.test(a)) {
            consumer.accept(a);
            supplier.get();
            function.apply(a);
            
            intConsumer.accept(1);
            intSupplier.getAsInt();
            intPredicate.test(11);
            toIntFunction.applyAsInt(a);
            unaryOperator.apply(a);
            
            biConsumer.accept(a, 2);
            objIntConsumer.accept(null, 1);
            biPredicate.test(a, 3);
            biFunction.apply("fdsa", a);
            toIntBiFunction.applyAsInt("fdsa", a);
        }
        List<String> strings = new ArrayList<>(List.of("a", "bb", "ccc"));
        strings.forEach(consumer);
        strings.removeIf(predicate);//不应该在不可变集合上调用
        System.out.println(strings);
        
        strings = Arrays.asList("a", "bb", "ccc");
        strings.replaceAll(unaryOperator);
        System.out.println(strings);

//        int i = 0;
//        Consumer<Integer> add = s -> i++;//报错,从lambda 表达式引用的本地变量必须是最终变量或实际上的最终变量
    }
}

Kotlin

/**
 *
 * @author 烧哥burn.red
 */

fun main() {
    val predicate = { s: String -> s.length == 3 }
    val consumer = { s: String? -> println(s) }
    val supplier = { "Hello Duke!" }
    val function = { s: String -> s.length }

    val intSupplier = { 1 }
    val intConsumer = { s: Int -> println(s) }
    val intPredicate = { i: Int -> i > 10 }
    val toIntFunction = { s: String -> s.length }
    val unaryOperator = { s: String -> s.uppercase() }

    val biConsumer = { s: String, number: Int -> s.indexOf(number.toChar()) }
    val objIntConsumer = { s: String?, value: Int -> println("$s,$value") }
    val biPredicate = { word: String, length: Int -> word.length == length }
    val biFunction = { word: String?, sentence: String -> sentence.indexOf(word!!) }
    val toIntBiFunction = { word: String?, sentence: String -> sentence.indexOf(word!!) }

    val a = "aaa"

    if (predicate(a)) {
        consumer(a)
        supplier()
        function(a)

        intConsumer(1)
        intSupplier()
        intPredicate(11)
        toIntFunction(a)
        unaryOperator(a)

        biConsumer(a, 2)
        objIntConsumer(null, 1)
        biPredicate(a, 3)
        biFunction("fdsa", a)
        toIntBiFunction("fdsa", a)
    }
    var strings = mutableListOf("a", "bb", "ccc")
    strings.forEach(consumer)
    strings.removeIf(predicate) //不应该在不可变集合上调用
    println(strings)

    strings = arrayListOf("a", "bb", "ccc")
    strings.replaceAll(unaryOperator)
    println(strings)

    var i = 0
    val add = { s: Int? -> i++ } //不报错
    add(i)
    println(i)
}

可以看出:

  • Kotlin的lambda,没有那四种划分,调用时类似函数,(参数..),非常简洁
  • Kotlin的lambda,可以改变外层变量的值
  • Kotlin没有自己的removeIf,replaceAll,但可以直接调用Java的
  • Java为原始类型准备了特别版,Kotlin默认都是原始类型
  • Kotlin变量默认都是非null

这一局,Kotlin胜出。

方法引用、链接

Java

import red.burn.bean.User;

import Java.util.*;
import Java.util.function.*;
import Java.util.logging.Logger;

/**
 * @author 烧哥burn.red
 */
public class Test2 {

    public static void main(String[] args) {
        //方法引用
        DoubleUnaryOperator sqrt = Math::sqrt;
        IntBinaryOperator max = Integer::max;//静态方法引用
        Supplier<List<String>> newListOfStrings = ArrayList::new;//构造方法引用
        Consumer<String> printer = System.out::println;//绑定到System.out
        Function<String, Integer> toLength = String::length;//非绑定,绑定到String的实例

        //Lambla的链接
        Predicate<String> isNull = Objects::isNull;
        Predicate<String> isEmpty = String::isEmpty;
        Predicate<String> isNullOrEmpty = isNull.or(isEmpty);
        Predicate<String> isNotNullNorEmpty = isNullOrEmpty.negate();
        Predicate<String> shorterThan5 = s -> s.length() < 5;
        Predicate<String> p = isNotNullNorEmpty.and(shorterThan5);

        Logger logger = Logger.getLogger("MyApplicationLogger");
        Consumer<String> log = logger::info;
        Consumer<String> printStr = System.out::println;
        Consumer<String> printAndLog = log.andThen(printStr);//
        printAndLog.accept("test");

        Function<String, Integer> function1 = String::length;
        Function<Integer, Integer> function2 = s -> ++s;
        Function<String, Integer> function = function1.andThen(function2);
        System.out.println("new=" + function.apply("abc")); //4

        Function<String, String> id = Function.identity();

        //Comparator
        Comparator<Integer> comparator = Integer::compare;
        Comparator<String> comparator1 = (s1, s2) -> Integer.compare(s1.length(), s2.length());
        Comparator<String> comparator2 = (s1, s2) -> Integer.compare(toLength.apply(s1), toLength.apply(s2));
        Comparator<String> comparator3 = Comparator.comparing(String::length);

        Comparator<User> byFirstName = Comparator.comparing(User::getFirstName);
        Comparator<User> byLastName = Comparator.comparing(User::getLastName);
        Comparator<User> byFirstNameThenLastName = byFirstName.thenComparing(byLastName)
                                                              .thenComparingInt(User::getAge);
        Comparator<User> byFirstNameThenLastName1 = Comparator.comparingInt(User::getAge)
                                                              .thenComparing(
                                                                      Comparator.nullsLast(Comparator.naturalOrder()));

        List<String> strings = Arrays.asList("one", "two", "three", "four", "five");
        strings.sort(comparator3.reversed());
        System.out.println(strings);
    }
}
import lombok.Builder;
import lombok.Data;
import org.jetbrains.annotations.NotNull;

/**
 * @author 烧哥burn.red
 */
@Data
@Builder
public class User implements Comparable<User> {

    private String name;
    private int age;
    private String firstName;
    private String lastName;

    @Override
    public int compareTo(User o) {

        return this.name.compareTo(o.name);
    }
}

Kotlin

import Java.util.logging.Logger
import Kotlin.math.sqrt
import red.burn.bean.UserKT
import Kotlin.math.max

/**
 *
 * @author 烧哥burn.red
 */
fun main() {
    //方法引用
    val sqrt = ::sqrt
//    val max = ::max //报错,歧义
//    val newListOfStrings = ::ArrayList  //报错,歧义
//    val printer =::println //报错,歧义
    val ar = 5.run<Int, ArrayList<String>>(::ArrayList)
    "a".run(::println)
    val kt = ::UserKT //构造方法引用
    val user = kt("abc", 10)
    var (name, age) = UserKT("csc")
    val firstName = user::firstName //属性引用
    val addAge = user::addAge //函数引用
    val toLength = String::length  //非绑定,绑定到String的实例

    //Lambla的链接
    val isNull = { obj: String? -> obj == null }
    val isEmpty = { obj: String -> obj.isEmpty() }
    val isNullOrEmpty = { obj: String? -> obj == null || isEmpty(obj) }
    val isNotNullNorEmpty = { obj: String? -> !isNullOrEmpty(obj) }
    val shorterThan5 = { s: String -> s.length < 5 }
    val p = { s: String -> isNotNullNorEmpty(s).and(shorterThan5(s)) }

    val logger = Logger.getLogger("MyApplicationLogger")
    val log = { message: String? -> logger.info(message) }
    val printStr = { message: String? -> println(message) }
    val printAndLog = { message: String? ->
        log(message).also { printStr(message) }
    }
    printAndLog("test")

    val function1 = String::length
//    val function2 = {  s: Int -> ++s }//报错 Val cannot be reassigned
    val function2 = { s: Int ->
        var i = s;
        ++i;
    }
    val function = { s: String -> function1(s).let(function2) }
    println("new=" + function("abc")) //4

    val id = { s: String? -> s }

    //Comparator
    val comparator = { x: Int, y: Int -> (x).compareTo(y) }
    val comparator1 = { s1: String, s2: String -> s1.length.compareTo(s2.length) }
    val comparator2 = { s1: String, s2: String -> toLength(s1).compareTo(toLength(s2)) }
    val comparator3 = compareBy(String::length)

    val byFirstName = compareBy(UserKT::firstName)
    val byLastName = compareBy(UserKT::lastName)
    val byFirstNameThenLastName = byFirstName.then(byLastName).thenBy(UserKT::age)
    val byFirstNameThenLastName1 = compareBy(UserKT::age).then(nullsLast(naturalOrder()))

    val strings = arrayListOf("one", "two", "three", "four", "five")
    strings.sortWith(comparator3.reversed())
    println(strings)
}

/**
 *
 * @author 烧哥burn.red
 */
data class UserKT(var name: String, var age: Int = 1) : Comparable<UserKT> {

    var firstName: String? = null
    var lastName: String? = null

    override fun compareTo(other: UserKT): Int {
        return name.compareTo(other.name)
    }

    fun printUser(s: UserKT) {
        println(s)
    }

    fun addAge(i: Int, j: Int): Int {
        return i + j
    }

}

可以看出:

  • Kotlin的lambda,可以有类引用、函数引用、属性引用、构造引用,其中函数引用不能有歧义
  • Kotlin的lambda,因为没有四种划分,缺乏Java里丰富的链接方式,不过可以自己实现
  • Kotlin的lambda,无法修改自己的参数,只能读取
  • Kotlin可读性比较强,Java容易看的分神

这一局,Kotlin跟Java打平。

StreamAPI

Java

import Java.util.List;
import Java.util.Map;
import Java.util.function.*;
import Java.util.stream.*;

/**
 * @author 烧哥burn.red
 */
public class Test3 {
    
    public static void main(String[] args) {
        
        //flatmap
        Function<String, Stream<Integer>> flatParser = s -> {
            try {
                return Stream.of(Integer.parseInt(s));
            } catch (NumberFormatException e) {
            }
            return Stream.empty();
        };
        
        List<String> strings = List.of("1", " ", "2", "3 ", "", "3");
        List<Integer> ints = strings.stream().flatMap(flatParser).toList();
        System.out.println("ints = " + ints);
        
        //mapMulti
        ints = strings.stream().<Integer>mapMulti((string, consumer) -> {
            try {
                consumer.accept(Integer.parseInt(string));
            } catch (NumberFormatException ignored) {
            }
        }).toList();
        System.out.println("ints = " + ints);
        
        List<Integer> ints2 = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
        List<Integer> result = ints2.stream().skip(2).limit(5).toList();
        System.out.println("result = " + result);
        
        List<Integer> list0 = List.of(1, 2, 3);
        List<Integer> list1 = List.of(4, 5, 6);
        List<Integer> list2 = List.of(7, 8, 9);
// 1st pattern: concat
        List<Integer> concat = Stream.concat(list0.stream(), list1.stream()).toList();
// 2nd pattern: flatMap
        List<Integer> flatMap = Stream.of(list0.stream(), list1.stream(), list2.stream())//类似city的外层组成的流
                                      .flatMap(Function.identity()).toList();
        System.out.println("concat  = " + concat);
        System.out.println("flatMap = " + flatMap);
        
        //reduce
        Stream<String> strings1 = Stream.of("one", "two", "three", "four");
        BinaryOperator<Integer> combiner = Integer::sum;
        Function<String, Integer> mapper = String::length;
        BiFunction<Integer, String, Integer> accumulator = (partialReduction, element) -> partialReduction + mapper.apply(element);
        int result1 = strings1.reduce(0, accumulator, combiner);
        System.out.println("sum = " + result1);
        
        //groupby map
        List<String> strings2 = List.of("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve");
        Map<Integer, Long> histogram = strings2.stream().collect(Collectors.groupingBy(String::length, Collectors.counting()));
        histogram.forEach((k, v) -> System.out.println(k + " :: " + v));
        
        Map<Long, List<Integer>> map = histogram.entrySet()
                                                .stream()
                                                .collect(Collectors.groupingBy(Map.Entry::getValue,
                                                                               Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
        Map.Entry<Long, List<Integer>> result2 = map.entrySet().stream().max(Map.Entry.comparingByKey())//再求max
                                                    .orElseThrow();
        System.out.println("result = " + result2);
    }
}

Kotlin

/**
 * @author 烧哥burn.red
 */
fun main() {
    
    //flatmap
    val flatParser = label@{ s: String ->
        try {
            return@label listOf(s.toInt())
        } catch (_: NumberFormatException) {
        }
        emptyList<Int>()
    }

    val strings = listOf("1", " ", "2", "3 ", "", "3")
    var ints = strings.flatMap(flatParser)
    println("ints = $ints")

    //mapMulti
    /*ints = strings.mapMulti { string: String, consumer: Consumer<Int?> ->
            try {
                consumer.accept(string.toInt())
            } catch (ignored: NumberFormatException) {
            }
        }
    println("ints = $ints")*/

    val ints2 = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
    val result = ints2.drop(2).take(5)
    println("result = $result")

    val list0 = listOf(1, 2, 3)
    val list1 = listOf(4, 5, 6)
    val list2 = listOf(7, 8, 9)
// 1st pattern: concat
    val concat = list0 + list1
// 2nd pattern: flatMap
    val flatMap = listOf(list0, list1, list2).flatten()
    println("concat  = $concat")
    println("flatMap = $flatMap")

    //reduce
    val strings1 = listOf("one", "two", "three", "four")
    val mapper = String::length
    val accumulator = { partialReduction: Int, element: String -> partialReduction + mapper(element) }
    val result1 = strings1.fold(0, accumulator)
    println("sum = $result1")

     //groupby map
    val strings2 = listOf("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve")
    val histogram: Map<Int, Int> = strings2.groupingBy { it.length }.eachCount()
    histogram.forEach({ k, v -> println("$k :: $v") })

    val map = histogram.map { it }.groupBy({ it.value }, { it.key }).maxBy { it.key }
    println(map)

}

可以看出:

  • Kotlin的lambda,不支持multiMap,但可以自己实现
  • Kotlin有运算符重载,可以对集合进行+-
  • flatten简化了flatmap,fold简化了reduce,eachCount简化了分组计数
  • 集合直接就是流,集合上的groupby等直接调用,不需要collect()
  • 函数很多有混淆,像groupBygroupingBymaxBymaxOf
  • 不过中间可能有null,还需要人工判断,不如Java,Optional总不会报错
  • groupBy({ it.value }, { it.key }).maxBy { it.key }这个能亮瞎

总体来说,代码量减少非常多,这局Kotlin胜出。

综合

Java

import red.burn.bean.*;

import Java.util.*;
import Java.util.function.BiFunction;
import Java.util.function.Function;
import Java.util.stream.*;

/**
 *
 * @author 烧哥burn.red
 */
public class Test4 {
    
    public static void main(String[] args) {
        
        Author au1 = new Author("Au1");
        Author au2 = new Author("Au2");
        Author au3 = new Author("Au3");
        Author au4 = new Author("Au4");
        Author au5 = new Author("Au5");
        Article a1 = new Article("a1", 1991, List.of(au1));
        Article a2 = new Article("a2", 1992, List.of(au1, au2));
        Article a3 = new Article("a3", 1993, List.of(au1, au3, au4));
        Article a4 = new Article("a4", 1992, List.of(au1, au2, au3, au4));
        List<Article> articles = List.of(a1, a2, a3, a4);
        
        BiFunction<Article, Author, Stream<PairOfAuthors>> buildPairOfAuthors =
                (article, firstAuthor) -> article.authors().stream().flatMap(
                        secondAuthor -> PairOfAuthors.of(firstAuthor, secondAuthor).stream());//Optional的Stream
        
        Function<Article, Stream<PairOfAuthors>> toPairOfAuthors =
                article -> article.authors().stream().flatMap(firstAuthor -> buildPairOfAuthors.apply(article, firstAuthor));
        Collector<PairOfAuthors, ?, Map<PairOfAuthors, Long>> collector1 =
                Collectors.groupingBy(Function.identity(), Collectors.counting());

//        System.out.println("numberOfAuthorsTogether=" + numberOfAuthorsTogether);
        Function<Map<PairOfAuthors, Long>, Map.Entry<PairOfAuthors, Long>> finisher1 =
                map1 -> map1.entrySet().stream().max(Map.Entry.comparingByValue()).orElseThrow();
        
        Map.Entry<PairOfAuthors, Long> result11 =
                articles.stream().flatMap(toPairOfAuthors).collect(Collectors.collectingAndThen(collector1, finisher1));
        
        Map.Entry<PairOfAuthors, Long> result12 =
                articles.stream().collect(Collectors.flatMapping(toPairOfAuthors, Collectors.collectingAndThen(collector1, finisher1)));
        
        //找出每年发表文章最多的两位联合作者
        Collector<Article, ?, Optional<Map.Entry<PairOfAuthors, Long>>> flatMapping = Collectors.flatMapping(toPairOfAuthors,
                                                                                                             Collectors.collectingAndThen(
                                                                                                                     collector1,
                                                                                                                     map2 -> map2.entrySet()
                                                                                                                                 .stream()
                                                                                                                                 .max(Map.Entry.comparingByValue())));
        Map<Integer, Optional<Map.Entry<PairOfAuthors, Long>>> result13 =
                articles.stream().collect(Collectors.groupingBy(Article::inceptionYear, flatMapping));
        Map<Integer, Map.Entry<PairOfAuthors, Long>> result14 = result13.entrySet()
                                                                        .stream()
                                                                        .flatMap(entry -> entry.getValue()
                                                                                               .map(value -> Map.entry(entry.getKey(),
                                                                                                                       value))
                                                                                               .stream())
                                                                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        
        System.out.println(result11);
        System.out.println(result12);
        System.out.println(result13);
        System.out.println(result14);
    }
}
public record Article(String title, int inceptionYear, List<Author> authors) {

}
public record Author(String name) implements Comparable<Author> {
    
    public int compareTo(Author other) {
        
        return this.name.compareTo(other.name);
    }
}

public record PairOfAuthors(Author first, Author second) {
    
    public static Optional<PairOfAuthors> of(Author first, Author second) {
        
        if (first.compareTo(second) > 0) {
            return Optional.of(new PairOfAuthors(first, second));
        } else {
            return Optional.empty();
        }
    }
}

Kotlin


可以看出:

  • 这个例子主要体现Optional跟StreamAPI的结合,Kotlin里没有Optional,所以很难写出。

这局Java胜。

最终

Kotlin以2:1微弱优势胜出。

Java用他的严谨,证明能实现从简单到复杂的各种场景。

Kotlin用它的简洁,通常情况下能减少工作量。

Kotlin还提供了委托、扩展、运算符重载、作用域函数、协程等等。

子曾经曰过”越简洁,越有坑“。想完全用Kotlin取代Java,还有一段路,目前二者可以互操作,建议同时使用。

复杂场景下,用什么语言并不是决定性的,解决方案才是。

标签:Java17,Java,String,val,更香,Kotlin,length,println
From: https://www.cnblogs.com/burn-red/p/17556437.html

相关文章

  • Kotlin 协程之Practice
    Kotlin协程作用让线程主动释放CPU是协程一个作用,一个协程执行后挂起,然后让另一个协程执行,等到这个协程执行完毕再让前一个协程继续执行。协程可以不依赖于线程直接运行在进程中;协程通过提升CPU的利用率,进而减少线程切换,进而提升程序运行效率;协程启动与停止都是由代码操作,而不是操......
  • Kotlin中的泛型:协变与逆变
    协变与逆变现在假设存在类A和类B,以及泛型类LIst<A>和泛型类LIst<B>,则协变和逆变的定义如下:协变如果A是B的子类,且List<A>是List<B>的子类,那么可以说泛型List<T>是协变的逆变如果A是B的子类,且List<B>是List<A>的子类,那么可以说泛型List<T>是逆变的Java中的泛型Java中的......
  • Kotlin 常用语法糖记录
    原文地址:Kotlin常用语法糖记录-Stars-One的杂货小窝当使用Kotlin编程时,有一些常用的函数可以帮助我们简化代码并提高开发效率。稍微列举下常用的方法runCatchingrunCatching是一个用于处理可能引发异常的代码块的函数。它提供了一种更简洁和安全的方式来执行可能出现......
  • Kotlin协程:打破线程框架的思维
    Kotlin协程:打破线程框架的思维前言协程是Kotlin对比Java的最大优势,需要理解协程的设计理念和知识体系,建立协程思维模型。本文将介绍协程的概念、特性和原理,以及如何在Android开发中使用协程来简化并发编程和优化软件架构。什么是协程协程是一种互相协作的程序,可以在任意地方挂......
  • 从头学Java17-Stream API(二)结合Record、Optional
    StreamAPIStreamAPI是按照map/filter/reduce方法处理内存中数据的最佳工具。本系列教程由Record讲起,然后结合Optional,讨论collector的设计。使用Record对不可变数据进行建模Java语言为您提供了几种创建不可变类的方法。可能最直接的是创建一个包含final字段的final类。......
  • Java17新特性及代码示例:还在使用Java8? 这5个Java17新功能,你会喜欢的
    SpringBoot3.0最低支持JDK17,各开源软件正在全面拥抱JDK17.有升级计划的调查受访者中,37%的人计划在未来六个月内升级到2021年9月份发布的LTS版本JDK17。另有25%的人计划在未来6到12个月内升级到JDK17。这5个你喜欢的JDK17新功能,会让你升级JDK17吗?1.record类(记录类)传统的......
  • Spring Boot 3.0.0 来啦!最小依赖 Java17!升还是不升?
    Spring官方于2022年1月20日发布SpringBoot3.0.0-M1版本,预示开启了SpringBoot3.0的里程碑。官方公告下的中文评论有点东西。。。熟悉的味道!就是那个味!  分享一篇朋友对SpringBoot3.0的介绍:生还是不生?SpringBoot3版本有起飞前兆,最小依赖Java17!一直......
  • 从头学Java17-Stream API(一)
    StreamAPIStreamAPI是按照map/filter/reduce方法处理内存中数据的最佳工具。本系列中的教程包含从基本概念一直到collector设计和并行流。在流上添加中继操作将一个流map为另一个流map流是使用函数转换其元素。此转换可能会更改该流处理的元素的类型,但您也可以在不更改......
  • 从头学Java17-Lambda表达式
    Lambda表达式这一系列教程,旨在介绍lambda的概念,同时逐步教授如何在实践中使用它们。回顾表达式、语句表达式表达式由变量、运算符和方法调用组成,其计算结果为单个值。您已经看到了表达式的示例,如下面的代码所示:intcadence=0;anArray[0]=100;System.out.println("......
  • 从头学Java17-Modules模块
    模块Modules了解module系统如何塑造JDK,如何使用,使项目更易于维护。烧哥注从头讲JDK17的文章比较少,英文为主,老外虽能讲清原理,但写的比较绕,所以决定翻译一下,也有个别细节完善。原文关注点主要在java生态,以及类库的维护者如何过渡到module,对新用户也同样适用。module简介......