首頁  >  文章  >  Java  >  Java 中的原始資料型別

Java 中的原始資料型別

WBOY
WBOY原創
2024-08-30 15:15:33402瀏覽

java中的原始資料類型是指定資料類型和大小但不提供任何額外方法的資料類型; Java 中可用的基本資料類型的範例包括byte、short、int、char、long、float、boolean 和double。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

以下是顯示 Java 中如何使用原始資料型別的語法:

byte byteData= 88;  //declaring byte data type
short shortData= 6000;   //declaring short data type
int intData= 20;   // declaring integer data type
long longData = 20000000000000L;  // declaring long data type
float floatdata= 1.1f;     // declaring float data type
double doubleData = 29.94d;    // declaring double data type
boolean booleanData= true;    //declaring boolean data type
char charData = ’b’;      // declaring character data type

Java 中的原始資料型別

java中的原始資料型別可以細分為以下四組:

1.整數資料型別

java中的整數資料型別儲存正數和負數。 byte、short、int 和 long 等資料型別都屬於這類資料型別。

  • Byte: Java中的Byte資料型別可以儲存-128到127範圍內的數字。每當我們想要節省記憶體時,都可以使用byte資料類型,因為它消耗的記憶體會較少轉換為 int 資料類型。
  • Int:java中的Int資料類型可以儲存-2147483648到2147483647範圍內的數字,是我們儲存數字時常用的資料類型。
  • Short: Short 資料型別可以儲存範圍從 -32768 到 32767 的資料。
  • Long:長資料型別可用於儲存範圍從-9223372036854775808到9223372036854775807的數字。當要儲存的資料超出整數資料類型的範圍時,通常首選長資料類型。如果是長資料型,實際資料後面必須跟著「L」。

2.浮點數

這種類型的資料類型是為了儲存十進制數字而設計的。浮點型和雙精度型屬於此類資料類型。

  • Float:Float 資料型別可以儲存小數點後 6 到 7 位元的小數值。使用浮點數資料型別時,實際資料後面應跟著「f」。
  • Double:Double 資料類型旨在儲存具有 14 到 15 位小數的數字。使用雙精確度資料類型時,實際值後面應帶有“d”。
  • Boolean:布林資料型別是使用 boolean 作為關鍵字宣告的,它只允許兩個 true 或 false 值。
  • 字元:java中的字元資料類型是使用char關鍵字聲明的,佔用2個位元組的空間。它只能用於儲存單一字元。

這是一個顯示不同資料型別以及大小的表格:

Primitive Data Type Size Details
byte 1 byte Stores positive and negative numbers ranging from -128 to 127.
int 4 bytes Stores positive and negative numbers ranging from -2,147,483,648 to 2,147,483,647.
short 2 bytes Stores positive and negative numbers ranging from -32,768 to 32,767.
long 8 bytes Stores positive and negative numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
float 4 bytes Stores Decimal numbers. It can be used for storing numbers having 6 to 7 decimal digits
double 8 bytes Stores Decimal numbers. It can be used for storing numbers having 15 decimal digits.
boolean 1 bit Can Store Only true or false.
char 2 bytes It can be used for storing only a single character, letter or ASCII values.
原始資料類型

尺寸

詳細資料

位元組 1 位元組 儲存範圍從 -128 到 127 的正數和負數。 整數 4 位元組 儲存範圍從 -2,147,483,648 到 2,147,483,647 的正數和負數。 短 2 位元組 儲存範圍從 -32,768 到 32,767 的正數和負數。 長 8 位元組 儲存從 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 的正數和負數。 浮動 4 位元組 儲存十進制數字。可用於儲存6到7位小數的數字 雙 8 位元組 儲存十進制數字。它可用於儲存具有 15 位十進制數字的數字。 布林值 1 位元 只能儲存 true 或 false。 字元 2 位元組 它可用於僅儲存單一字元、字母或 ASCII 值。 表> 在原始資料中實現的範例 在此範例中,我們將展示如何使用 Java 程式設計中可用的不同基本類型:
public class DataTypeDemo {
public static void main(String[] args) {
byte byteData= 88;    //declaring byte data type
int intData= 20;     // declaring integer data type
short shortData= 6000;    //declaring short data type
long longData = 20000000000000L;  // declaring long data type
float floatdata= 1.1f;   // declaring float data type
double doubleData = 29.94d;    // declaring double data type
boolean booleanData= true;  //declaring boolean data type
char charData = 'A';     // declaring character data type
System.out.println("Value Declared using Byte Data Type is  " + byteData);
System.out.println("Value Declared using Integer Data Type is  " + intData);
System.out.println("Value Declared using Short Data Type is  " + shortData);
System.out.println("Value Declared using Long Data Type is  " + longData);
System.out.println("Value Declared using Float Data Type is  " + floatdata);
System.out.println("Value Declared using Double Data Type is  " + doubleData);
System.out.println("Value Declared using Character Data Type is  " + charData);
System.out.println("Value Declared using Boolean Data Type is  " + booleanData);
}
}

代碼:

Java 中的原始資料型別

輸出:

結論 為了學習任何程式語言,正確理解不同的資料型別非常重要。上面的文章詳細解釋了java原始資料類型,並舉例說明了每種資料類型的意義。

以上是Java 中的原始資料型別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn