⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typeswitch.java

📁 JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程
💻 JAVA
字号:
package ch01.section01;

public class TypeSwitch {
  public TypeSwitch() {
  }

  private static void charSwitch(char x) {

    int i = x;
    long l = x;
    float f = x;
    double d = x;
    if (x == i && x == l && x == f && x == d) {
      System.out.println("字符型(char)可以转换为整型(int)、长整型(long)、" +
                         "单精度型(float)和双精度型(double)");
    }
  }

  private static void byteSwitch(byte x) {

    short s = x;
    int i = x;
    long l = x;
    float f = x;
    double d = x;
    if (x == s && x == i && x == l && x == f && x == d) {
      System.out.println("字节型(byte)可以转换为短整型(short)、整型(int)、" +
                         "长整型(long)、单精度型(float)和双精度型(double)");
    }
  }

  private static void shortSwitch(short x) {

    int i = x;
    long l = x;
    float f = x;
    double d = x;
    if (x == i && x == l && x == f && x == d) {
      System.out.println("短整型(short)可以转换为整型(int)、" +
                         "长整型(long)、单精度型(float)和双精度型(double)");
    }
  }

  private static void intSwitch(int x) {
    long l = x;
    float f = x;
    double d = x;
    if (x == l && x == f && x == d) {
      System.out.println("整型(int)可以转换为长整型(long)、单精度型(float)和双精度型(double)");
    }
  }

  private static void longSwitch(long x) {

    float f = x;
    double d = x;
    if (x == f && x == d) {
      System.out.println("长整型(long)可以转换为单精度型(float)和双精度型(double)");
    }
  }

  private static void floatSwitch(float x) {

    double d = x;
    if (x == d) {
      System.out.println("单精度型(float)可以转换为双精度型(double)");
    }
  }

  public static void main(String[] args) {
    char char_x = 'F';
    byte byte_x = 1;
    short short_x = 045;
    int int_x = 78;
    long long_x = 4350L;
    float float_x = 2424.23F;
    double double_x = 230.56D;
    charSwitch(char_x);
    byteSwitch(byte_x);
    shortSwitch(short_x);
    intSwitch(int_x);
    longSwitch(long_x);
    floatSwitch(float_x);
    int_x = ~short_x;
    --short_x;
    double_x = short_x + double_x;
    int_x = short_x + byte_x;
    System.out.println(int_x);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -