失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > java 编译开关_Java开关盒字符串

java 编译开关_Java开关盒字符串

时间:2022-09-30 12:51:10

相关推荐

java 编译开关_Java开关盒字符串

java 编译开关

Today we will look into Java Switch Case String Example. Being a java programmer, I know the importance of String and how many times it’s used for conditional flow. Whether you have a simple method that behaves differently for different input String or a Servlet controller class to check the incoming action and process it accordingly, we use String and compare it to determine the flow.

今天,我们将研究Java Switch Case字符串示例。 作为一名Java程序员,我知道String的重要性以及它用于条件流的次数。 无论您有一个简单的方法针对不同的输入String表现不同,还是有一个Servlet控制器类来检查传入的操作并进行相应的处理,我们都使用String进行比较以确定流程。

Java开关盒 (Java Switch Case)

Java switch case is a neat way to code for conditional flow, just like if-else conditions. Before Java 7, the only means to achieve string based conditional flow was using if-else conditions. But Java 7 has improved the switch case to support String also.

Java切换案例是一种用于条件流编码的简洁方法,就像if-else条件一样。 在Java 7之前,实现基于字符串的条件流的唯一方法是使用if-else条件。 但是Java 7也改进了开关盒以支持String。

Java开关案例字符串示例 (Java switch case String Example)

Here I am providing a java program that shows the use of String in java switch case statements. For comparison, I am also providing another method which does the same conditional flow using if-else conditions.

在这里,我提供了一个Java程序,该程序显示了在Java切换案例中使用String的情况。 为了进行比较,我还提供了另一种使用if-else条件执行相同条件流的方法。

SwitchStringExample.java

SwitchStringExample.java

package com.journaldev.util;public class SwitchStringExample {public static void main(String[] args) {printColorUsingSwitch("red");printColorUsingIf("red");// switch case string is case sensitiveprintColorUsingSwitch("RED");printColorUsingSwitch(null);}private static void printColorUsingIf(String color) {if (color.equals("blue")) {System.out.println("BLUE");} else if (color.equals("red")) {System.out.println("RED");} else {System.out.println("INVALID COLOR CODE");}}private static void printColorUsingSwitch(String color) {switch (color) {case "blue":System.out.println("BLUE");break;case "red":System.out.println("RED");break;default:System.out.println("INVALID COLOR CODE");}}}

Here is the output of the above program.

这是上面程序的输出。

REDREDINVALID COLOR CODEException in thread "main"java.lang.NullPointerExceptionat com.journaldev.util.SwitchStringExample.printColorUsingSwitch(SwitchStringExample.java:24)at com.journaldev.util.SwitchStringExample.main(SwitchStringExample.java:10)

Keys points to know for java switch case String are:

Java切换案例字符串要了解的关键点是:

Java switch case String make code more readable by removing the multiple if-else-if chained conditions.Java转换大小写String通过删除多个if-else-if链接条件,使代码更具可读性。 Java switch case String is case sensitive, the output of example confirms it.Java开关大小写String区分大小写,示例的输出确认它。 Java Switch case uses String.equals() method to compare the passed value with case values, so make sure to add a NULL check to avoid NullPointerException.Java Switch case使用String.equals()方法将传递的值与case值进行比较,因此请确保添加NULL检查以避免NullPointerException。 According to Java 7 documentation for Strings in Switch, java compiler generates more efficient byte code for String in Switch statement than chained if-else-if statements.根据Java 7 Switch中的Strings文档,与链接的if-else-if语句相比,java编译器为Switch语句中的String生成更有效的字节代码。 Make sure to use java switch case String only when you know that it will be used with Java 7 else it will throw Exception.确保仅在知道它将与Java 7一起使用时,才使用Java开关大小写字符串,否则它将抛出Exception。

Thats all for Java switch case String example.

多数民众赞成在Java切换案例的字符串示例。

Tip: We can usejava ternary operatorrather than switch to write smaller code.

提示:我们可以使用Java三元运算符而不是切换来编写较小的代码。

翻译自: /588/java-switch-case-string

java 编译开关

如果觉得《java 编译开关_Java开关盒字符串》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。