关键字: java 面试题 基本类型 int long boolean float double char
public class PrimitiveTypeTest { public static void main(String[] args) { // byte System.out.println("基本类型:byte 二进制位数:" + Byte.SIZE); System.out.println("包装类:java.lang.Byte"); System.out.println("最小值:Byte.MIN_VALUE=" + Byte.MIN_VALUE); System.out.println("最大值:Byte.MAX_VALUE=" + Byte.MAX_VALUE); System.out.println(); // short System.out.println("基本类型:short 二进制位数:" + Short.SIZE); System.out.println("包装类:java.lang.Short"); System.out.println("最小值:Short.MIN_VALUE=" + Short.MIN_VALUE); System.out.println("最大值:Short.MAX_VALUE=" + Short.MAX_VALUE); System.out.println(); // int System.out.println("基本类型:int 二进制位数:" + Integer.SIZE); System.out.println("包装类:java.lang.Integer"); System.out.println("最小值:Integer.MIN_VALUE=" + Integer.MIN_VALUE); System.out.println("最大值:Integer.MAX_VALUE=" + Integer.MAX_VALUE); System.out.println(); // long System.out.println("基本类型:long 二进制位数:" + Long.SIZE); System.out.println("包装类:java.lang.Long"); System.out.println("最小值:Long.MIN_VALUE=" + Long.MIN_VALUE); System.out.println("最大值:Long.MAX_VALUE=" + Long.MAX_VALUE); System.out.println(); // float System.out.println("基本类型:float 二进制位数:" + Float.SIZE); System.out.println("包装类:java.lang.Float"); System.out.println("最小值:Float.MIN_VALUE=" + Float.MIN_VALUE); System.out.println("最大值:Float.MAX_VALUE=" + Float.MAX_VALUE); System.out.println(); // double System.out.println("基本类型:double 二进制位数:" + Double.SIZE); System.out.println("包装类:java.lang.Double"); System.out.println("最小值:Double.MIN_VALUE=" + Double.MIN_VALUE); System.out.println("最大值:Double.MAX_VALUE=" + Double.MAX_VALUE); System.out.println(); // char System.out.println("基本类型:char 二进制位数:" + Character.SIZE); System.out.println("包装类:java.lang.Character"); // 以数值形式而不是字符形式将Character.MIN_VALUE输出到控制台 System.out.println("最小值:Character.MIN_VALUE=" + (int) Character.MIN_VALUE); // 以数值形式而不是字符形式将Character.MAX_VALUE输出到控制台 System.out.println("最大值:Character.MAX_VALUE=" + (int) Character.MAX_VALUE); } }
public class PrimitiveTypeTest { public static void main(String[] args) { // 给byte类型变量赋值时,数字后无需后缀标识 byte byte_a = 1; // 编译器会做范围检查,如果赋予的值超出了范围就会报错 // byte byte_b = 1000; // 把一个long型值赋值给byte型变量,编译时会报错,即使这个值没有超出byte类型的取值范围 // byte byte_c = 1L; // 给short类型变量赋值时,数字后无需后缀标识 short short_a = 1; // 编译器会做范围检查,如果赋予的值超出了范围就会报错 // short short_b = 70000; // 把一个long型值赋值给short型变量,编译时会报错,即使这个值没有超出short类型的取值范围 // byte short_c = 1L; // 给short类型变量赋值时,数字后无需后缀标识 int int_a = 1; // 编译器会做范围检查,如果赋予的值超出了范围就会报错 // int int_b = 2200000000; // 把一个long型值赋值给int型变量,编译时会报错,即使这个值没有超出int类型的取值范围 // int int_c = 1L; // 可以把一个int型值直接赋值给long型变量,数字后无需后缀标识 long long_a = 1; // 如果给long型变量赋予的值超出了int型值的范围,数字后必须加L(不区分大小写)标识 long long_b = 2200000000L; // 编译器会做范围检查,如果赋予的值超出了范围就会报错 // long long_c = 9300000000000000000L; // 可以把一个int型值直接赋值给float型变量 float float_a = 1; // 可以把一个long型值直接赋值给float型变量 float float_b = 1L; // 没有F(不区分大小写)后缀标识的浮点数默认为double型的,不能将它直接赋值给float型变量 // float float_c = 1.0; // float型数值需要有一个F(不区分大小写)后缀标识 float float_d = 1.0F; // 把一个double型值赋值给float型变量,编译时会报错,即使这个值没有超出float类型的取值范围 // float float_e = 1.0D; // 编译器会做范围检查,如果赋予的值超出了范围就会报错 // float float_f = 3.5000000E38F; // 可以把一个int型值直接赋值给double型变量 double double_a = 1; // 可以把一个long型值直接赋值给double型变量 double double_b = 1L; // 可以把一个float型值直接赋值给double型变量 double double_c = 1F; // 不带后缀标识的浮点数默认为double类型的,可以直接赋值 double double_d = 1.0; // 也可以给数字增加一个D(不区分大小写)后缀标识,明确标出它是double类型的 double double_e = 1.0D; // 编译器会做范围检查,如果赋予的值超出了范围就会报错 // double double_f = 1.8000000000000000E308D; // 把一个double型值赋值给一个byte类型变量,编译时会报错,即使这个值没有超出byte类型的取值范围 // byte byte_d = 1.0D; // 把一个double型值赋值给一个short类型变量,编译时会报错,即使这个值没有超出short类型的取值范围 // short short_d = 1.0D; // 把一个double型值赋值给一个int类型变量,编译时会报错,即使这个值没有超出int类型的取值范围 // int int_d = 1.0D; // 把一个double型值赋值给一个long类型变量,编译时会报错,即使这个值没有超出long类型的取值范围 // long long_d = 1.0D; // 可以用字符初始化一个char型变量 char char_a = 'a'; // 也可以用一个int型数值初始化char型变量 char char_b = 1; // 把一个long型值赋值给一个char类型变量,编译时会报错,即使这个值没有超出char类型的取值范围 // char char_c = 1L; // 把一个float型值赋值给一个char类型变量,编译时会报错,即使这个值没有超出char类型的取值范围 // char char_d = 1.0F; // 把一个double型值赋值给一个char类型变量,编译时会报错,即使这个值没有超出char类型的取值范围 // char char_e = 1.0D; // 编译器会做范围检查,如果赋予的值超出了范围就会报错 // char char_f = 70000; } }
public class PrimitiveTypeTest { public static void main(String[] args) { int a = 123456; short b = (short) a; // b的值会是什么呢? System.out.println(b); } }
public class PrimitiveTypeTest { public static void main(String[] args) { short s1 = 1; // 这一行代码会报编译错误 // s1 = s1 + 1; // 这一行代码没有报错 s1 = 1 + 1; // 这一行代码也没有报错 s1 += 1; } }
public class EqualsTest { public static void main(String[] args) { // int类型用int类型初始化 int int_int = 0; // int类型用Integer类型初始化 int int_Integer = new Integer(0); // Integer类型用Integer类型初始化 Integer Integer_Integer = new Integer(0); // Integer类型用int类型初始化 Integer Integer_int = 0; System.out.println("int_int == int_Integer结果是:" + (int_int == int_Integer)); System.out.println("Integer_Integer == Integer_int结果是:" + (Integer_Integer == Integer_int)); System.out.println(); System.out.println("int_int == Integer_Integer结果是:" + (int_int == Integer_Integer)); System.out.println("Integer_Integer == int_int结果是:" + (Integer_Integer == int_int)); System.out.println(); // boolean类型用boolean类型初始化 boolean boolean_boolean = true; // boolean类型用Boolean类型初始化 boolean boolean_Boolean = new Boolean(true); // Boolean类型用Boolean类型初始化 Boolean Boolean_Boolean = new Boolean(true); // Boolean类型用boolean类型初始化 Boolean Boolean_boolean = true; System.out.println("boolean_boolean == boolean_Boolean结果是:" + (boolean_boolean == boolean_Boolean)); System.out.println("Boolean_Boolean == Boolean_boolean结果是:" + (Boolean_Boolean == Boolean_boolean)); System.out.println(); System.out.println("boolean_boolean == Boolean_Boolean结果是:" + (boolean_boolean == Boolean_Boolean)); System.out.println("Boolean_Boolean == boolean_boolean结果是:" + (Boolean_Boolean == boolean_boolean)); } }
public static int round(float a) { //other code } public static long round(double a) { //other code }
public class MathTest { public static void main(String[] args) { System.out.println("小数点后第一位=5"); System.out.println("正数:Math.round(11.5)=" + Math.round(11.5)); System.out.println("负数:Math.round(-11.5)=" + Math.round(-11.5)); System.out.println(); System.out.println("小数点后第一位<5"); System.out.println("正数:Math.round(11.46)=" + Math.round(11.46)); System.out.println("负数:Math.round(-11.46)=" + Math.round(-11.46)); System.out.println(); System.out.println("小数点后第一位>5"); System.out.println("正数:Math.round(11.68)=" + Math.round(11.68)); System.out.println("负数:Math.round(-11.68)=" + Math.round(-11.68)); } }
public static int round(float a) { return (int)floor(a + 0.5f); } public static long round(double a) { return (long)floor(a + 0.5d); }
评论