八大类型及拓展

八大数据类型及拓展

 

整数型

 

byte: 占用 一个字节: 取值范围(-128~127)

short: 占用2个字节: 取值范围(-32768~32767)

int : 占用4个字节

long : 占用8个字节: 对long 型变量赋值要在其后边加 L或l :

 

浮点数

 

float: 占用4个字节: 对float型赋值要在其后边加 F 或f

double: 占用8个字节: 对于double 型赋值

 

字符型

 

char : 占用2个字节 对char 型赋值 需加双引号

 

布尔值型

 

boolean 占用一个字节: 只有两种值 (true 或false)

 

public class 八大类型 {
   public static void main(String[] args) {
       //八大基本数据类型


       //整数
       int num1 =10;
       long nunm3 =10000L;   //long类型需要L锁定


       //浮点小数
       float num4 =3.14F;  //float后需要F锁定
       double num5 =3.1415926;


       //字符类型

       char name ='国';  //string不是关键字,是类


       //布尔值
       boolean problem1 = false;
       boolean problem2 = true;
       System.out.println("wulixcc");
  }
}

 

 

 

 

拓展

 点击查询转义字符表

public class 数据拓展 {
   public static void main(String[] args){

       //整数进制 二进制0b 十进制 八进制0 十六进制0x

       int s1=10;
       int s2=010;     //八进制
       int s3=0x10;    //十进制
       int s4=011;

       System.out.println(s1);
       System.out.println(s2);
       System.out.println(s3);
       System.out.println(s4);


       System.out.println("======================");

       //浮点数拓展   最好不要用浮点数比较

       float b=0.1f;
       double c=1.0/10;

       System.out.println(b==c); //此处==表示判断,结果显示false

       float d=21212121121f;
       float f=d+1;

       System.out.println(d==f);  //结果显示正确

       //原因:浮点数 有限 离散 舍入误差 大约 接近但不等于
       //原因:浮点数 有限 离散 舍入误差 大约 接近但不等于
       //原因:浮点数 有限 离散 舍入误差 大约 接近但不等于

       //银行等比较业务bigDecimal类进行表示

       System.out.println("======================");

       //字符拓展 强制转换

       char c1 ='中';
       char c2 ='a';

       System.out.println(c1);
       System.out.println((int)c1);  //强制转换
       System.out.println(c2);
       System.out.println((int)c2);

       //编码 Unicode表(97=a 65=A) 表示2字节 字的本质是字符

       System.out.println("======================");

       //转义字符 \t (tab作用) \n(换行)

       System.out.println("Hello\tWorld");
       System.out.println("Hello\nWorld"); //换行

       System.out.println("======================");

       //思考:对象 内存分析

       String sa ="Hello Wrold";
       String sb ="Hello Wrold";

       System.out.println(sa==sb);  //正确

       String sc= new String("Hello World");
       String sd= new String("Hello World");

       System.out.println(sc==sd); //错误

       System.out.println("======================");

       //布尔值拓展
       // boolean flag = ture;   if(flag){} = if(flag=ture){} 代码要精简易读
  }

}

输出值

10
8
16
9
======================
false
true
======================

20013
a
97
======================
Hello World
Hello
World
======================
true
false
======================

进程已结束,退出代码为 0

 

posted @ 2021-03-08 18:54  小莀学编程  阅读(177)  评论(0)    收藏  举报