산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> java

자바 - 변수 유형

변수는 프로그램이 조작할 수 있는 명명된 저장소를 제공합니다. Java의 각 변수에는 변수 메모리의 크기와 레이아웃을 결정하는 특정 유형이 있습니다. 해당 메모리에 저장할 수 있는 값의 범위 변수에 적용할 수 있는 작업 집합입니다.

모든 변수를 사용하려면 먼저 선언해야 합니다. 다음은 변수 선언의 기본 형식입니다. -

data type variable [ = value][, variable [ = value] ...] ;

여기 데이터 유형 Java의 데이터 유형 중 하나이며 변수 변수의 이름입니다. 지정된 유형의 변수를 두 개 이상 선언하려면 쉼표로 구분된 목록을 사용할 수 있습니다.

다음은 Java에서 변수 선언 및 초기화의 유효한 예입니다 -

int a, b, c;         // Declares three ints, a, b, and c.
int a = 10, b = 10;  // Example of initialization
byte B = 22;         // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a';        // the char variable a iis initialized with value 'a'

이 장에서는 Java 언어에서 사용할 수 있는 다양한 변수 유형에 대해 설명합니다. 자바에는 세 종류의 변수가 있습니다 -

로컬 변수

예시

여기, 나이 지역변수이다. 이것은 pupAge() 내부에 정의되어 있습니다. 메소드 및 그 범위는 이 메소드로만 제한됩니다.

라이브 데모
public class Test {
   public void pupAge() {
      int age = 0;
      age = age + 7;
      System.out.println("Puppy age is : " + age);
   }

   public static void main(String args[]) {
      Test test = new Test();
      test.pupAge();
   }
}

이것은 다음 결과를 생성합니다 -

출력

Puppy age is: 7

예시

다음 예는 age를 사용합니다. 초기화하지 않고 컴파일시 에러가 발생합니다.

라이브 데모
public class Test {
   public void pupAge() {
      int age;
      age = age + 7;
      System.out.println("Puppy age is : " + age);
   }

   public static void main(String args[]) {
      Test test = new Test();
      test.pupAge();
   }
}

컴파일하는 동안 다음 오류가 발생합니다. -

출력

Test.java:4:variable number might not have been initialized
age = age + 7;
         ^
1 error

인스턴스 변수

예시

라이브 데모
import java.io.*;
public class Employee {

   // this instance variable is visible for any child class.
   public String name;

   // salary  variable is visible in Employee class only.
   private double salary;

   // The name variable is assigned in the constructor.
   public Employee (String empName) {
      name = empName;
   }

   // The salary variable is assigned a value.
   public void setSalary(double empSal) {
      salary = empSal;
   }

   // This method prints the employee details.
   public void printEmp() {
      System.out.println("name  : " + name );
      System.out.println("salary :" + salary);
   }

   public static void main(String args[]) {
      Employee empOne = new Employee("Ransika");
      empOne.setSalary(1000);
      empOne.printEmp();
   }
}

이것은 다음 결과를 생성합니다 -

출력

name  : Ransika
salary :1000.0

클래스/정적 변수

예시

라이브 데모
import java.io.*;
public class Employee {

   // salary  variable is a private static variable
   private static double salary;

   // DEPARTMENT is a constant
   public static final String DEPARTMENT = "Development ";

   public static void main(String args[]) {
      salary = 1000;
      System.out.println(DEPARTMENT + "average salary:" + salary);
   }
}

이것은 다음과 같은 결과를 생성합니다 -

출력

Development average salary:1000

참고 − 외부 클래스에서 변수에 액세스하는 경우 상수는 Employee.DEPARTMENT로 액세스해야 합니다.

다음은 무엇입니까?

이 장에서 이미 액세스 수정자(공개 및 비공개)를 사용했습니다. 다음 장에서는 Access Modifiers와 Non-Access Modifiers에 대해 자세히 설명합니다.


java

  1. C# 변수 및 (기본) 데이터 형식
  2. C 변수, 상수 및 리터럴
  3. C 스토리지 클래스
  4. 자바 변수와 리터럴
  5. Java 데이터 유형(기본)
  6. 자바 연산자
  7. 자바 인터페이스
  8. 자바 리소스 사용
  9. 자바 주석
  10. 자바 주석 유형