当前位置: 首页 > news >正文

施工企业副总经理竞聘优化网站推广网站

施工企业副总经理竞聘,优化网站推广网站,在搜狐快站上做网站怎么跳转,什么网站可以免费做视频目录 一、Java21新特性 1、字符串模版 2、scoped values 3、record pattern 4、switch格式匹配 5、可以在switch中使用when 6、Unnamed Classes and Instance Main Methods 7、Structured Concurrency 一、Java21新特性 1、字符串模版 字符串模版可以让开发者更简洁的…

目录

一、Java21新特性

1、字符串模版

2、scoped values

3、record pattern

4、switch格式匹配

5、可以在switch中使用when

6、Unnamed Classes and Instance Main Methods

7、Structured Concurrency


一、Java21新特性

1、字符串模版

        字符串模版可以让开发者更简洁的进行字符串拼接(例如拼接sql,xml,json等)。该特性并不是为字符串拼接运算符+提供的语法,也并非为了替换StringBuffer和StringBuilder。

利用STR模版进行字符串与变量的拼接

public class StringTest {public static void main(String[] args) {String sport="basketball";String msg=STR."I like \{sport}";System.out.println(msg);}
}
预览功能(java文件不要加package包信息)
$>javac --enable-preview -source 21 StringTest.java
注: StringTest.java 使用 Java SE 21 的预览功能。
注: 有关详细信息,请使用 -Xlint:preview 重新编译。
$>java --enable-preview  StringTest
I like basketball

上面使用的STR是java中定义的模版处理器,它可以将变量的值取出,完成字符串的拼接。在每个java源文件中都引入了一个public static final修饰的STR属性,STR通过打印STR可以知道它是java.lang.StringTemplate,是一个接口。

Processor<String, RuntimeException> STR = StringTemplate::interpolate;default String interpolate() {return StringTemplate.interpolate(fragments(), values());}static String interpolate(List<String> fragments, List<?> values) {Objects.requireNonNull(fragments, "fragments must not be null");Objects.requireNonNull(values, "values must not be null");int fragmentsSize = fragments.size();int valuesSize = values.size();if (fragmentsSize != valuesSize + 1) {throw new IllegalArgumentException("fragments must have one more element than values");}JavaTemplateAccess JTA = SharedSecrets.getJavaTemplateAccess();return JTA.interpolate(fragments, values);}public static JavaTemplateAccess getJavaTemplateAccess() {var access = javaTemplateAccess;if (access == null) {try {Class.forName("java.lang.runtime.TemplateSupport", true, null);access = javaTemplateAccess;} catch (ClassNotFoundException e) {}}return access;}final class TemplateSupport implements JavaTemplateAccess {
...@Overridepublic String interpolate(List<String> fragments, List<?> values) {int fragmentsSize = fragments.size();int valuesSize = values.size();if (fragmentsSize == 1) {return fragments.get(0);}int size = fragmentsSize + valuesSize;String[] strings = new String[size];int i = 0, j = 0;for (; j < valuesSize; j++) {strings[i++] = fragments.get(j);strings[i++] = String.valueOf(values.get(j));}strings[i] = fragments.get(j);return JLA.join("", "", "", strings, size);}

其他使用示例,在STR中可以进行基本的运算(支持三元运算)

int x=10,y=20;
String result=STR."\{x} + \{y} = \{x+y}";
System.out.println(result);//10 + 20 = 30

调用方法

String res=STR."获取一个随机数:\{Math.random()}";
System.out.println(res);

获取属性

String res1=STR."int的最大值是:\{Integer.MAX_VALUE}";
System.out.println(res1);

查看时间

String res2=STR."现在时间:\{new SimpleDateFormat("yyyy-MM-dd").format(new Date()) }";
System.out.println(res2);

计数操作

int index=0;
String result=STR."\{index++},\{index++},\{index++}";
System.out.println(result);

获取数组数据

String[] cars ={"bmw","ben","audi"};
String result = STR. "\{ cars[0] },\{ cars[1] },\{ cars[2] }" ;
System.out.println(result);

拼接多行数据

String[] cars ={"bmw","ben","audi"};
String result = STR. """\{cars[0] }\{ cars[1] }\{ cars[2] }""" ;
System.out.println(result);

自定义模版

    public static void main(String[] args) {var INTER = StringTemplate.Processor.of((StringTemplate st) -> {StringBuilder sb = new StringBuilder();Iterator<String> iterator = st.fragments().iterator();for (Object value : st.values()) {sb.append(iterator.next());sb.append(value);}sb.append(iterator.next());return sb.toString();});int x = 10, y = 20;String result = INTER. "\{ x } + \{ y } = \{ x + y }" ;System.out.println(result);}

2、scoped values

scoped values是一个隐藏的方法参数,只有方法可以访问scoped values,它可以让两个方法之间传递参数时无需声明形参。例如在UserDao类中编写savaUser方法,LogDao类中编写了saveLog方法,那么在保存用户的时候需要保证事务,此时就需要在service层获取Connection对象,然后将该对象分别传入到两个Dao的方法中,但对于savaUser方法来说并不是直接使用Connection对象,却又不得不在方法的形参中写上该对象,其实仅从业务上来看,该方法中只要传入User对象就可以了。

public class ScopeedValueTest {private static final ScopedValue<String> GIFT=ScopedValue.newInstance();public static void main(String[] args) {ScopeedValueTest scopeedValueTest=new ScopeedValueTest();scopeedValueTest.giveGift();}public void giveGift(){ScopedValue.where(GIFT,"手机").run(()->recieveGift());}public void recieveGift(){System.out.println(GIFT.get());}
}

多线程环境是否会出问题

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ScopeedValueTest {private static final ScopedValue<String> GIFT = ScopedValue.newInstance();public static void main(String[] args) {ScopeedValueTest t = new ScopeedValueTest();ExecutorService pool = Executors.newCachedThreadPool();for (int i = 0; i < 10; i++) {pool.submit(() -> {t.giveGift();});}pool.shutdown();}public void giveGift() {ScopedValue.where(GIFT, Thread.currentThread().getName()).run(() -> recieveGift());}public void recieveGift() {System.out.println(GIFT.get());}
}

变更中间数值,和当前线程绑定关系

public class ScopeedValueTest {private static final ScopedValue<String> GIFT = ScopedValue.newInstance();public static void main(String[] args) {ScopeedValueTest t = new ScopeedValueTest();t.giveGift();}public void giveGift() {ScopedValue.where(GIFT, "500").run(() -> recieveMiddleMan());}//中间人public void recieveMiddleMan() {System.out.println(GIFT.get());//500ScopedValue.where(GIFT, "200").run(() -> recieveGift());//中间人抽成300System.out.println("recieveMiddleMan=" + GIFT.get());//500}public void recieveGift() {System.out.println(GIFT.get());//200}
}

3、record pattern

通过该特性可以解构record类型中的值,例如:

public class RecordTest {public static void main(String[] args) {Student s = new Student(1, "小米");print(s);}static void print(Object obj) {if (obj instanceof Student(int a, String b)) {System.out.println("a=" + a);System.out.println("b=" + b);}}
}record Student(int id, String name) {}

4、switch格式匹配

public class SwitchTest {public static void main(String[] args) {String str="hello";String result = getObjInstance(str);System.out.println(result);}public static String getObjInstance(Object obj) {return switch (obj) {case null -> "空对象";case Integer i -> "Integer对象" + i;case String s -> "String对象" + s;default -> obj.toString();};}
}

5、可以在switch中使用when

public class Switch02Test {public static void main(String[] args) {yesOrNo("yes");}public static void yesOrNo(String str) {switch (str) {case null -> {System.out.println("空对象");}case String swhen s.equalsIgnoreCase("yes") -> {System.out.println("确定");}case String swhen s.equalsIgnoreCase("no") -> {System.out.println("取消");}case String s -> {System.out.println("请输入yes或no");}}}
}

6、Unnamed Classes and Instance Main Methods

对于初学者来说,写第一个HelloWorld代码有太多的概念,为了方便初学者快速编写第一段java代码,这里提出了无名类和实例main方法,下面代码可以直接运行编译,相当于是少了类的定义,main方法的修饰符和形参也省略掉了。

void main() {System.out.println("Hello,World!");
}

7、Structured Concurrency

结构化并发:该特性主要作用是在使用虚拟线程时,可以使任务和子任务的代码编写起来可读性更强,维护性更高,更加可靠。

import java.util.concurrent.ExecutionException;
import java.util.concurrent.StructuredTaskScope;
import java.util.function.Supplier;public class Test {public static void main(String[] args) {Food f = new Test().handle();System.out.println(f);}Food handle() {try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {Supplier<String> fork1 = scope.fork(() -> "奶茶做好了");//任务1:线程1Supplier<String> fork2 = scope.fork(() -> "烤串烤好了");//任务2:线程2scope.join().throwIfFailed();//失败传播//当两个任务都成功后,最终才能吃饭return new Food(fork1.get(), fork2.get());} catch (ExecutionException e) {throw new RuntimeException(e);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}record Food(String f1, String f2) {}

Java17-20新特性

一个程序员最重要的能力是:写出高质量的代码!!
有道无术,术尚可求也,有术无道,止于术。
无论你是年轻还是年长,所有程序员都需要记住:时刻努力学习新技术,否则就会被时代抛弃!

http://www.mmbaike.com/news/87382.html

相关文章:

  • metinfo怎么做网站怎么自己弄一个网站
  • 河北沧州网站建设百度指数查询手机版app
  • 西安网站建设中企建站广东疫情最新数据
  • dedecms美食网站石家庄网站建设排名
  • 一键生成作文的网站关键词排名查询网站
  • 网站开发需要什么条件深圳百度网站排名优化
  • 门户网站建设 工具爱战网关键词查询网站
  • 阜宁做网站价格谷歌浏览器官网下载
  • flash 如何做游戏下载网站搜索引擎排名优化包括哪些方面
  • 专业足球网站建设大数据培训机构排名前十
  • 网站如何做点击链接厦门百度广告
  • 镇江外贸型网站建设哪些网站可以免费推广
  • 成人编程培训班关键词优化排名软件
  • wordpress缩略图延时加载seo算法
  • 南山区做网站精准客户数据采集软件
  • 网站建设外包怎么样企业网站排名优化公司
  • 杭州网站建设企业站长工具收录
  • 免注册个人网站制作武汉网站建设
  • 做网站应该画什么图网站优化公司大家好
  • 设计签名网站优化包括哪些
  • 阿里云做网站的代码广州建网站的公司
  • 那个网站攻略做的好好用的视频播放器app
  • 制作网站的页面设计怎么做万网域名注册官网
  • 网站建设一个月做十单软文写手
  • 平面设计师长逛的网站有哪些百度网盘app下载安装 官方下载
  • 可以绑定域名的免费网站好的在线crm系统
  • 哪种编程语言可以做网站网上营销方法
  • 做旅游项目用哪家网站好北京seo学校
  • 做网站服务器需要系统怎么开自己的网站
  • 欧派网站谁做的app引导页模板html