Fork me on GitHub

数据类型

强类型语言:要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用

弱类型语言:要求变量的使用要符合规定,所有变量都必须先定义后才能使用

Java的数据类型分为两大类

 

 


八大基本类型,公司笔试经常考


基本类型

byte 1字节

short 2字节

int 4字节

long 8字节 long a = 20L (数字后面加L)

float 4字节 float b = 20.1F (数据后面加F)

double 8字节

char 2字节

public class Demo02 {
   public static void main(String[] args) {
       String a = "hello";
       int num = 10;
       System.out.println(a);
       System.out.println(num);

       //八大数据类型

       //整数
       int num1 = 10; // 最常用
       byte num2 = 20;
       short num3 = 30;
       long num4 = 40L;//long类型要在数字后面加个L

       //小数:浮点数
       float num5 = 50.1F;//float类型要在数字后面加个F
       double num6 = 50.1;

       //字符
       char name = '国';
       //字符串,String不是关键字,是类
       //String namea = “赵”;

       //布尔值:是非
       boolean flag = true;
       boolean flag2 = false;

  }
}

 

引用类型

除了基本类型之外的类型都是引用类型

接口

数组

面试题拓展

public class Demo03{
   public static void main(String[] args) {
       // 整数拓展:   进制   二进制0b 十进制   八进制0   十六进制0x

       int i = 10;
       int i2 = 010;
       int i3 = 0x10;

       System.out.println(i);
       System.out.println(i2);
       System.out.println(i3);
       System.out.println("================================");
       //=============================================
       //浮点数扩展?   银行业务怎么表示钱?
       // BigDecimal   数学工具类
       //=============================================

       //float   有限   离散   舍入误差   大约   接近但不等于
       //double
       //最好避免完全使用浮点数进行比较
       //最好避免完全使用浮点数进行比较
       //最好避免完全使用浮点数进行比较

       float f = 0.1f;
       double d = 1.0/10;

       System.out.println(f==d);//flase

       float d1 = 313213135135f;
       float d2 = d1 + 1;

       System.out.println(d1==d2);//true

       //================================================
       //字符拓展
       //================================================

       System.out.println("================================================");
       char c1 = 'a';
       char c2 = '中';

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

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

       //所有的字符本质还是数字
       //编码 Unicode 表:(97 = a   65 = A )2字节   0 - 65536 Excel

       // U0000   UFFFF

       char c3 = '\u0061';

       System.out.println(c3);//a

       //转义字符
       // \t 制表符
       // \n 换行
       // ........

       System.out.println("Hello\nWorld");

       System.out.println("================================================");
       String sa = new String("hello world");
       String sb = new String("hello world");
       System.out.println(sa == sb);//flase

       String sc = "hello world";
       String sd = "hello world";
       System.out.println(sc == sd);//true
       //对象 从内存分析

       //布尔值拓展
       boolean flag = true;
       if(flag==true){}//新手
       if (flag){}//老手
       //上面两个判断句的意思都是判断flag==true
       //Less is more! 代码要精简易读
  }
}
 
posted @ 2022-06-01 13:38  944964684  阅读(33)  评论(0)    收藏  举报
1