JAVA_1

一、JAVA8种数据类型

1.byte 1字节
2.short 2字节
3.int 4字节
4.long 8字节
5.float 4字节
6.double 8字节
7.char 2字节
8.boolean true和false

import java.util.Scanner;

public class test1_data_type {
    public static void main(String[] args) {
        byte age = 18;
        short salary = 15000;
        int salary2 = 50000;
        long population = 7000000000L;

        int x1 = 65; // 十进制
        int x2 = 065; // 八进制
        int x3 = 0x65; // 十六进制
        int x4 = 0b01000001; // 二进制
        System.out.println(x1);
        System.out.println(x2);
        System.out.println(x3);
        System.out.println(x4);

        float f1 = 3.14F; //浮点常量默认double,改成float加F
        double d1 = 3.14;
        double d2 = 314e-2;
        System.out.println(d2);
        //浮点数是不精确的,要精确运算使用BigDecimal类
        char c1 = 'A';
        char c2 = '高';
        System.out.print(c1);
        System.out.println(c2);

        //字符串不是基本数据类型,是类!
        String str = "HelloWorld";
        System.out.println(str);

        boolean flag = true;
    }
}



二、JAVA数据类型自动和强制转换

1.自动类型转换(无数据丢失,从左至右)
************int-char不能超出范围
byte-short-int-long
************int-float-double-int(从int指过来)
2.可能精度损失的:int-float long-float long-double
3.强制类型转换,超过表示范围会被截断

import java.util.Scanner;

public class test1_data_type {
    public static void main(String[] args) {
        double d3 = 3.14;
        int d4 = (int) d1;
        char m1 = 'c';
        int m2 = m1 + 2;
        char m3 = (char) m2;//e

        //键盘输入
        Scanner scanner = new Scanner(System.in);
        String str2 = scanner.nextLine();
        System.out.println(str2);
    }
}

 

三、JAVA运算符

算术运算符
±*/% ++ –
扩展运算符
+= -= *= /= %=
关系运算符
== != > <
逻辑运算符 对真值进行操作
& | ! && || ^
位运算符 对数字进行二进制操作
~取反 & | ^ << >>

public class test2_operator {
    public static void main(String[] args) {
        //  字符串运算符
        int a = 3;
        System.out.println("a = " + a);

        //  条件运算符
        int b = 2;
        b = a > b ? a : b;
        System.out.println(b);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值