更新時間:2022-06-02 10:20:51 來源:動力節(jié)點 瀏覽2130次
Java 是一種強類型語言。這意味著,在 Java 中,每種數(shù)據(jù)類型都有自己的嚴格定義。當數(shù)據(jù)類型之間發(fā)生任何沖突時,不存在隱式數(shù)據(jù)類型轉換。數(shù)據(jù)類型的任何更改都應由程序員明確聲明。
Java 定義了8 種原始數(shù)據(jù)類型:byte、short、int、long、char、float和.doubleboolean
它們分為以下幾類:
整數(shù)
浮點數(shù)字
人物
布爾類型
每種數(shù)據(jù)類型的詳細信息如下:
它們有四種類型:byte, short, int, long。重要的是要注意這些是有符號的正值和負值。有符號整數(shù)使用2 的補碼存儲在計算機中。它包含負值和正值,但格式不同,例如(-1 to -128)or (0 to +127)。無符號整數(shù)可以容納更大的正值,而沒有像(0 to 255). 與 C++ 不同,Java 中沒有無符號整數(shù)。
字節(jié)數(shù)據(jù)類型是一個 8 位有符號二進制補碼整數(shù)。
Wrapper Class: Byte
Minimum value: -128 (-2^7)
Maximum value: 127 (2^7 -1)
Default value: 0
Example: byte a = 10 , byte b = -50;
短數(shù)據(jù)類型是 16 位有符號二進制補碼整數(shù)。
Wrapper Class: Short
Minimum value: -32,768 (-2^15)
Maximum value: 32,767 (2^15 -1)
Default value: 0.
Example: short s = 10, short r = -1000;
int 數(shù)據(jù)類型是一個 32 位有符號二進制補碼整數(shù)。它通常用作整數(shù)值的默認數(shù)據(jù)類型,除非存在內存問題。
Wrapper Class: Integer
Minimum value: (-2^31)
Maximum value: (2^31 -1)
The default value: 0.
Example: int a = 50000, int b = -20
Long 數(shù)據(jù)類型是一個 64 位有符號二進制補碼整數(shù)。
Wrapper Class: Long
Minimum value: (-2^63)
Maximum value: (2^63 -1)
Default value: 0L.
Example: long a = 100000L, long b = -600000L;
By default all integer type variable is "int". So long num=600851475143 will give an error.
But it can be specified as long by appending the suffix L (or l)
這些也稱為實數(shù),用于涉及小數(shù)精度的表達式。它們有兩種類型:float, double。在貨幣或研究數(shù)據(jù)等精確數(shù)據(jù)的情況下,實際上避免了浮動。
float 數(shù)據(jù)類型是單精度 32 位IEEE 754 浮點數(shù)。
Wrapper Class: Float
Float is mainly used to save memory in large arrays of floating point numbers.
Default value: 0.0f.
Example: float f1 = 24.5f;
The default data type of floating-point number is double. So float f = 24.5 will introduce an error.
However, we can append the suffix F (or f) to designate the data type as float.
double 數(shù)據(jù)類型是雙精度 64 位IEEE 754 浮點。此數(shù)據(jù)類型通常是默認選擇。此數(shù)據(jù)類型絕不應用于精確值,例如貨幣。
Wrapper Class: Double
This data type is generally used as the default data type for decimal values.
Default value: 0.0d.
Example: double d1 = 123.400778;
我們使用這種數(shù)據(jù)類型來存儲字符。這與 C/C++ 中的 char 不同。Java 使用UNICODE國際公認的字符集。Java 中的字符為 16 位,而 C/C++ 中的字符為 8 位。
Wrapper Class: Character
Minimum value: '\u0000' (or 0).
Maximum value: '\uffff' (or 65,535).
Default value: null ('\u0000').
Example: char letterA ='a';
這用于存儲邏輯值。布爾類型的值可以是真或假。這種類型通常由關系運算符返回。
There are only two possible values: true and false.
Wrapper Class: Boolean
This data type is used for simple flags that track true/false conditions.
Default value is false.
Example: boolean b = true, boolean b1 = 1(this is not possible in java and give incompatible type error), boolean b2;
除了原始數(shù)據(jù)類型之外,還有使用不同類的構造函數(shù)創(chuàng)建的引用變量。引用變量可用于任何類以及數(shù)組、字符串、掃描儀、隨機數(shù)、模具等。使用新關鍵字初始化引用變量。
例子 :
public class Box{
int length, breadth, height;
public Box(){
length=5;
breadth=3;
height=2;
}
}
class demo{
public static void main(String args[]) {
Box box1 = new Box(); //box1 is the reference variable
char[] arr = new char[10]; //arr is the reference variable
}
}
String 不是原始數(shù)據(jù)類型,但它允許您將多個字符數(shù)據(jù)類型存儲在一個數(shù)組中,并且有許多可以使用的方法。當用戶輸入數(shù)據(jù)并且您必須對其進行操作時,它非常常用。
在下面的示例中,我們嘗試從字符串中刪除所有字母并將其輸出:
String input = "My birthday is 10 January 1984 and my favorite number is 42";
String output = "";
for(int i=0;i<input.length();i++){
//if the character at index i on the string is a letter or a space, move on to the next index
if(Character.isLetter(input.charAt(i)) || input.charAt(i)==' '){
continue;
}
output = output + input.charAt(i); //the number is added onto the output
}
System.out.println(output);
輸出:
10198442