news 2026/3/27 2:54:38

Java语言提供了八种基本类型。六种数字类型【函数三十二】

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Java语言提供了八种基本类型。六种数字类型【函数三十二】

变量就是申请内存来存储值。也就是说,当创建变量的时候,需要在内存中申请空间。

内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据。

因此,通过定义不同类型的变量,可以在内存中储存整数、小数或者字符。

Java 的两大数据类型:

  • 内置数据类型
  • 引用数据类型

内置数据类型

Java语言提供了八种基本类型。六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型。

byte:

  • byte 数据类型是8位、有符号的,以二进制补码表示的整数;
  • 最小值是 -128(-2^7);
  • 最大值是 127(2^7-1);
  • 默认值是 0;
  • byte 类型用在大型数组中节约空间,主要代替整数,因为 byte 变量占用的空间只有 int 类型的四分之一;
  • 例子:byte a = 100,byte b = -50。

short:

  • short 数据类型是 16 位、有符号的以二进制补码表示的整数
  • 最小值是 -32768(-2^15);
  • 最大值是 32767(2^15 - 1);
  • Short 数据类型也可以像 byte 那样节省空间。一个short变量是int型变量所占空间的二分之一;
  • 默认值是 0;
  • 例子:short s = 1000,short r = -20000。

int:

  • int 数据类型是32位、有符号的以二进制补码表示的整数;
  • 最小值是 -2,147,483,648(-2^31);
  • 最大值是 2,147,483,647(2^31 - 1);
  • 一般地整型变量默认为 int 类型;
  • 默认值是 0 ;
  • 例子:int a = 100000, int b = -200000。

long:

  • long 数据类型是 64 位、有符号的以二进制补码表示的整数;
  • 最小值是 -9,223,372,036,854,775,808(-2^63);
  • 最大值是 9,223,372,036,854,775,807(2^63 -1);
  • 这种类型主要使用在需要比较大整数的系统上;
  • 默认值是 0L;
  • 例子: long a = 100000L,long b = -200000L。
    "L"理论上不分大小写,但是若写成"l"容易与数字"1"混淆,不容易分辩。所以最好大写。

float:

  • float 数据类型是单精度、32位、符合IEEE 754标准的浮点数;
  • float 在储存大型浮点数组的时候可节省内存空间;
  • 默认值是 0.0f;
  • 浮点数不能用来表示精确的值,如货币;
  • 例子:float f1 = 234.5f。

double:

  • double 数据类型是双精度、64 位、符合 IEEE 754 标准的浮点数;
  • 浮点数的默认类型为 double 类型;
  • double类型同样不能表示精确的值,如货币;
  • 默认值是 0.0d;
  • 例子:

    double d1 = 7D ; double d2 = 7.; double d3 = 8.0; double d4 = 8.D; double d5 = 12.9867;

    7 是一个 int 字面量,而 7D,7. 和 8.0 是 double 字面量。

boolean:

  • boolean数据类型表示一位的信息;
  • 只有两个取值:true 和 false;
  • 这种类型只作为一种标志来记录 true/false 情况;
  • 默认值是 false;
  • 例子:boolean one = true。

char:

  • char 类型是一个单一的 16 位 Unicode 字符;
  • 最小值是 \u0000(十进制等效值为 0);
  • 最大值是 \uffff(即为 65535);
  • char 数据类型可以储存任何字符;
  • 例子:char letter = 'A';。

实例

对于数值类型的基本类型的取值范围,我们无需强制去记忆,因为它们的值都已经以常量的形式定义在对应的包装类中了。请看下面的例子:

实例

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); } }


运行实例 »

编译以上代码输出结果如下所示:

基本类型:byte 二进制位数:8 包装类:java.lang.Byte 最小值:Byte.MIN_VALUE=-128 最大值:Byte.MAX_VALUE=127 基本类型:short 二进制位数:16 包装类:java.lang.Short 最小值:Short.MIN_VALUE=-32768 最大值:Short.MAX_VALUE=32767 基本类型:int 二进制位数:32 包装类:java.lang.Integer 最小值:Integer.MIN_VALUE=-2147483648 最大值:Integer.MAX_VALUE=2147483647 基本类型:long 二进制位数:64 包装类:java.lang.Long 最小值:Long.MIN_VALUE=-9223372036854775808 最大值:Long.MAX_VALUE=9223372036854775807 基本类型:float 二进制位数:32 包装类:java.lang.Float 最小值:Float.MIN_VALUE=1.4E-45 最大值:Float.MAX_VALUE=3.4028235E38 基本类型:double 二进制位数:64 包装类:java.lang.Double 最小值:Double.MIN_VALUE=4.9E-324 最大值:Double.MAX_VALUE=1.7976931348623157E308 基本类型:char 二进制位数:16 包装类:java.lang.Character 最小值:Character.MIN_VALUE=0 最大值:Character.MAX_VALUE=65535

Float和Double的最小值和最大值都是以科学记数法的形式输出的,结尾的"E+数字"表示E之前的数字要乘以10的多少次方。比如3.14E3就是3.14 × 103 =3140,3.14E-3 就是 3.14 x 10-3 =0.00314。

https://avg.163.com/topic/detail/11075096
https://avg.163.com/topic/detail/11075095
https://avg.163.com/topic/detail/11075094
https://avg.163.com/topic/detail/11075092
https://avg.163.com/topic/detail/11075091
https://avg.163.com/topic/detail/11075090
https://avg.163.com/topic/detail/11075089
https://avg.163.com/topic/detail/11075088
https://avg.163.com/topic/detail/11075087
https://avg.163.com/topic/detail/11075085
https://avg.163.com/topic/detail/11075083
https://avg.163.com/topic/detail/11075082
https://avg.163.com/topic/detail/11075081
https://avg.163.com/topic/detail/11075079
https://avg.163.com/topic/detail/11075077
https://avg.163.com/topic/detail/11075075
https://avg.163.com/topic/detail/11075074
https://avg.163.com/topic/detail/11075073
https://avg.163.com/topic/detail/11075071
https://avg.163.com/topic/detail/11075070
https://avg.163.com/topic/detail/11075068
https://avg.163.com/topic/detail/11075067
https://avg.163.com/topic/detail/11075065
https://avg.163.com/topic/detail/11075064
https://avg.163.com/topic/detail/11075063
https://avg.163.com/topic/detail/11075062
https://avg.163.com/topic/detail/11075060
https://avg.163.com/topic/detail/11075058
https://avg.163.com/topic/detail/11075057
https://avg.163.com/topic/detail/11075056
https://avg.163.com/topic/detail/11075055
https://avg.163.com/topic/detail/11075054
https://avg.163.com/topic/detail/11075053
https://avg.163.com/topic/detail/11075051
https://avg.163.com/topic/detail/11075050
https://avg.163.com/topic/detail/11075049
https://avg.163.com/topic/detail/11075048
https://avg.163.com/topic/detail/11075047
https://avg.163.com/topic/detail/11075046
https://avg.163.com/topic/detail/11075044
https://avg.163.com/topic/detail/11075042
https://avg.163.com/topic/detail/11075041
https://avg.163.com/topic/detail/11075040
https://avg.163.com/topic/detail/11075039
https://avg.163.com/topic/detail/11075038
https://avg.163.com/topic/detail/11075037
https://avg.163.com/topic/detail/11075035
https://avg.163.com/topic/detail/11075034
https://avg.163.com/topic/detail/11075033
https://avg.163.com/topic/detail/11075032
https://avg.163.com/topic/detail/11075030
https://avg.163.com/topic/detail/11075031
https://avg.163.com/topic/detail/11075029
https://avg.163.com/topic/detail/11075028
https://avg.163.com/topic/detail/11075027
https://avg.163.com/topic/detail/11075026
https://avg.163.com/topic/detail/11075025
https://avg.163.com/topic/detail/11075023
https://avg.163.com/topic/detail/11075022
https://avg.163.com/topic/detail/11075021
https://avg.163.com/topic/detail/11075020
https://avg.163.com/topic/detail/11075019
https://avg.163.com/topic/detail/11075018
https://avg.163.com/topic/detail/11075017
https://avg.163.com/topic/detail/11075016
https://avg.163.com/topic/detail/11075014
https://avg.163.com/topic/detail/11075013
https://avg.163.com/topic/detail/11075012
https://avg.163.com/topic/detail/11075011
https://avg.163.com/topic/detail/11075010
https://avg.163.com/topic/detail/11075008
https://avg.163.com/topic/detail/11075006
https://avg.163.com/topic/detail/11075005
https://avg.163.com/topic/detail/11075004
https://avg.163.com/topic/detail/11075003
https://avg.163.com/topic/detail/11075002
https://avg.163.com/topic/detail/11075001
https://avg.163.com/topic/detail/11075000
https://avg.163.com/topic/detail/11074999
https://avg.163.com/topic/detail/11074998
https://avg.163.com/topic/detail/11074997
https://avg.163.com/topic/detail/11074996
https://avg.163.com/topic/detail/11074995
https://avg.163.com/topic/detail/11074993
https://avg.163.com/topic/detail/11074992
https://avg.163.com/topic/detail/11074991
https://avg.163.com/topic/detail/11074989
https://avg.163.com/topic/detail/11074988
https://avg.163.com/topic/detail/11074987
https://avg.163.com/topic/detail/11074985
https://avg.163.com/topic/detail/11074983
https://avg.163.com/topic/detail/11074984
https://avg.163.com/topic/detail/11074982
https://avg.163.com/topic/detail/11074980
https://avg.163.com/topic/detail/11074979
https://avg.163.com/topic/detail/11074978
https://avg.163.com/topic/detail/11074977
https://avg.163.com/topic/detail/11074976
https://avg.163.com/topic/detail/11074975
https://avg.163.com/topic/detail/11074974
https://avg.163.com/topic/detail/11074972
https://avg.163.com/topic/detail/11074971
https://avg.163.com/topic/detail/11074970
https://avg.163.com/topic/detail/11074969
https://avg.163.com/topic/detail/11074968
https://avg.163.com/topic/detail/11074967
https://avg.163.com/topic/detail/11074966
https://avg.163.com/topic/detail/11074963
https://avg.163.com/topic/detail/11074962
https://avg.163.com/topic/detail/11074961
https://avg.163.com/topic/detail/11074960
https://avg.163.com/topic/detail/11074959
https://avg.163.com/topic/detail/11074958
https://avg.163.com/topic/detail/11074957
https://avg.163.com/topic/detail/11074956
https://avg.163.com/topic/detail/11074955
https://avg.163.com/topic/detail/11074954
https://avg.163.com/topic/detail/11074951
https://avg.163.com/topic/detail/11074950
https://avg.163.com/topic/detail/11074949
https://avg.163.com/topic/detail/11074947
https://avg.163.com/topic/detail/11074945
https://avg.163.com/topic/detail/11074944
https://avg.163.com/topic/detail/11074943
https://avg.163.com/topic/detail/11074942
https://avg.163.com/topic/detail/11074941
https://avg.163.com/topic/detail/11074940
https://avg.163.com/topic/detail/11074939
https://avg.163.com/topic/detail/11074937
https://avg.163.com/topic/detail/11074936
https://avg.163.com/topic/detail/11074935
https://avg.163.com/topic/detail/11074934
https://avg.163.com/topic/detail/11074932
https://avg.163.com/topic/detail/11074931
https://avg.163.com/topic/detail/11074928
https://avg.163.com/topic/detail/11074926
https://avg.163.com/topic/detail/11074925
https://avg.163.com/topic/detail/11074924
https://avg.163.com/topic/detail/11074923
https://avg.163.com/topic/detail/11074922
https://avg.163.com/topic/detail/11074920
https://avg.163.com/topic/detail/11074919
https://avg.163.com/topic/detail/11074918
https://avg.163.com/topic/detail/11074916
https://avg.163.com/topic/detail/11074915
https://avg.163.com/topic/detail/11074914
https://avg.163.com/topic/detail/11074913
https://avg.163.com/topic/detail/11074912
https://avg.163.com/topic/detail/11074911
https://avg.163.com/topic/detail/11074909
https://avg.163.com/topic/detail/11074908
https://avg.163.com/topic/detail/11074906
https://avg.163.com/topic/detail/11074905
https://avg.163.com/topic/detail/11074904
https://avg.163.com/topic/detail/11074902
https://avg.163.com/topic/detail/11074900
https://avg.163.com/topic/detail/11074899
https://avg.163.com/topic/detail/11074898
https://avg.163.com/topic/detail/11074897
https://avg.163.com/topic/detail/11074896
https://avg.163.com/topic/detail/11074895
https://avg.163.com/topic/detail/11074894
https://avg.163.com/topic/detail/11074893
https://avg.163.com/topic/detail/11074892
https://avg.163.com/topic/detail/11074890
https://avg.163.com/topic/detail/11074889
https://avg.163.com/topic/detail/11074888
https://avg.163.com/topic/detail/11074887
https://avg.163.com/topic/detail/11074886
https://avg.163.com/topic/detail/11074884
https://avg.163.com/topic/detail/11074883
https://avg.163.com/topic/detail/11074881
https://avg.163.com/topic/detail/11074879
https://avg.163.com/topic/detail/11074878
https://avg.163.com/topic/detail/11074877
https://avg.163.com/topic/detail/11074876
https://avg.163.com/topic/detail/11074875
https://avg.163.com/topic/detail/11074874
https://avg.163.com/topic/detail/11074873
https://avg.163.com/topic/detail/11074871
https://avg.163.com/topic/detail/11074870
https://avg.163.com/topic/detail/11074868
https://avg.163.com/topic/detail/11074867
https://avg.163.com/topic/detail/11074865
https://avg.163.com/topic/detail/11074864
https://avg.163.com/topic/detail/11074863
https://avg.163.com/topic/detail/11074862
https://avg.163.com/topic/detail/11074861
https://avg.163.com/topic/detail/11074860
https://avg.163.com/topic/detail/11074859
https://avg.163.com/topic/detail/11074858
https://avg.163.com/topic/detail/11074857
https://avg.163.com/topic/detail/11074855
https://avg.163.com/topic/detail/11074853
https://avg.163.com/topic/detail/11074852
https://avg.163.com/topic/detail/11074851
https://avg.163.com/topic/detail/11074850
https://avg.163.com/topic/detail/11074849
https://avg.163.com/topic/detail/11074848
https://avg.163.com/topic/detail/11074847
https://avg.163.com/topic/detail/11074846
https://avg.163.com/topic/detail/11074845
https://avg.163.com/topic/detail/11074844
https://avg.163.com/topic/detail/11074843
https://avg.163.com/topic/detail/11074842
https://avg.163.com/topic/detail/11074840
https://avg.163.com/topic/detail/11074839
https://avg.163.com/topic/detail/11074837
https://avg.163.com/topic/detail/11074836
https://avg.163.com/topic/detail/11074834
https://avg.163.com/topic/detail/11074832
https://avg.163.com/topic/detail/11074831
https://avg.163.com/topic/detail/11074830
https://avg.163.com/topic/detail/11074828
https://avg.163.com/topic/detail/11074826
https://avg.163.com/topic/detail/11074824
https://avg.163.com/topic/detail/11074823
https://avg.163.com/topic/detail/11074821
https://avg.163.com/topic/detail/11074820
https://avg.163.com/topic/detail/11074819
https://avg.163.com/topic/detail/11074818
https://avg.163.com/topic/detail/11074817
https://avg.163.com/topic/detail/11074815
https://avg.163.com/topic/detail/11074814
https://avg.163.com/topic/detail/11074811
https://avg.163.com/topic/detail/11074810
https://avg.163.com/topic/detail/11074808
https://avg.163.com/topic/detail/11074806
https://avg.163.com/topic/detail/11074805
https://avg.163.com/topic/detail/11074804
https://avg.163.com/topic/detail/11074803
https://avg.163.com/topic/detail/11074802
https://avg.163.com/topic/detail/11074801
https://avg.163.com/topic/detail/11074799
https://avg.163.com/topic/detail/11074800
https://avg.163.com/topic/detail/11074798
https://avg.163.com/topic/detail/11074797
https://avg.163.com/topic/detail/11074795
https://avg.163.com/topic/detail/11074794
https://avg.163.com/topic/detail/11074791
https://avg.163.com/topic/detail/11074792
https://avg.163.com/topic/detail/11074790
https://avg.163.com/topic/detail/11074789
https://avg.163.com/topic/detail/11074788
https://avg.163.com/topic/detail/11074787
https://avg.163.com/topic/detail/11074786
https://avg.163.com/topic/detail/11074784
https://avg.163.com/topic/detail/11074785
https://avg.163.com/topic/detail/11074783
https://avg.163.com/topic/detail/11074781
https://avg.163.com/topic/detail/11074780
https://avg.163.com/topic/detail/11074778
https://avg.163.com/topic/detail/11074776
https://avg.163.com/topic/detail/11074775
https://avg.163.com/topic/detail/11074773
https://avg.163.com/topic/detail/11074771
https://avg.163.com/topic/detail/11074769
https://avg.163.com/topic/detail/11074765
https://avg.163.com/topic/detail/11074761
https://avg.163.com/topic/detail/11074759
https://avg.163.com/topic/detail/11074757
https://avg.163.com/topic/detail/11074747
https://avg.163.com/topic/detail/11074742
https://avg.163.com/topic/detail/11074738
https://avg.163.com/topic/detail/11074733
https://avg.163.com/topic/detail/11074729
https://avg.163.com/topic/detail/11074727
https://avg.163.com/topic/detail/11074723
https://avg.163.com/topic/detail/11074722
https://avg.163.com/topic/detail/11074720
https://avg.163.com/topic/detail/11074715
https://avg.163.com/topic/detail/11074712
https://avg.163.com/topic/detail/11074707
https://avg.163.com/topic/detail/11074697
https://avg.163.com/topic/detail/11074696
https://avg.163.com/topic/detail/11074695
https://avg.163.com/topic/detail/11074691
https://avg.163.com/topic/detail/11074690
https://avg.163.com/topic/detail/11074684
https://avg.163.com/topic/detail/11074679
https://avg.163.com/topic/detail/11074678
https://avg.163.com/topic/detail/11074676
https://avg.163.com/topic/detail/11074660
https://avg.163.com/topic/detail/11074658
https://avg.163.com/topic/detail/11074657
https://avg.163.com/topic/detail/11074656
https://avg.163.com/topic/detail/11074655
https://avg.163.com/topic/detail/11074654
https://avg.163.com/topic/detail/11074653
https://avg.163.com/topic/detail/11074648
https://avg.163.com/topic/detail/11074647
https://avg.163.com/topic/detail/11074644
https://avg.163.com/topic/detail/11074643
https://avg.163.com/topic/detail/11074640
https://avg.163.com/topic/detail/11074634
https://avg.163.com/topic/detail/11074632
https://avg.163.com/topic/detail/11074625
https://avg.163.com/topic/detail/11074623
https://avg.163.com/topic/detail/11074622
https://avg.163.com/topic/detail/11074619
https://avg.163.com/topic/detail/11074618
https://avg.163.com/topic/detail/11074613
https://avg.163.com/topic/detail/11074610
https://avg.163.com/topic/detail/11074608
https://avg.163.com/topic/detail/11074606
https://avg.163.com/topic/detail/11074598
https://avg.163.com/topic/detail/11074588
https://avg.163.com/topic/detail/11074587
https://avg.163.com/topic/detail/11074585
https://avg.163.com/topic/detail/11074582
https://avg.163.com/topic/detail/11074579
https://avg.163.com/topic/detail/11074577
https://avg.163.com/topic/detail/11074573
https://avg.163.com/topic/detail/11074568
https://avg.163.com/topic/detail/11074565
https://avg.163.com/topic/detail/11074563
https://avg.163.com/topic/detail/11074561
https://avg.163.com/topic/detail/11074543
https://avg.163.com/topic/detail/11074542
https://avg.163.com/topic/detail/11074541
https://avg.163.com/topic/detail/11074540
https://avg.163.com/topic/detail/11074539
https://avg.163.com/topic/detail/11074538
https://avg.163.com/topic/detail/11074537
https://avg.163.com/topic/detail/11074535
https://avg.163.com/topic/detail/11074534
https://avg.163.com/topic/detail/11074533
https://avg.163.com/topic/detail/11074531
https://avg.163.com/topic/detail/11074530
https://avg.163.com/topic/detail/11074528
https://avg.163.com/topic/detail/11074526
https://avg.163.com/topic/detail/11074524
https://avg.163.com/topic/detail/11074523
https://avg.163.com/topic/detail/11074521
https://avg.163.com/topic/detail/11074520
https://avg.163.com/topic/detail/11074518
https://avg.163.com/topic/detail/11074517
https://avg.163.com/topic/detail/11074516
https://avg.163.com/topic/detail/11074514
https://avg.163.com/topic/detail/11074509
https://avg.163.com/topic/detail/11074507
https://avg.163.com/topic/detail/11074506
https://avg.163.com/topic/detail/11074504
https://avg.163.com/topic/detail/11074503
https://avg.163.com/topic/detail/11074502
https://avg.163.com/topic/detail/11074498
https://avg.163.com/topic/detail/11074490
https://avg.163.com/topic/detail/11074488
https://avg.163.com/topic/detail/11074487
https://avg.163.com/topic/detail/11074486
https://avg.163.com/topic/detail/11074485
https://avg.163.com/topic/detail/11074481
https://avg.163.com/topic/detail/11074461
https://avg.163.com/topic/detail/11074460
https://avg.163.com/topic/detail/11074459
https://avg.163.com/topic/detail/11074457
https://avg.163.com/topic/detail/11074440
https://avg.163.com/topic/detail/11074264
https://avg.163.com/topic/detail/11074419
https://avg.163.com/topic/detail/11074407
https://avg.163.com/topic/detail/11074405
https://avg.163.com/topic/detail/11074404
https://avg.163.com/topic/detail/11074403
https://avg.163.com/topic/detail/11074400
https://avg.163.com/topic/detail/11074397
https://avg.163.com/topic/detail/11074339
https://avg.163.com/topic/detail/11074336
https://avg.163.com/topic/detail/11074330
https://avg.163.com/topic/detail/11074295
https://avg.163.com/topic/detail/11074290
https://avg.163.com/topic/detail/11074274
https://avg.163.com/topic/detail/11074264
https://avg.163.com/topic/detail/11074265
https://avg.163.com/topic/detail/11074248
https://avg.163.com/topic/detail/11074251
https://avg.163.com/topic/detail/11074223
https://avg.163.com/topic/detail/11074161
https://avg.163.com/topic/detail/11074156
https://avg.163.com/topic/detail/11074104
https://avg.163.com/topic/detail/11074096
https://avg.163.com/topic/detail/11074077
https://avg.163.com/topic/detail/11074065
https://avg.163.com/topic/detail/11073655
https://avg.163.com/topic/detail/11074005
https://avg.163.com/topic/detail/11073970
https://avg.163.com/topic/detail/11073961
https://avg.163.com/topic/detail/11073956
https://avg.163.com/topic/detail/11073954
https://avg.163.com/topic/detail/11073949
https://avg.163.com/topic/detail/11073780
https://avg.163.com/topic/detail/11073895
https://avg.163.com/topic/detail/11073538
https://avg.163.com/topic/detail/11073867
https://avg.163.com/topic/detail/11073551
https://avg.163.com/topic/detail/11073815
https://avg.163.com/topic/detail/11073813
https://avg.163.com/topic/detail/11073801
https://avg.163.com/topic/detail/11073787
https://avg.163.com/topic/detail/11073780
https://avg.163.com/topic/detail/11073732
https://avg.163.com/topic/detail/11073730
https://avg.163.com/topic/detail/11073719
https://avg.163.com/topic/detail/11073713
https://avg.163.com/topic/detail/11073701
https://avg.163.com/topic/detail/11073675
https://avg.163.com/topic/detail/11073655
https://avg.163.com/topic/detail/11073642
https://avg.163.com/topic/detail/11073625
https://avg.163.com/topic/detail/11073587
https://avg.163.com/topic/detail/11073558
https://avg.163.com/topic/detail/11073557
https://avg.163.com/topic/detail/11073551
https://avg.163.com/topic/detail/11073549
https://avg.163.com/topic/detail/11073538
https://avg.163.com/topic/detail/11073513
https://avg.163.com/topic/detail/11073505
https://avg.163.com/topic/detail/11073474
https://avg.163.com/topic/detail/11073472
https://avg.163.com/topic/detail/11073467
https://avg.163.com/topic/detail/11072848
https://avg.163.com/topic/detail/11073386
https://avg.163.com/topic/detail/11073345
https://avg.163.com/topic/detail/11073304
https://avg.163.com/topic/detail/11073301

实际上,JAVA中还存在另外一种基本类型 void,它也有对应的包装类 java.lang.Void,不过我们无法直接对它们进行操作。

类型默认值

下表列出了 Java 各个类型的默认值:

数据类型默认值
int0
long0L
short0
char'\u0000'
byte0
float0.0f
double0.0d
booleanfalse
引用类型(类、接口、数组)null

说明:

  • int,short,long,byte的默认值是0。
  • char的默认值是\u0000(空字符)。
  • float的默认值是0.0f
  • double的默认值是0.0d
  • boolean的默认值是false
  • 引用类型(类、接口、数组)的默认值是null

实例

public class Test { static boolean bool; static byte by; static char ch; static double d; static float f; static int i; static long l; static short sh; static String str; public static void main(String[] args) { System.out.println("Bool :" + bool); System.out.println("Byte :" + by); System.out.println("Character:" + ch); System.out.println("Double :" + d); System.out.println("Float :" + f); System.out.println("Integer :" + i); System.out.println("Long :" + l); System.out.println("Short :" + sh); System.out.println("String :" + str); } }

实例输出结果为:

Bool :false Byte :0 Character: Double :0.0 Float :0.0 Integer :0 Long :0 Short :0 String :null

引用类型

  • 在Java中,引用类型的变量非常类似于C/C++的指针。引用类型指向一个对象,指向对象的变量是引用变量。这些变量在声明时被指定为一个特定的类型,比如 Employee、Puppy 等。变量一旦声明后,类型就不能被改变了。
  • 对象、数组都是引用数据类型。
  • 所有引用类型的默认值都是null。
  • 一个引用变量可以用来引用任何与之兼容的类型。
  • 例子:Site site = new Site("Runoob")。

Java 常量

常量在程序运行时是不能被修改的。

在 Java 中使用 final 关键字来修饰常量,声明方式和变量类似:

final double PI = 3.1415927;

虽然常量名也可以用小写,但为了便于识别,通常使用大写字母表示常量。

字面量可以赋给任何内置类型的变量。例如:

byte a = 68; char a = 'A'

byte、int、long、和short都可以用十进制、16进制以及8进制的方式来表示。

当使用字面量的时候,前缀 0 表示 8 进制,而前缀 0x 代表 16 进制, 例如:

int decimal = 100; int octal = 0144; int hexa = 0x64;

和其他语言一样,Java的字符串常量也是包含在两个引号之间的字符序列。下面是字符串型字面量的例子:

"Hello World" "two\nlines" "\"This is in quotes\""

字符串常量和字符变量都可以包含任何 Unicode 字符。例如:

char a = '\u0001'; String a = "\u0001";

Java语言支持一些特殊的转义字符序列。

符号字符含义
\n换行 (0x0a)
\r回车 (0x0d)
\f换页符(0x0c)
\b退格 (0x08)
\0空字符 (0x0)
\s空格 (0x20)
\t制表符
\"双引号
\'单引号
\\反斜杠
\ddd八进制字符 (ddd)
\uxxxx16进制Unicode字符 (xxxx)

自动类型转换

整型、实型(常量)、字符型数据可以混合运算。运算中,不同类型的数据先转化为同一类型,然后进行运算。

转换从低级到高级。

低 ------------------------------------> 高 byte,short,char—> int —> long—> float —> double

数据类型转换必须满足如下规则:

  • 1. 不能对boolean类型进行类型转换。

  • 2. 不能把对象类型转换成不相关类的对象。

  • 3. 在把容量大的类型转换为容量小的类型时必须使用强制类型转换。

  • 4. 转换过程中可能导致溢出或损失精度,例如:

    int i =128; byte b = (byte)i;

    因为 byte 类型是 8 位,最大值为127,所以当 int 强制转换为 byte 类型时,值 128 时候就会导致溢出。

  • 5. 浮点数到整数的转换是通过舍弃小数得到,而不是四舍五入,例如:

    (int)23.7 == 23; (int)-45.89f == -45

自动类型转换

必须满足转换前的数据类型的位数要低于转换后的数据类型,例如: short数据类型的位数为16位,就可以自动转换位数为32的int类型,同样float数据类型的位数为32,可以自动转换为64位的double类型。

实例

public class ZiDongLeiZhuan{ public static void main(String[] args){ char c1='a';//定义一个char类型 int i1 = c1;//char自动类型转换为int System.out.println("char自动类型转换为int后的值等于"+i1); char c2 = 'A';//定义一个char类型 int i2 = c2+1;//char 类型和 int 类型计算 System.out.println("char类型和int计算后的值等于"+i2); } }

运行结果为:

char自动类型转换为int后的值等于97 char类型和int计算后的值等于66

解析:c1 的值为字符a,查 ASCII 码表可知对应的 int 类型值为 97, A 对应值为 65,所以 i2=65+1=66。

强制类型转换

  • 1. 条件是转换的数据类型必须是兼容的。

  • 2. 格式:(type)value type是要强制类型转换后的数据类型 实例:

    实例

    public class ForceTransform { public static void main(String[] args){ int i1 = 123; byte b = (byte)i1;//强制类型转换为byte System.out.println("int强制类型转换为byte后的值等于"+b); } }

    运行结果:

    int强制类型转换为byte后的值等于123

隐含强制类型转换

  • 1、 整数的默认类型是 int。

  • 2、 小数默认是 double 类型浮点型,在定义 float 类型时必须在数字后面跟上 F 或者 f。

这一节讲解了 Java 的基本数据类型。下一节将探讨不同的变量类型以及它们的用法。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/24 1:24:34

[硬核运营] 告别手动搬运!浅析如何用 Python+RPA 打造“1688 批量采集 -> 智能清洗 -> 自动上架”的无人值守流水线

1688采集 跨境电商RPA 自动上架 数据清洗 ETL技术 铺货模式 自动化工具前言在跨境电商(TikTok Shop, Temu, Amazon, Shopify)的“唯快不破”时代,铺货速度 往往决定了你能否抢到新品的第一波红利。绝大多数卖家的供应链源头都在 1688。但是&a…

作者头像 李华
网站建设 2026/3/23 0:08:22

CANN Runtime:AI 处理器的运行核心与计算编排中枢

CANN 组织链接: https://atomgit.com/cann runtime 仓库链接: https://atomgit.com/cann/runtime 在异构计算架构中,硬件的强大性能需要高效的软件来激活和管理。对于 AI 处理器而言,CANN Runtime 正是扮演着这一关键角色。作为 C…

作者头像 李华
网站建设 2026/3/24 3:41:18

实操教程:c盘分区小了怎么扩大?分享3种分区扩容方法

看着任务栏里那个刺眼的C盘红色警告条,系统频繁弹出的“磁盘空间不足”提示,这不仅会影响电脑的运行速度,甚至可能导致软件无法安装或系统崩溃。c盘分区小了怎么扩大?针对这个问题,本文会分享多种适合不同水平用户的解…

作者头像 李华
网站建设 2026/3/25 15:49:12

从零开始构建多智能体系统:7种核心架构模式详解,建议收藏!

“单体智能体”(指只靠一个大语言模型,再塞一堆系统提示词)的路子走不长远。 我们很快就意识到,要搭建高效的系统,得用多个 “专精型智能体”。它们要能协作,还能自主组织。 为实现这一点,AI …

作者头像 李华
网站建设 2026/3/22 4:50:29

AI驱动人才管理系统的分布式架构设计:架构师的考虑

AI驱动人才管理系统的分布式架构设计:架构师的考虑 1. 引入与连接 1.1 引人入胜的开场 想象一下,在一个大型跨国企业中,每天都有成千上万份简历涌入,人力资源部门需要从这些海量信息中筛选出符合岗位要求的潜在人才。传统的人才管理方式犹如在茫茫大海中捞针,效率低下且…

作者头像 李华