数据类型

96 阅读1分钟

数据类型:

  • 数据类型是非常重要的概念,作用:告知计算机如何存放数据;分配多大的空间

  • 分两类:

    1、基本数据类型(固定-4类8种)

    2、引用数据类型(目前只需要知道:String -- 字符串)

1、整型

byte short int long

1 2 4 8

常用的是int和long,int是默认的

public class TestDataType {

public static void main(String[] args) {

System.out.println(1);//int

System.out.println(1L);//long -- L大小写均可

2、浮点类型

float double

4 8

默认是double

System.out.println(3.14);//double

System.out.println(3.14f);//float - f大小写均可

3、字符型

char

2

每个字符都必须用单引号,里面只能有一个字符;

但是"转义字符"例外:

'\n' -- 换行

'\r' -- 回车

'\t' -- 水平制表

'' -- 代表 \

System.out.println('A');//用单引号括起来

// System.out.println('AB');--报错

System.out.println('中');

System.out.println(' ');//空格字符

System.out.println('\n');

System.out.println('\');

4.布尔类型

boolean

空间大小不确定,由JVM根据实际情况划分

只有两个值:true false

System.out.println(true);

System.out.println(false);

5.额外的常用非基本数据类型 --- 字符串

String

  • "Hello"
  • "我饿了"
  • "1+3"
  • "1 + 3 = \n 4"

System.out.println("1 + 3 = \n 4");

数据类型转换

数据类型大小顺序: byte < short = char < int < long < float < double 

\

布尔类型不参与转换

2-1、小值 放入 大变量 -- 自动类型转换 d = 0.2f; d = i; i = c; 2-2、

强制类型转换 语法: (被转换类型)数据值     强转后果是精度丢失,注意:不是四舍五入