Java中的基本数据类型——练习代码

JAVA中定义了3类8种数据类型
1、基本数据类型

分类数据类型
数值型byte、short、int、long、float、double
字符型char
布尔型boolean
数据类型byteshortintlongfloatdoublecharboolean
所占字节大小12484821/8

补充:boolean类型在数组中占用8字节.
2、引用数据类型
类(class)、接口( interface)、数组(array)

基本数据初始化eg:

package com.xiafly;				//包

public class Tesst {
    public static void main(String[] args) {
        int x = 9;           //int型变量声明并初始化
        char y = 'A';
        boolean flag = true; //声明Boolean型变量并赋值
    }
}

int类型eg:

class TestInt {
    public static void main(String[] args) {
        byte a = 100;
        int b = 1001;
        int c = 056;      //八进制
        int d = 0x65;   //十六进制
        int e = 0b10101001;     //二进制

        long f = 66666666;
        long g = 555555555555555L;       //超出数据范围可加l或者L扩展
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
    }
}

运行结果:

46
101
169

Process finished with exit code 0

double类型eg:

class TestDouble {
    public static void main(String[] args) {
        final double PI = 3.1415926;	

        double r = 2;
        double area = PI * r * r;
        double circle = 2 * PI * r;
        System.out.println("面积:" + area);
        System.out.println("周长:" + circle);
    }//也可使用math类
}

运行结果:

面积:12.5663704
周长:12.5663704

float类型eg:

class TestFloat {
    public static void main(String[] args) {
        double a = 3.14;
        double a1 = 3.14E2;      //科学计数法
        System.out.println(a1);

//      float b=1.22;   Error:(50, 25) java: 不兼容的类型: 从double转换到float可能会有损失    //补充: 浮点数不是精确的
        float b = 1.22F;

        float c = 1.0F;
        double c1 = 1.0 / 10;
        System.out.println(c1);
        System.out.println(c == c1);
    }
}

运行结果:

314.0
0.1
false
Process finished with exit code 0

char类型eg:

class TestChar {
    public static void main(String[] args) {
        char c1 = 'A';
        char c2 = '中';
        char c3 = '\u0061';

        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);

        String a = "我在学习Java";
        System.out.println(a);

        System.out.println("\tA\tB\tC\n\t1\t2\t3");
    }
}

运行结果:

A
中
a
我在学习Java
	A	B	C
	1	2	3

Process finished with exit code 0

bool类型eg:

class Testbool {
    public static void main(String[] args) {
        boolean a = true;             //bool类型单独使用占4个字节,在数组中使用占一个字节
        boolean b = false;
        if (a) {
            System.out.println("a是true");
        } else {
            System.out.println("b是false");

        }
    }
}

运行结果:

a是true

Process finished with exit code 0

个人习惯:eg是例如的意思,英文全拼:exempli gratia

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值