Java 数据类型

数据类型在java 语言中包括两种:

  第一种:基本数据类型,可以划分成4大类8小种

      整数型:byte,short,int,long byte:1 个字节,short :2 个字节  ;int :4个字节 ;long :8个字节 

      浮点型 :float,double  float 4 个字节,double 8 个字节

      布尔型:boolean  占用 1 个字节   

      字符型:char,占用 2 个字节

  第二种:引用数据类型 ,字符串属于引用数据类型

  ps: 一个字节 8个二进制位,bigdecimal(取值范围比较大,财务)

4个类型的取值范围

  byte: [-128 ~127]

  short:  [-32768 ~ 32767]

  int    :  [-2147483648 ~ 2147483647]

  char :  [0 ~ 65535]

字符编码:人为规定的一套转换表,规定了一系列的文字对应的二进制

使用 \ 转义: \n 转行  \t 制表符  \' 单引号

\u 代表后面的是一个字符的 unicode 编码 , unicode 编码 是 16 位。 char x =   '\u4e2d'   代表 中

整数型:

  八进制 :  010 ,0开头    十六进制:0x10      0x开头

数据类型转换: 

  从小容量 到 大容量 可以 自动转换 

    在任何情况下,整数型的“字面量/数据”默认被当做int类型处理。(记住就行)
    如果希望该“整数型字面量”被当做long类型来处理,需要在“字面量”后面添加L/l

      // 错误: 整数太大
      //long e = 2147483648;

      // 怎么解决这个问题呢?
      long e = 2147483648L;

  从大容量 到小容量必须强制转换,但是可能会损失精度。

    当一个整数型的字面量没有超出byte,short,char的取值范围,可以将该字面量直接赋值给byte,short,char类型的变量,如果超出范围则需要添加强制类型转换符。

      byte  i = 1 ;

    转换的时候,去扣除前面的字节

           byte b = (byte)300;
      System.out.println(b); // 44

 1 public class TypeTransferTest{
 2     public static void main(String[] args){
 3         // 编译报错,因为1000已经超出范围了。
 4         //byte b1 = 1000;
 5         // 可以
 6         byte b2 = 20;
 7         // 可以
 8         short s = 1000;
 9         // 可以
10         int c = 1000;
11         // 可以
12         long d = c;
13         // 编译报错
14         //int e = d;
15         // 可以
16         int f = 10 / 3;
17         // 可以
18         long g = 10;
19         // 编译报错
20         //int h = g / 3;
21         // 可以
22         long m = g / 3;
23         // 编译报错
24         //byte x = (byte)g / 3;
25         // 可以
26         short y = (short)(g / 3);
27         // 可以
28         short i = 10;
29         // 可以
30         byte j = 5;
31         // 编译报错
32         //short k = i + j;
33         // 可以
34         int n = i + j;
35         // 可以
36         char cc = 'a';
37         System.out.println(cc); // a
38         System.out.println((byte)cc); // 97
39         // cc 会先自动转换成int类型,再做运算
40         int o = cc + 100;
41         System.out.println(o); // 197
42     }
43 }
View Code

 

整数类型

  1、当一个整数赋值给 char 类型变量的时候,会转换自动转换成字符型

  2、当一个整数没有超出byte,short,char取值范围,可以直接赋值

byte ,short ,char  的混合运算

  结论:byte、char、short做混合运算的时候,各自先转换成int再做运算。

  编译器只能检测到语法,不确定具体的存储的什么值,因为如果查过范围,是需要强制转换的

    short i = 10;
    byte j = 5;
    // 编译报错
    //short k = i + j;

    short k = (short)(i+j);

 1 public class IntTest06{
 2     public static void main(String[] args){
 3 
 4         char c1 = 'a';
 5         byte b = 1;
 6 
 7         // 注意:这里的"+"是负责求和的
 8         System.out.println(c1 + b); // 98
 9 
10         // 错误: 不兼容的类型: 从int转换到short可能会有损失
11         //short s = c1 + b; // 编译器不知道这个加法最后的结果是多少。只知道是int类型。
12 
13         // 这样修改行吗?
14         //错误: 不兼容的类型: 从int转换到short可能会有损失
15         //short s = (short)c1 + b;
16 
17         short s = (short)(c1 + b);
18 
19         //short k = 98;
20 
21         int a = 1;
22         //错误: 不兼容的类型: 从int转换到short可能会有损失
23         // short x = 1; 可以
24         short x = a; // 不可以,编译器只知道a是int类型,不知道a中存储的是哪个值。
25         System.out.println(x);
26     }
27 }
View Code

 

多种数据类型混合运算,最终结果是最大容量的对应的类型 

  char+short+byte 这个除外。
  因为char + short + byte混合运算的时候,会各自先转换成int再做运算。

浮点型

  任何一个浮点型数据默认是当做 double 来处理,如果想用 float 类型, 添加 F或者f. 或者强制转换

boolean 

  true  flase

 

short s1 = 1; s1 = s1 + 1;有什么错?
s1是short类型,1是int类型,short和int混合运算的时候short会自动转换为int类型,所以s1 + 1编译器检测出是int类型,int类型无法赋值给short类型的变量s1。这样修改:s1 = (short)(s1 + 1);

 

八种基本数据类型中,除boolean类型不能转换,剩下七种类型之间都可以进行转换;
 如果整数型字面量没有超出byte,short,char的取值范围,可以直接将其赋值给byte,short,char类型的变量;
 小容量向大容量转换称为自动类型转换,容量从小到大的排序为:byte < short(char) < int < long < float < double,其中short和char都占用两个字节,但是char可以表示更大的正整数;
 大容量转换成小容量,称为强制类型转换,编写时必须添加“强制类型转换符”,但运行时可能出现精度损失,谨慎使用;
 byte,short,char类型混合运算时,先各自转换成int类型再做运算;
 多种数据类型混合运算,各自先转换成容量最大的那一种再做运算;

posted @ 2020-08-27 23:30  一叶扁舟,乘风破浪  阅读(214)  评论(0编辑  收藏  举报