JAVA基础【3.3】《Java核心技术1》Java的基本程序设计结构-数据类型

来源:互联网 发布:成都室内设计公司 知乎 编辑:程序博客网 时间:2024/05/16 09:45

3.3.1 整型     3.3.2 浮点类型     3.3.3 char类型     3.3.4 boolean类型

      Java是一种强类型语言,必须为每个变量声明其类型。在Java中,一共有8种基本类型,其中4种整型,2种浮点类型,1种表示Unicode编码的字符单元的字符类型char,1种用于表示真假值得boolean类型。
      1    数据类型
Java数据类型(type)可以分为两大类:基本类型(primitive types)和引用类型(reference types)primitive types 包括boolean类型以及数值类型(numeric types)。numeric types又分为整型(integer types)和浮点型(floating-point type)。整型有5种:byte short int long char(char本质上是一种特殊的int)。浮点类型有float和double。关系整理一下如下图:

   存储需求取值范围默认值

numeric


numericbyte1字节

[-128, 127]

(byte)0short2字节[-32768, 32767] int4字节[-2147483648, 2147483647] long8字节[-9223372036854775808, 9223372036854775807] char2字节

[0, 65535]

'\u0000'floating-pointfloat4字节  double8字节   boolean  falsereference  class type    interface type    array type    null type   

    2    数据类型转换
byte->short,char -> int -> long     
float -> double
int -> float
long -> double

byte <(short=char)< int < long < float < double
如果从小转换到大,可以自动完成,而从大到小,必须强制转换。short和char两种相同类型也必须强制转换。
  1. int a = 1;
  2. long c = 0;
  3. long b = 0;
  4. //小转大
  5. b = a;
  6. //大转小必须强制转
  7. a = (int)c;

 


 

0 0