Java 每天学习一点点之数据类型

    xiaoxiao2021-03-26  37

    特别声明:如下内容均来自于不同网络课程视频或书籍,整理合并完成。 如果有整理错误,忘及时指出,共同进步,不胜感激。

    Java 入门之数据类型

    Java 数据类型分为两种

    1. 基本数据类型

    数值型(两种类型):

      整数类型:byte,short,int,long (从小到大顺序排列)

      取值范围:

         -byte:

    public static void main(String[] args) { byte x = Byte.MIN_VALUE; byte y = Byte.MAX_VALUE; System.out.println("The range of byte type between "+x+" and "+ y); Output: The range of byte type between -128 and 127 }

         - short:

    public static void main(String[] args) { short x = Short.MAX_VALUE; short y = Short.MIN_VALUE; System.out.println("The range of short type between "+x+" and "+ y); output: The range of short type between -32768 and 32767 }

         - int:

    public static void main(String[] args) { int x = Integer.MAX_VALUE; int y = Integer.MIN_VALUE; System.out.println("The range of int type between "+x+" and "+ y); output: The range of int type between -2147483648 and 2147483647 }

         - long:

    public static void main(String[] args) { long x = Long.MAX_VALUE; long y = Long.MIN_VALUE; System.out.println("The range of long type between "+x+" and "+ y); out put: The range of long type between -9223372036854775808 and 9223372036854775807 }

      浮点类型:float,double (从小到大顺序排列)   取值范围:

         -float:

    public static void main(String[] args) { float x = Float.MAX_VALUE; float y = Float.MIN_VALUE; System.out.println("The range of float type between "+x+" and "+y); output:The range of float type between -3.4028235E38 and 1.4E-45 }

         -double:

    public static void main(String[] args) { double x = Double.MAX_VALUE; double y = Double.MIN_VALUE; System.out.println("The range of double type between "+x+" and "+y); output: The range of double type between -1.7976931348623157E308 and 4.9E-324 }

    字符型:char 给字符型变量赋值时,我们使用单引号,而非双引号,否则出现错误。 例如:

    public static void main(String[] args) { char x = 'a'; System.out.println("The value of a is "+x); output: The value of x is a } public static void main(String[] args) { char x = "b"; System.out.println("The value of a is "+b); output: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Type mismatch: cannot convert from String to char b cannot be resolved to a variable. }

    布尔型:boolean 该类型赋值结果只有 TRUE 和 False 两种。 例如:

    public static void main(String[] args) { boolean x = true; boolean y = false; System.out.println(x+" and "+y); output: true and false }

      特别注意:

       1. 如何正确选取合理的数据类型?

       a). 符合实际情况     例如:统计记录学生身高时,我们应该选择float 类型,即小数类型,而非整数类型。

       b). 事先考虑需要赋值的大小作用范围     例如:计算某地区人口数量,假设选择 short 类型,则造成数据溢出导致错误

    public static void main(String[] args) { short x = 20000000; System.out.println(x); output: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from int to short }

       2. 数据精度问题?     例如:当把小数3.14 赋值给 int 型变量时,我们将损失精度,因为 int 为整数型,只会记录3损失小数部分。 若将低精度数值例如整数4 赋值给     double,则不会造成精度损失。

       3. float 赋值问题?     例如:float赋值时需要在结尾加上“f” 以表示该类型为float,否则系统将会默认为double 类型。

       4. 不同数据类型之间的计算问题?     例如:int 型的数值10除以double 型数值2.5,则结果为4.0 与double 类型一致。

       5. 在输出语句进行计算问题?     例如:

    public static void main(String[] args) { int x = 10; int y =20; System.out.println("This is wrong: "+x+y); output: 1020 System.out.println("This is correct"+(x+y)); output:30 }

        我们可以看见第一条输出结果被转化成了字符串string格式,因此当需要在输出语句中进行计算式,务必加上括号。

       6. 数据可以进行强制转换     例如:

    public static void main(String[] args) { int x = 10; float y = (float) x; System.out.println(y); output: 10.0 }

    2. 引用数据类型

    类: 接口: 数组:

    未完待续。。。 更新时间 每周一,三,五,日。

    转载请注明原文地址: https://ju.6miu.com/read-662833.html

    最新回复(0)