typeswitch.java
来自「JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程」· Java 代码 · 共 92 行
JAVA
92 行
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 + =
减小字号Ctrl + -
显示快捷键?