JAVA八种基本类型

1.Java八种基本类型分类

*Java的八种数据类型:
 *      1.整型
 *          1.1 byte  1字节
 *          1.2 int   2字节
 *          1.3 short 4字节
 *          1.4 long  8字节
 *      2.浮点型(小数型)
 *          2.1 float 4字节   精度为7*          2.2 double 8字节  精度为15*          2.3 注意:Java中的小数默认为double*                   如果定义为 float f = 2.34 会标红报错
 *                   修改为  float f = 2.34f就行或者进行强制转换 float f = (float)2.34
 *      3.布尔型(boolean)
 *          只能取值truefalse
 *      4.字符型(char)  2字节  可以存放一个汉字

2.byte型

package com.zelin.test;
import java.util.Scanner;
public class Test01 {
    public static void main(String[] args){
        //1-基本类型
        //1.1 byte类型
        useByte();    
    }
    private static void useByte() {
        //2-Byte类型 1个字节 8位
        //2.1)获取byte类型最大值与最小值
        byte maxValue = Byte.MAX_VALUE;
        byte minValue = Byte.MIN_VALUE;
        System.out.println("maxValue = "+maxValue);
        System.out.println("minValue = "+minValue);
        //2.2) 装箱与拆箱
        byte b = 1;
        Byte b1 = b;
        byte b2 = b1;
        System.out.println("b = "+ b);
        System.out.println("b1 = "+ b1);
        System.out.println("b2 = "+ b2);
        //2.3)字符串转换为byte类型的三种方法
        String str = "121";
        Byte b3 = new Byte(str);       //方法一
        byte b4 = Byte.parseByte(str); //方法二
        Byte b5 = Byte.valueOf(str);   //方法三
        System.out.println("b3 = "+(b3+1));
        System.out.println("b4 = "+(b4+1));
        System.out.println("b5 = "+(b5+1));
    }
}

3.short型

package com.zelin.test;
import java.util.Scanner;
public class Test01 {
    public static void main(String[] args){
        //1-基本类型
        //1.2 short 类型
        useShort();   
    }
    private static void useShort() {
        //3.short类型 2个字节  16位
        //3.1)获取short类型最大值与最小值
        short maxValue = Short.MAX_VALUE;
        short minValue = Short.MIN_VALUE;
        System.out.println("maxValue = "+maxValue);
        System.out.println("minValue = "+minValue);
        //3.2)装箱与拆箱
        short s1 = 12348;
        Short s2 = s1;
        short s3 = s2;
        System.out.println("s1 = " + (s1+1));
        System.out.println("s2 = " + (s2+1));
        System.out.println("s3= " + (s3+1));
        //3.3)字符串转换为byte类型的三种方法
        String str = "12345";
        short s4 = new Short(str);               //方法一
        Short s5 = Short.parseShort(str);        //方法二
        Short s6 = Short.valueOf(str);           //方法三
        System.out.println("s4 = " + (s4+1));
        System.out.println("s5 = " + (s5+1));
        System.out.println("s6 = " + (s6+1));
    }
 }

4.int类型

package com.zelin.test;
import java.util.Scanner;
public class Test01 {
    public static void main(String[] args){
        //1-基本类型
        //1.3 int 类型
        useInt();   
    }
    private static void useInt() {
        //4.int类型 4个字节  32位
        //4.1) int中的最大值与最小值
        int maxValue = Integer.MAX_VALUE;
        int minValue = Integer.MIN_VALUE;
        System.out.println("maxValue = "+maxValue);
        System.out.println("minValue = "+minValue);
        //4.2) 装箱与拆箱
        int i = 189;
        Integer j = i;
        int k = j;
        System.out.println("i = " + (i+22));
        System.out.println("j = " + (j+23));
        System.out.println("k = " + (k+24));
        //4.3) 字符串转Int类型的三种方法
        String str = "123";
        int d1 = Integer.parseInt(str);     //方法一
        int d2 = Integer.parseInt(str);     //方法二
        Integer d3 = Integer.valueOf(str);  //方法三
        System.out.println("d1 = " + (d1+1));
        System.out.println("d2 = " + (d2+1));
        System.out.println("d3 = " + (d3+10));
    }
}

5.long

package com.zelin.test;
import java.util.Scanner;
public class Test01 {
    public static void main(String[] args){
        //1-基本类型
       //1.4 long类型
        useLong();  
    }
    private static void useLong() {
        //5-long类型 8个字节 64位
        //5.1)获取long类型最大值与最小值
        long maxValue = Long.MAX_VALUE;
        long minValue = Long.MIN_VALUE;
        System.out.println("maxValue = "+maxValue);
        System.out.println("minValue = "+minValue);
        //5.2) 装箱与拆箱
        long  b = 1899999;
        Long b1 = b;
        long  b2 = b1;
        System.out.println("b = "+ b);
        System.out.println("b1 = "+ b1);
        System.out.println("b2 = "+ b2);
        //5.3)字符串转换为Long类型的三种方法
        String str = "12166666666";
        Long b3 = new Long(str);        //方法一
        Long b4 = Long.parseLong(str);  //方法二
        Long b5 = Long.valueOf(str);    //方法三
        System.out.println("b3 = "+(b3+1));
        System.out.println("b4 = "+(b4+1));
        System.out.println("b5 = "+(b5+1));
    }
}

6.float

package com.zelin.test;
import java.util.Scanner;
public class Test01 {
    public static void main(String[] args){
        //1-基本类型
       //1.5 float类型
        useFloat();
    }
    private static void useFloat() {
        //6-float类型 4个字节 32位
        //6.1)获取float类型最大值与最小值
        float maxValue = Float.MAX_VALUE;
        float minValue = Float.MIN_VALUE;
        System.out.println("maxValue = "+maxValue);
        System.out.println("minValue = "+minValue);
        //6.2) 装箱与拆箱
        float  f = 2.36f;
        Float f1 = f;
        float  f2 = f1;
        System.out.println("f = "+ (f+2.35f));
        System.out.println("f1 = "+ (f1+2.35f));
        System.out.println("f2 = "+ (f2+2.35f));
        //6.3)字符串转换为float类型的三种方法
        String str = "2.36";
        float f3 = new Float(str);        //方法一
        float f4 = Float.parseFloat(str); //方法二
        float f5 = Float.valueOf(str);    //方法三
        System.out.println("f3 = "+(f3+1f));
        System.out.println("f4 = "+(f4+1f));
        System.out.println("f5 = "+(f5+1f));
    }
}

7.double

package com.zelin.test;
import java.util.Scanner;
public class Test01 {
    public static void main(String[] args){
        //1-基本类型
        //1.6 double类型
        useDouble();    
    }
    private static void useDouble() {
        //7-double类型 8个字节 64位
        //7.1)获取float类型最大值与最小值
        double maxValue = Double.MAX_VALUE;
        double minValue = Double.MIN_VALUE;
        System.out.println("maxValue = "+maxValue);
        System.out.println("minValue = "+minValue);
        //7.2) 装箱与拆箱
        double  f = 2.35;
        Double f1 = f;
        double  f2 = f1;
        System.out.println("f = "+ (f+2.35));
        System.out.println("f1 = "+ (f1+2.35));
        System.out.println("f2 = "+ (f2+2.35));
        //7.3)字符串转换为double类型的三种方法
        String str = "2.36";
        double f3 = new Float(str);        //方法一
        double f4 = Float.parseFloat(str); //方法二
        double f5 = Float.valueOf(str);    //方法三
        System.out.println("f3 = "+(f3+1));
        System.out.println("f4 = "+(f4+1));
        System.out.println("f5 = "+(f5+1));
    }
}

8.boolean

package com.zelin.test;
import java.util.Scanner;
public class Test01 {
    public static void main(String[] args){
        //1-基本类型
        //1.7 boolean类型
        useBoolean();    
    }
    private static void useBoolean() {
        //8-boolean类型 4个字节 32位
        //8.1) 装箱与拆箱
        boolean  b = true;
        Boolean b1 = b;
        boolean  b2 = b1;
        System.out.println("b = "+ b);
        System.out.println("b1 = "+ b1);
        System.out.println("b2 = "+ b2);
        //8.2)字符串转换为boolean类型的三种方法
        String str = "true0201";
        Boolean b3 = new Boolean(str);              //方法一
        Boolean b4 = Boolean.parseBoolean(str);    //方法二
        boolean b5 = Boolean.valueOf(str);         //方法三

        System.out.println("b3 = " + b3);
        System.out.println("b4 = " + b4);
        System.out.println("b5 = " + b5);
    }
}

9.char

package com.zelin.test;
import java.util.Scanner;
public class Test01 {
    public static void main(String[] args){
        //1-基本类型
        //1.8 char类型
        useChar();    
    }
    private static void useChar() {
        //9.char类型 2字节 16位
        //9.1)装箱与拆箱
        char ch = 'a';
        Character ch1 = ch;
        char ch2 = ch1;
        System.out.println("ch = " + ch);
        System.out.println("ch1 = " + ch1);
        System.out.println("ch2 = " + ch2);
        //9.2)字符串转换为字符类型
        String str = "hello,java!";
        char cc = str.charAt(0);
        System.out.println("cc = " + cc);
        //9.3)字符类型转换为字符串
        String st = cc+"";
        System.out.println("st = " + st);
        useSample01();

    }

    private static void useSample01() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个小写字符:");
        //1-首先获取字符串
        String  s1 =sc.next();
        //2-将字符串转化为字符
        char c = s1.charAt(0);
        c = (char)(c- 32);
        System.out.println("输出的大写字符为:" + c);



    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值