Java 中的原始資料型別與物件資料型別及示例
在 Java 環境中,每個 變數 都包含某些 資料型別,這些資料型別指定已排序識別符號的值和型別。
主要有兩類:
原始資料型別
非原始資料型別或物件資料型別
原始資料型別是一些預定義的資料型別,具有特定的大小和型別。此方法具有一些標準值,其型別稱為位元組、短整型、整型、長整型、浮點型、雙精度浮點型、字元型和布林型。當我們想要執行原始結構時,它會將資料儲存在棧中併為該過程分配一個值。另一方面,物件資料型別是一種引用資料型別,包括陣列、字串、類、介面等。引用變數將被排序並存儲在棧中,而原始物件則採用堆排序路徑。
建立原始資料型別和物件資料型別的演算法
在此可能的演算法中,我們將向您展示如何在 Java 環境中執行原始資料型別和物件資料型別。透過使用此演算法,我們將構建一些 Java 語法來全面瞭解問題陳述。
步驟 1 - 開始程序。
步驟 2 - 宣告並匯入 Java 包。
步驟 3 - 宣告一個公共類。
步驟 4 - 宣告引數字串。
步驟 5 - 宣告輸入值方法。
步驟 6 - 宣告一個數組流。
步驟 7 - 為其填充資料。
步驟 8 - 將方法指定為原始或物件。
步驟 9 - 設定記憶體堆。
步驟 10 - 獲取受影響的值。
步驟 11 - 列印輸出。
步驟 12 - 終止程序。
建立原始資料型別和物件資料型別的語法
public class ARBRDDTest{
public static void main(String[] args){
int[] x = new int[3];
System.out.println(x.getClass().getName());
}
}
public class TestARBRDD {
public static void main(String[] args){
System.out.println(args instanceof Object);
int[] arr = new int[2];
System.out.println(arr instanceof Object);
}
}
在此可能的語法中,我們試圖向您展示。
遵循的方法
方法 1 - 使用整數資料、浮點數、字元資料和布林資料定義原始資料型別的 Java 程式碼
方法 2 - 使用更改方法更改值的 Java 程式碼,並透過 BigDecimal 物件方法定義物件資料型別
方法 1
使用整數資料、浮點數、字元資料和布林資料。
透過整數資料使用原始資料型別
在此可能的方法中,我們將使用整數資料型別應用原始資料型別。透過遵循此方法,我們將描述原始資料型別在 Java 環境中的工作過程。
public static void main(String[] args){
System.out.print("Y" + "O");
System.out.print('L' + 'O');
}
public static void main(String[] args){
System.out.print("Y" + "O");
System.out.print('L');
System.out.print('O');
}
示例
//Java code to define Primitive Data Type by using the Integer data
public class ARBRDD {
public static void main(String[] args){
byte b = 16;
System.out.println("b= " + b);
short s = 07;
System.out.println("s= " + s);
int i = 2001;
System.out.println("i= " + i);
long l = 1997;
System.out.println("l= " + l);
}
}
輸出
b= 16 s= 7 i= 2001 l= 1997
透過浮點數資料使用原始資料型別
在此可能的方法中,我們將使用浮點數資料型別應用原始資料型別。透過遵循此方法,我們將描述原始資料型別在 Java 環境中的工作過程。
示例
//Java code to define Primitive Data Type by using the Floating-Point Number data
public class ARBRDD{
public static void main(String[] args){
float f = 2001.10f;
System.out.println("f= " + f);
double d = 1997.10;
System.out.println("d= " + d);
}
}
輸出
f= 2001.1 d= 1997.1
透過字元資料使用原始資料型別
在此可能的方法中,我們將使用字元資料型別應用原始資料型別。透過遵循此方法,我們將描述原始資料型別在 Java 環境中的工作過程。
示例
//Java code to define Primitive Data Type by using the character data
public class ARBRDD{
public static void main(String[] args){
char ch = 'S';
System.out.println(ch);
char ch2 = '&';
System.out.println(ch2);
char ch3 = '$';
System.out.println(ch3);
}
}
輸出
S & $
透過布林資料使用原始資料型別
在此可能的方法中,我們將使用布林資料型別應用原始資料型別。透過遵循此方法,我們將描述原始資料型別在 Java 環境中的工作過程。
示例
//Java code to define Primitive Data Type by using the Boolean data
public class ARBBRDD{
public static void main(String[] args){
boolean t = true;
System.out.println(t);
boolean f = false;
System.out.println(f);
}
}
輸出
true false
方法 2
使用更改方法並透過 BigDecimal 物件方法定義物件資料型別。
使用值更改方法
在此可能的方法中,我們將使用 BigDecimal 物件方法應用物件資料型別。透過遵循此方法,我們將描述原始資料型別在 Java 環境中的工作過程。下面列出了一些基本宣告:
byte byteData= 88; //declaring the byte data type int intData= 20; // declaring the integer data type short shortData= 6000; //declaring the short data type long longData = 20000000000000L; // declaring the long data type float floatdata= 1.1f; // declaring the float data type double doubleData = 29.94d; // declaring the double data type boolean booleanData= true; //declaring the boolean data type char charData = 'A'; // declaring the character data type
示例
//Java code to define Object Data Type by using the method to make a change in the value of y which will not affect the value of x
import java.lang.*;
import java.util.*;
public class ARBRDD {
public static void main(String ar[]){
System.out.println("OBJECTIVE DATA TYPES ARE HERE\n");
int x = 10;
int y = x;
System.out.print("Initially: ");
System.out.println("x = " + x + ", y = " + y);
y = 30;
System.out.print("After changing y to 30: ");
System.out.println("x = " + x + ", y = " + y);
System.out.println(
"**Only value of y is affected here "
+ "because of Primitive Data Type\n");
System.out.println("REFERENCE DATA TYPES\n");
int[] c = { 10, 20, 30, 40 };
int[] d = c;
System.out.println("Initially");
System.out.println("Array c: "
+ Arrays.toString(c));
System.out.println("Array d: "
+ Arrays.toString(d));
System.out.println("\nModifying the value at "
+ "index 1 to 50 in array d\n");
d[1] = 50;
System.out.println("After modification");
System.out.println("Array c: "
+ Arrays.toString(c));
System.out.println("Array d: "
+ Arrays.toString(d));
System.out.println(
"**Here value of c[1] is also affected "
+ "because of Reference Data Type\n");
}
}
輸出
OBJECTIVE DATA TYPES ARE HERE Initially: x = 10, y = 10 After changing y to 30: x = 10, y = 30 **Only value of y is affected here because of Primitive Data Type REFERENCE DATA TYPES Initially Array c: [10, 20, 30, 40] Array d: [10, 20, 30, 40] Modifying the value at index 1 to 50 in array d After modification Array c: [10, 50, 30, 40] Array d: [10, 50, 30, 40] **Here value of c[1] is also affected because of Reference Data Type
使用 Java.math.BigDecimal 物件方法
在此可能的方法中,我們將使用 java.math.BigDecimal 物件方法應用物件資料型別。透過遵循此方法,我們將描述原始資料型別在 Java 環境中的工作過程。
示例
//Java code to define Object Data Type by using the BigDecimal object method
import java.io.*;
import java.math.BigDecimal;
public class ARBRDD {
public static void main(String[] args){
BigDecimal num1 = new BigDecimal("12");
int num2 = 15;
System.out.println(
"Addition of num1 and num2 = "
+ num1.add(new BigDecimal(num2)));
System.out.println(
"Subtraction of num1 and num2 = "
+ num1.subtract(new BigDecimal(num2)));
System.out.println(
"Multiplication of num1 and num2 = "
+ num1.multiply(new BigDecimal(num2)));
System.out.println(
"Division of num1 and num2 = "
+ num1.divide(new BigDecimal(num2)));
}
}
輸出
Addition of num1 and num2 = 27 Subtraction of num1 and num2 = -3 Multiplication of num1 and num2 = 180 Division of num1 and num2 is = 0.8
結論
在本文中,我們學習瞭如何操作原始資料型別和物件資料型別的各種方法。透過使用上述語法和演算法,我們構建了一些 Java 程式碼以有效的方式解釋問題陳述。
另請參閱: Java 面試問題及答案
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP