Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

คลาสไบต์ใน Java


คลาส Byte ล้อมค่าของ byte ชนิดดั้งเดิมในอ็อบเจ็กต์ ออบเจ็กต์ประเภท Byte มีฟิลด์เดียวที่มีประเภทเป็นไบต์

ต่อไปนี้เป็นวิธีการบางอย่างของคลาส Byte -

Sr.No. วิธีการ &คำอธิบาย
1 byte byteValue()
วิธีนี้จะคืนค่าของไบต์นี้เป็นไบต์
2 int comparisonTo(Byte anotherByte)
วิธีนี้จะเปรียบเทียบวัตถุสองไบต์เป็นตัวเลข
3 สแตติกไบต์ถอดรหัส (สตริง นาโนเมตร)
วิธีนี้จะถอดรหัสสตริงเป็นไบต์
4 double doubleValue()
เมธอดนี้คืนค่าของไบต์นี้เป็นสองเท่า
5 บูลีนเท่ากับ (Object obj)
เมธอดนี้เปรียบเทียบอ็อบเจ็กต์นี้กับอ็อบเจ็กต์ที่ระบุ
6 float floatValue()
วิธีนี้จะคืนค่าของไบต์นี้เป็นทศนิยม
7 int hashCode()
เมธอดนี้ส่งคืนรหัสแฮชสำหรับไบต์นี้
8 int intValue()
เมธอดนี้คืนค่าของไบต์นี้เป็น int
9 long longValue()
วิธีนี้จะคืนค่าของไบต์นี้เป็นแบบยาว
10 สแตติก parseByte (สตริง s)
เมธอดนี้แยกวิเคราะห์อาร์กิวเมนต์สตริงเป็นไบต์ทศนิยมที่มีเครื่องหมาย

เรามาดูตัวอย่างกัน-

ตัวอย่าง

import java.lang.*;
public class Demo {
   public static void main(String[] args){
      Byte b1, b2;
      int i1, i2;
      b1 = new Byte("1");
      b2 = new Byte("-1");
      i1 = b1.intValue();
      i2 = b2.intValue();
      String str1 = "int value of Byte " + b1 + " is " + i1;
      String str2 = "int value of Byte " + b2 + " is " + i2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

ผลลัพธ์

int value of Byte 1 is 1
int value of Byte -1 is -1

ตัวอย่าง

เรามาดูตัวอย่างอื่นกัน −

import java.lang.*;
public class Demo {
   public static void main(String[] args){
      Byte b1, b2;
      String s1, s2;
      b1 = new Byte("-123");
      b2 = new Byte("0");
      s1 = b1.toString();
      s2 = b2.toString();
      String str1 = "String value of Byte " + b1 + " is " + s1;
      String str2 = "String value of Byte " + b2 + " is " + s2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

ผลลัพธ์

String value of Byte -123 is -123
String value of Byte 0 is 0