首頁  >  文章  >  Java  >  Java 中的變數

Java 中的變數

WBOY
WBOY原創
2024-08-30 15:10:381109瀏覽

變數是Java中用來儲存任何資訊的基本單位。變數名稱是指派給這些單元的名稱。 Java 程式碼可能需要數字或字串值形式的資訊。程式碼的多個階段可能需要同一組值。這就是變數出現的地方。

所有這些所需的值都可以分配給不同的變量,這些變數將儲存在各自的記憶體位置;因此,變數只不過是記憶體位置的名稱。將值儲存到變數比在程式碼中到處重複這些值更有效。此外,當所需值發生變更時,它也會有所幫助,因為只需在變數聲明的一個位置更改它就足夠了,而不必在多個位置更改。

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

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

變數宣告

變數在聲明之前不能使用,並且我們在聲明期間分配的資料類型定義了它可以保存的值的類型。可以將各種資料類型(例如 int、float、string、char、Boolean、long、double)賦值給變數。

Java 中宣告變數的一般語法如下:

Ex: int a = 1;

哪裡,

  • int – 資料型別
  • a – 變數名稱
  • 1 – 變數值

下圖給了相同的圖示:

Java 中的變數

變數初始化

為變數分配預設值稱為該變數的初始化。變數可以根據需要在宣告期間和程序的後期階段進行初始化。 範例: 以下是我們可以根據特定資料類型指派給變數的一些值:

  •  int i;我=10;
  •  字符串城市;城市=”班加羅爾”;
  •  字符a; a='H';
  •  浮點十二月;十二月=3.5;
  •  布爾值; val=true;

變數類型

Java中有3種類型的變數:

  • 局部變數
  • 實例變數
  • 靜態變數

1.局部變數

  • 這些是在它們所在的特定方法或區塊或建構子內宣告的變數。
  • 它們的變數值在聲明期間存儲,僅在該方法的範圍內有效,並在該方法退出時丟失。
  • 具有相同變數名稱的局部變數可以在多個方法或區塊中聲明,不會發生任何衝突。

範例: 在下面的範例中,我們將「num」和「name」視為局部變數。 代碼:

public class PatientDetails{
public void Patient()
{
// local variable num
//local variable name
int num = 1200;
string name = "Harish";
id = id + 1;
System.out.println("Patient Name is: " + name + " Patient Number is: " + num);
name = "Sudha";
System.out.println("Patient Name is: " + name + " Patient Number is: " + num);
}
public void DoctorDetails()
{
int num = 12000;
string name = "Vijay";
num = num +1;
System.out.println("Doctor Name is: " + name + " Doctor ID is: " + num);
name = "Suma";
System.out.println("Doctor Name is: " + name + " Doctor ID is: " + num);
}
public static void main(String args[])
{
PatientDetails pat = new PatientDetails();
pat. Patient();
pat.DoctorDetails();
}
}

輸出:

患者姓名是:Harish
病人號碼是:1200
患者姓名是:Sudha
患者號碼是:1201
醫師姓名是:Vijay
醫生ID是:12000
醫生名字是:Suma
醫生 ID 為:12001

這表明,在兩種不同的方法(即 Patient 和 DoctorDetails)中聲明時,相同的局部變數名稱「num」和「name」可以用於分配任意數量的不同值。

範例:

如果我們嘗試在方法之外顯示相同的局部變數“num”和“name”,它們的值將無效。

代碼:

public class PatientDetails{
public void Patient()
{
// local variable num
//local variable name
int id = 1200;
}
public static void main(String args[])
{
System.out.println("Patient Number is: " + num);
//printing local variable outside it's method
}
}

輸出:

java 程式碼中的編譯錯誤:-
prog.java:12:錯誤:找不到符號
System.out.println(「病患編號是:」 + num);
^
符號:變數 num
位置:類別PatientDetails
1 個錯誤

2.實例變數

  • 實例變數是在類別內部宣告的變量,而不是在任何方法內宣告的變數。
  • 它們在物件創建時創建,當物件銷毀時它們的值就會遺失。
  • 這些變數的初始化不是強制的,預設情況下,該值為零。
  • 它們是非靜態變量,這意味著每當創建新物件時都會分配變數的記憶體。

範例:

這裡的a、b、c是兩個不同物件在兩個不同實例中宣告的實例變數。

代碼:

import java.io.*;
class Marks {
// a, b, c are instance variables
// a, b, c variables are being declared inside a class and not function
int a;
int b;
int c;
}
class MarksDemo {
public static void main(String args[])
{
// first object declaration
Alpha alp1 = new Alpha();
alp1 .a= 44;
alp1 .b= 77;
alp1 .c= 88;
// second object declaration
Alpha alp2 = new Alpha();
alp2 .a= 77;
alp2 .b= 55;
alp2 .c= 74;
// displaying variable values for first object
System.out.println("Values for first object:");
System.out.println(alp1.a);
System.out.println(alp1.b);
System.out.println(alp1.c);
// displaying variable values for second object
System.out.println("Values for second object:");
System.out.println(alp2.a);
System.out.println(alp2.b);
System.out.println(alp2.c);
}
}

輸出:

第一個物件的值:
44
77
88
第二個物件的值:
77
55
74

3. Static Variables

  • Static variables are declared at the beginning of the program, preceded by the static keyword.
  • Like instance variables, the initialization of static variables is not mandatory, and their default value is 0.
  • Only one static variable name will be created when the program is started and lost when execution has ended.
  • The memory for these variables is allocated only once at the time of loading the class and can be shared by multiple objects.
  • When the objects are different, the changes made in the value of the static variable in one of the objects will be reflected in all unlike instance variables where the values declared in one object will not be reflected in others.

Example:

Code:

import java.io.*;
class Students {
//static variable rollno
public static double rollno;
public static String name = "Lilly";
public static classnum;
}
public class StudentDetails {
public static void main(String args[])
}
{
// no need of object to access static variables
Students .rollno= 101;
Students.classnum=3;
System.out.println(Students .name + "'s rollno is:" + Students .rollno + "and class number is:" + Students.classnum);
}
}

Output:

Lilly’s rollno is 101, and the class number is: 3

Conclusion – Variables in Java

Variables form the elemental part of a Java program. They point to a particular memory location when they are created, and the same is released when the object is not referenced anymore. This memory which is released, will be swept away when garbage collection takes place. This process can also be considered as the life cycle of a variable.

以上是Java 中的變數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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