java day01记录

详细记录见本地基础培训资料

一、数据类型

/*
    数据类型:Java是一种强类型语言,针对每一种数据都给出了明确的数据类型。
    
    数据类型分类:
        A:基本数据类型
        B:引用数据类型(类,接口,数组)
        
    基本数据类型:4类8种
        A:整數            占用字节数
            byte        1
            short       2
            int         4
            long        8
        B:浮点数
            float       4    
            double      8
        C:字符
            char        2
        D:布尔
            boolean     1
            
    注意:
        A:整数默认是int类型,浮点数默认是double类型
        B:定义long类型数据的时候,要加L或者l,建议加L
          定义float类型数据的时候,要加F或者f,建议加F
*/

二、类型转换
• + 是一个运算符, 我们应该能够看懂, 做数据的加法。

• boolean类型不能转换为其他的数据类型(true,false)


• 默认转换
byte,short,char —> int —> long —> float —> double

   分别占用字节1,2,2 ---> 4--->8---4---8

– byte,short,char相互之间不转换, 他们参与运算首先转换为int类型

• 强制转换
– 目标类型 变量名=(目标类型)(被转换的数据);

/*
+:这是一个运算符,用于做加法运算的。
    我们在做运算的时候,一般要求参与运算的数据的类型必须一致。

    类型转换:
        隐式转换
        强制转换

    隐式转换:
        byte,short,char -- int -- long -- float -- double
*/
public class TypeCastDemo {
    public static void main(String[] args){
        //定义两个int变量
        int a = 3;
        int b = 4;
        int c = a+b;
        System.out.println(c);

        //定义一个byte类型变量和一个int类型变量
        byte aa = 2;
        int bb = 3;
        System.out.println(aa + bb);

//        byte oo = aa + bb;
        int ee = aa + bb;
        System.out.println(ee);
//        System.out.println(oo);
    }
}
/*
    强制转换:
        目标类型 变量名 = (目标类型) (被转换的数据);
        建议:数据做运算,结果应该是什么类型,就用什么类型接收,不要随意转换类型,否则会有精度的损失。
*/
public class TypeCastDemo2 {
    public static void main(String[] args){
        int a = 2;
        byte b = 3;
        int c = a + b;
//        byte e = a + b; 会损失精度
        System.out.println(c);

        byte d = (byte)(a + b);
        System.out.println(d);
    }
}

 https://www.cnblogs.com/kuangwong/p/6198862.html

String 转int

1 int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String],[int radix]);
2 int i = Integer.valueOf(my_str).intValue();

原文地址:https://www.cnblogs.com/longesang/p/10637710.html