首页  >  文章  >  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