Java教程

Java byte关键字

Java byte关键字是原始数据类型。它用于声明变量。它也可以与返回字节值的方法一起使用。它可以包含一个8位带符号的二进制补码整数。

要记住的要点

字节范围在-128到127(含)之间。 其默认值为0。 处理来自网络或文件的大量数据非常有用。 对于在大阵列中节省内存也很有用。 它可以代替int数据类型。 Javabyte关键字

Javabyte关键字示例

示例1: 将字节与变量一起使用

public class byteExample1 {
    public static void main(String[] args) {
        byte num1 = 127;
        byte num2 = -128;
        System.out.println("num1 : " + num1);
        System.out.println("num2 : " + num2);
    }
}

示例2: 字节超出范围

public class byteExample2 {
    public static void main(String[] args) {
        byte num1 = 128;
        byte num2 = -129;
        System.out.println("num1 : " + num1);
        System.out.println("num2 : " + num2);
    }
}
输出:
Javabyte关键字

示例3: 将字节与方法结合使用

public class byteExample3 {
    byte age = 18;
    public byte display() {
        return age;
    }
    public static void main(String[] args) {
        byteExample2 b = new byteExample2();
        System.out.println("The age must be: " + b.display());
    }
}
输出:
The age must be: 18
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4