Java數(shù)據(jù)類型和變量
Java 是一種流行的編程語言,以其強類型系統(tǒng)而聞名,這意味著每個變量都必須有一個聲明的類型。Java提供了多種數(shù)據(jù)類型來適應各種數(shù)據(jù)和操作。
在本文中,我將帶您了解 Java 的數(shù)據(jù)類型并解釋它們的工作原理。
Java 中有兩種數(shù)據(jù)類型——原始數(shù)據(jù)類型和引用數(shù)據(jù)類型。讓我們深入了解每一個。
(更|多優(yōu)質內|容:java567 點 c0m)
原始數(shù)據(jù)類型和參考數(shù)據(jù)類型之間的差異
在 Java 中,引用數(shù)據(jù)類型和原始數(shù)據(jù)類型之間存在重要區(qū)別。
原始數(shù)據(jù)類型將實際值直接存儲在內存中,而引用數(shù)據(jù)類型存儲指向存儲對象位置的引用或內存地址。
如果未顯式初始化,原始數(shù)據(jù)類型具有默認值,而引用數(shù)據(jù)類型默認為null.
原始數(shù)據(jù)類型具有由語言定義的固定大小,而引用數(shù)據(jù)類型具有固定大小,無論它們引用的對象如何。
對原始數(shù)據(jù)類型的操作可以直接進行,而對引用數(shù)據(jù)類型的操作只能通過對象提供的方法進行。
原始數(shù)據(jù)類型有相應的包裝類,而引用數(shù)據(jù)類型則沒有。
當傳遞基本數(shù)據(jù)類型作為方法參數(shù)時,會傳遞值的副本,而傳遞引用數(shù)據(jù)類型時會按值傳遞引用。
這些差異顯示了引用數(shù)據(jù)類型和原始數(shù)據(jù)類型之間的存儲、默認值、大小、操作和按值傳遞語義在 Java 中的重要性。
Java 中的原始數(shù)據(jù)類型
Java 有八種基本數(shù)據(jù)類型,它們是存儲數(shù)據(jù)的最基本的構建塊。這些類型充當 Java 中數(shù)據(jù)操作的構建塊。
原始數(shù)據(jù)類型僅用于一個目的——包含某種純粹、簡單的值。它們是 Java 中的保留關鍵字。因為它們是關鍵字,所以不能用作變量名。它們包括以下內容:
byte
想象一下您有一個可以容納數(shù)字的小盒子。這個盒子可以容納從-128到127的數(shù)字。它就像一個玩具盒,只能容納特定范圍的玩具,從-128到127。你可以在盒子里放入這個范圍內的任何數(shù)字。
數(shù)據(jù)byte類型是一個 8 位有符號整數(shù),可以保存從 -128 到 127 的值。當需要考慮內存空間時通常使用它。讓我們創(chuàng)建一個類型的變量byte:
?byte myByte = 100;
?System.out.println("byte: " + myByte);
?Output
?byte: 100
short
現(xiàn)在,想象你有一個更大的盒子。該框可以保存short16 位有符號整數(shù)的數(shù)據(jù)類型,可以保存從 -32,768 到 32,767 的值。它對于存儲比數(shù)據(jù)類型更大的整數(shù)值非常有用byte。
?short myShort = 30000;
?System.out.println("short: " + myShort);
?Output:
?short: 30000
int
現(xiàn)在,讓我們考慮一個更大的存儲容器。Box 是一種int數(shù)據(jù)類型,是 32 位有符號整數(shù),可以容納從 -2,147,483,648 到 2,147,483,647 的數(shù)字。它就像一個大寶箱,可以容納各種各樣的數(shù)字,包括正數(shù)和負數(shù)。它是 Java 中最常用的表示整數(shù)的數(shù)據(jù)類型。
?int myInt = 2000000000;
?System.out.println("int: " + myInt);
?Output::
?int: 2000000000
long
好吧,現(xiàn)在我們有一個巨大的儲藏室了。該房間可容納的號碼范圍為 -9,223,372,036,854,775,808 至 9,223,372,036,854,775,807。這就像擁有一個可以存儲大量數(shù)字的大型倉庫。當需要更大范圍的整數(shù)值時使用它。
?long myLong = 9,223,372,036,854,775,807;
?System.out.println("long: " + myLong);
?Output::
?long: 9,223,372,036,854,775,807
Float
想象一下您有一個可以容納十進制數(shù)字的特殊盒子。該框可以保存中等精度的十進制數(shù)。它就像一個可以以合理的精度盛水的容器。
數(shù)據(jù)float類型是單精度32位浮點數(shù)。它對于表示具有中等精度的十進制數(shù)很有用。
?float myFloat = 9,223,372,036,854,775,807;
?System.out.println("float: " + myFloat);
?Output::
?float: 9,223,372,036,854,775,807
Double
數(shù)據(jù)double類型是雙精度64位浮點數(shù)。它提供比浮點更高的精度,通常用于涉及十進制數(shù)的計算。
?double myDouble = 129.7;
?System.out.println("double: " + mydouble);
?Output::
?double: 129.7
布爾值
數(shù)據(jù)boolean類型表示一個boolean值,可以是 true 或 false。它用于邏輯運算和控制流。
?boolean isJavaFun = true;
?boolean isProgrammingFun = false;
?
?System.out.println(isJavaFun); ? ?
?System.out.println(isProgrammingFun);
?Output::
?true
?false
Char
該char數(shù)據(jù)類型表示單個 Unicode 字符,大小為 16 位。它可以存儲 Unicode 字符集中的任何字符。
?char johnGrade = 'B';
?System.out.println(johnGrade);
?Output::
?B
Java 中的參考數(shù)據(jù)類型
除了原始數(shù)據(jù)類型之外,引用數(shù)據(jù)類型還用于存儲指向內存中存儲的對象的引用或內存地址。
這些數(shù)據(jù)類型實際上并不存儲數(shù)據(jù)本身,而是對存儲數(shù)據(jù)的內存位置的引用?,F(xiàn)在讓我們看一下幾種流行的參考數(shù)據(jù)類型。
字符串
該類String表示字符序列。它廣泛用于操作和存儲文本數(shù)據(jù)。
?String name = "John Doe";
?System.out.println("Name: " + name);
?Output::
?Name: John Doe
數(shù)組
Arrays用于存儲相同類型元素的集合。它們提供了一種處理相關值組的便捷方法。
?int[] numbers = {1, 2, 3, 4, 5};
?System.out.println("Numbers: " + java.util.Arrays.toString(numbers));
?Output::
?Numbers: 12345
Class
數(shù)據(jù)類型在Java中class表示a 。class它用于創(chuàng)建對象并定義它們的行為。
為了了解類在 Java 中的工作原理,我們將創(chuàng)建一個類示例并在主類中實現(xiàn)該類。
在下面的示例中,我們將創(chuàng)建一個Car類來表示具有顏色和速度屬性的汽車。我們將有一個構造函數(shù)來初始化顏色,并且速度默認設置為 0。該課程還將包括加速汽車和制動汽車的方法。
這是一個例子:
?public class ClassCarExample {
? ? ?// Instance variables or fields
? ? ?String color;
? ? ?int speed;
?
? //Method start that gets the car started
? ? ?public void start() {
? ? ? ? ?System.out.println("The car has started.");
? ? ?}
?
? ? ?//Method accelerate that increases the speed of the car by 10 km/h
? ? ? ?public void accelerate() {
? ? ? ? ?speed += 10;
? ? ? ? ?System.out.println("The car is accelerating. Current speed: " + ? speed + " km/h");
? ? ?}
?
? ? ?//Method brake that reduces the speed of the car by 5 each time the ? ?method is called
? ? ?
? ? ?public void brake() {
? ? ? ? ?speed -= 5;
? ? ? ? ?System.out.println("The car is braking. Current speed: " + ? ?speed + " km/h");
? ? ?}
?}
現(xiàn)在,我們將創(chuàng)建主要方法,我們將在其中運行我們的類并讓我們的汽車移動、加速和制動。
?public class Main {
? ? ?public static void main(String[] args) {
? ? ? ? ?// Create an instance of ClassCarExample
? ? ? ? ?ClassCarExample car = new ClassCarExample();
?
? ? ? ? ?// Start the car
? ? ? ? ?car.start();
? ? ? ? ?
?
? ? ? ? ?// Accelerate the car
? ? ? ? ?car.accelerate();
? ? ? ?
?
? ? ? ? ?// Brake the car
? ? ? ? ?car.brake();
? ? ? ? ?}
? ? ?}
?}
?Output:
?The car has started.
?The car is accelerating. Current speed: 10 km/h
?The car is braking. Current speed: 5 km/h
接口
該interface關鍵字用于聲明interface提供了 .Total 抽象(隱藏),這意味著 an 中的所有方法interface都使用空主體聲明,并且所有字段默認為public、static和final。
在 an 中聲明的所有方法都interface必須由class實現(xiàn) 的a 來實現(xiàn)interface。
為了更好地理解接口的工作原理,我們將創(chuàng)建一個名為 的接口類MyInterfaceClass,它聲明了三個方法:methodExampleOne() methodExampleTwo()、 和methodExampleThree():
?// The interface class
?interface MyInterfaceClass {
? ? ?void methodExampleOne();
? ? ?void methodExampleTwo();
? ? ?void methodExampleThree();
?}
現(xiàn)在我們需要一個實現(xiàn)接口類的類。我們將創(chuàng)建一個MyClass實現(xiàn)接口的類MyInterfaceClass,并為上面創(chuàng)建的所有三個方法提供實現(xiàn)。
?// Implement the interface in a class
?class MyClass implements MyInterfaceClass {
? ? ?public void methodExampleOne() {
? ? ? ? ?System.out.println("Implementing methodExampleOne");
? ? ?}
?
? ? ?public void methodExampleTwo() {
? ? ? ? ?System.out.println("Implementing methodExampleTwo");
? ? ?}
?
? ? ?public void methodExampleThree() {
? ? ? ? ?System.out.println("Implementing methodExampleThree");
? ? ?}
?}
為了更好地理解這一點,讓我們創(chuàng)建一個 main 方法,在該方法中我們可以創(chuàng)建我們的對象,并在我們將創(chuàng)建和運行 Java 程序的對象myClass上調用我們的方法。myObj
?// Main class to test the implementation
?public class Main {
? ? ?public static void main(String[] args) {
? ? ?
? ? ? ? ?//create myObj from MyClass
? ? ? ? ?MyClass myObj = new MyClass();
?
? ? ? ? ?// Call the implemented methods on the object we created.
? ? ? ? ?myObj.methodExampleOne();
? ? ? ? ?myObj.methodExampleTwo();
? ? ? ? ?myObj.methodExampleThree();
? ? ?}
?}
當我們運行該類時Main,我們將看到以下輸出:
?Output:
?Implementing methodExampleOne
?Implementing methodExampleTwo
?Implementing methodExampleThree
枚舉
數(shù)據(jù)Enum類型表示枚舉(列表)類型。它用于定義一組固定的命名值,例如一周中的幾天或顏色。
?class EnumClassExample { ?
? //defining the enum inside the class ?
?public enum Weekdays {
? ? ?SUNDAY,MONDAY,TUESDAY,WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
? ?} ?
? //main method ?
?public static void main(String[] args) { ?
?
? //loop throught the enum ?
?
? for (Weekdays w : Weekdays.values()) ?
? ? ?System.out.println(w); ?
? }
?} ?
?Output::
?SUNDAY
?MONDAY
?TUESDAY
?WEDNESDAY
?THURSDAY
?FRIDAY
?SATURDAY
包起來
了解 Java 數(shù)據(jù)類型對于有效地進行 Java 編程至關重要。無論是用于基本值存儲的原始數(shù)據(jù)類型還是用于復雜對象和行為的引用數(shù)據(jù)類型,每種數(shù)據(jù)類型都有特定的用途。
通過利用適當?shù)臄?shù)據(jù)類型,軟件工程師可以用 Java 編寫更高效、可靠且可維護的代碼。