Java学习笔记----数据类型

 1.浮点数——默认数据类型为double型

public class TestDemo{
    public static void main(String[] args) {
        double num = 10.2;     //10.2是一个小数所以属于double型
        System.out.println(num);
        System.out.println(num * 2);
    }
}
10.2
20.4

2.浮点数使用float型

public class TestDemo{
    public static void main(String[] args) {
        float f1 = 10.2f;     //小数都是double型,需要强制转换成float型
        float f2 = (float)10.2;
        System.out.println(f1 * f2);
    }
}
104.03999

3.字符型

public class TestDemo{
    public static void main(String[] args) {
        // 整型和字符型的转化
        char c = 'A';
        int num = c;
        System.out.println(c);
        System.out.println(num);
    }
}

A
65

4.布尔型:保存TRUE和FALSE两类数据

public class TestDemo{
    public static void main(String[] args) {
        boolean sign = false;
        if(!sign)
        {
            System.out.println("Hello world!");
        }
    }
}
Hello world!

5.字符串类型----引用数据类型,属于一个类,在java中,类的每一个首字母都是大写

// 字符串的操作
public class TestDemo{
    public static void main(String[] args) {
        String str = "Hello world!";
        System.out.println(str);
        System.out.println("Hello world!");
    }
}
Hello world!
Hello world!
// 字符串的连接
public class TestDemo{
    public static void main(String[] args) {
        String str = "Hello world!";
        str = str + " Java is much easier to learn than C++";
        str += "!!!";
        System.out.println(str);
    }
}
Hello world! Java is much easier to learn than C++!!!

注:“+”在数学运算和字符串连接中需要考虑优先级问题

// “+”在字符串连接和数学运算中的优先级关系
public class TestDemo{
    public static void main(String[] args) {
        int numA = 100;
        double numB = 99.0;
        String str1 = "字符串连接优先: " + numA + numB;
        String str2 = "数学运算优先: " + (numA + numB);
        System.out.println(str1);
        System.out.println(str2);
    }
}
字符串连接优先: 10099.0
数学运算优先: 199.0

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值