logo

Java의 비원시 데이터 유형

데이터 유형은 변수에 저장되는 데이터 유형을 정의합니다. 유형은 데이터의 종류(다양한 크기와 값)를 지정합니다.

Java 프로그래밍 언어에는 두 가지 유형의 데이터 유형이 있습니다.

  1. 기본이 아닌 데이터 유형

    기본 데이터 유형과 달리 사전 정의되지 않습니다. 프로그래머가 만든 사용자 정의 데이터 형식입니다. 이러한 데이터 유형은 여러 값을 저장하는 데 사용됩니다.

    예를 들어 값 그룹을 저장하는 배열입니다. 클래스는 다양한 메소드와 변수를 저장하는 기본 유형이기도 합니다. 그러므로 이들은 또한 다음과 같이 불린다. 고급 데이터 유형 자바에서.

    비원시 데이터 유형이 정의될 때마다 이는 데이터가 힙 메모리에 저장되는 메모리 위치, 즉 객체가 배치되는 메모리 위치를 나타냅니다. 따라서 기본이 아닌 데이터 유형 변수라고도 합니다. 참조된 데이터 유형 또는 단순히 객체 참조 변수 .

    객체 참조 변수는 스택 메모리에 있으며, 이 변수가 가리키는 객체는 항상 힙 메모리에 있습니다. 스택은 힙의 개체에 대한 포인터를 보유합니다.

    Java 프로그래밍에서 기본이 아닌 모든 데이터 유형은 간단히 클래스를 인스턴스화하여 생성된 객체라고 합니다.

    키 포인트:

    1. 모든 참조 변수의 기본값은 null입니다.
    2. 기본이 아닌 데이터 유형을 메소드에 전달할 때마다 데이터가 저장된 해당 객체의 주소를 전달합니다.

    비원시 데이터 유형의 유형

    Java에는 5가지 유형의 비원시 데이터 유형이 있습니다. 그것들은 다음과 같습니다:

    1. 수업
    2. 물체
    3. 정렬
    4. 상호 작용

    1. 클래스 및 객체:

    수업 Java에서 사용자 정의 데이터 유형은 사용자가 생성합니다. 멤버 변수와 메소드로 구성된 데이터에 대한 템플릿 역할을 합니다.

    물체 클래스의 요소, 즉 메소드 및 변수에 액세스할 수 있는 클래스의 변수입니다.

    예:

    자바 문자열을 int로

    다음 예에서는 변수와 메소드를 포함하는 클래스를 생성합니다( add() 및 하위() ). 여기서는 클래스 객체를 사용하여 메소드에 액세스하고 있습니다. 객체 .

    ClassExample.java

     public class ClassExample { // defining the variables of class int a = 20; int b = 10; int c; // defining the methods of class public void add () { int c = a + b; System.out.println('Addition of numbers is: ' + c); } public void sub () { int c = a - b; System.out.println('Subtraction of numbers is: ' + c); } // main method public static void main (String[] args) { // creating the object of class ClassExample obj = new ClassExample(); // calling the methods obj.add(); obj.sub(); } } 

    산출:

     Addition of numbers is: 30 Subtraction of numbers is: 10 

    2. 인터페이스:

    상호 작용 클래스와 유사하지만 유일한 차이점은 해당 메서드가 기본적으로 추상적이라는 것입니다. 즉, 본문이 없습니다. 인터페이스에는 최종 변수와 메서드 선언만 있습니다. 완전 추상 클래스라고도 합니다.

    참고: 클래스가 인터페이스를 구현하는 경우 해당 인터페이스의 모든 메서드를 구현해야 합니다. 그렇지 않다면 클래스를 추상 클래스로 선언해야 합니다.

    예:

    다음 예에서는 두 개의 추상 메소드( 곱하기()와 나누기() ). 여기서 InterfaceExample 클래스는 인터페이스를 구현하고 해당 인터페이스의 메서드를 추가로 정의합니다. 그런 다음 클래스의 객체를 사용하여 해당 메서드에 액세스합니다.

    인터페이스Example.java

     interface CalcInterface { void multiply(); void divide(); } public class InterfaceExample implements CalcInterface { // defining the variables of class int a = 10; int b = 20; int c; // implementing the interface methods public void multiply() { int c = a * b; System.out.println('Multiplication of numbers is: ' + c); } public void divide() { int c = a / b; System.out.println('Division of numbers is: ' + c); } // main method public static void main (String[] args) throws IOException { InterfaceExample obj = new InterfaceExample(); // calling the methods obj.multiply(); obj.divide(); } } 

    3. 문자열:

    문자열은 'Javatpoint', 'Hello world' 등과 같은 일련의 문자를 나타냅니다. 문자열은 Java의 클래스입니다.

    문자열을 생성하고 그 안에 값을 저장하는 방법 중 하나는 다음과 같습니다.

     String str = 'You're the best'; 

    여기서는 String형 변수 str '당신이 최고다'라는 가치를 가지고 있습니다. 자세한 내용을 보려면 여기를 클릭하세요. 자바의 문자열 .

    예:

    다음 예에서는 값이 포함된 문자열을 생성합니다. 여기서는 String 클래스 메서드 중 하나를 사용하고 있습니다. 부분문자열() 문자열의 지정된 색인 부분을 인쇄합니다.

    StringExample.java

     public class StringExample { public static void main(String[] args) { // creating a string and initializing it String str = 'Hello! This is example of String type'; // applying substring() on above string String subStr = str.substring(0,14); // printing the string System.out.println(subStr); } } 

    산출:

     Hello! This is 

    4. 배열:

    정렬 여러 개의 동종 변수, 즉 동일한 유형의 변수를 시퀀스로 저장할 수 있는 데이터 유형입니다. 변수는 인덱스 0부터 시작하는 인덱스 방식으로 저장됩니다. 변수는 기본 데이터 유형이거나 기본이 아닌 데이터 유형일 수 있습니다.

    다음 예는 기본 데이터 유형의 배열을 선언하는 방법을 보여줍니다. 정수 :

     int [ ] marks; 

    다음 예에서는 기본이 아닌 데이터 유형의 배열을 선언하는 방법을 보여줍니다.

     Student [ ] students; 

    어디, 학생 는 클래스 이름이고 [ ]는 객체 배열을 생성합니다. 재학생 .

    예:

    다음 예에서는 하나가 초기화되고 다른 하나가 선언되는(사용자로부터 입력을 읽음) 두 개의 기본 배열을 생성합니다. 또한 for 루프를 사용하여 해당 배열을 인쇄합니다.

    ArrayExample.java

     // importing required packages import java.io. * ; import java.util. * ; public class ArrayExample { public static void main(String[] args) throws IOException { int i; Scanner sc = new Scanner(System. in ); // declaring and initializing an array int arr[] = {1, 2, 3, 6, 9}; // defining another array arr1 int arr1[] = new int[5]; // reading values from the user System.out.println(&apos;Enter the numbers (size = 5) :&apos;); for (i = 0; i <5; i++) { arr1[i]="sc.nextInt();" } system.out.println('previous array with initialized size is: '); for (i="0;" i < 5; system.out.print(arr[i] + ' system.out.println('
    the new we have entered is:'); system.out.print(arr1[i] pre> <p> <strong>Output:</strong> </p> <pre> Enter the numbers (size = 5) : 56 43 22 1 7 Previous array with initialized size is: 1 2 3 6 9 The new array we have entered is: 56 43 22 1 7 </pre> <h2>Difference between Primitive and Non-primitive Data types in Java</h2> <ol class="points"> <li>In Java, the primitive data types are system defined however we have to create and define the non-primitive data types.</li> <li>In primitive data type, variables can store only one value at a time. However in non-primitive data types, either multiple values of the same type or different type or both can be stored.</li> <li>All the data for primitive type variables are stored on the stack whereas, for reference types, the stack holds a pointer to the object on the heap.</li> <li>A primitive type starts with a lowercase letter, while non-primitive types start with an uppercase letter.</li> <li>The size of a primitive type depends on the data type, while non-primitive types have all the same size.</li> </ol> <hr></5;>

    Java의 기본 데이터 유형과 비원시 데이터 유형의 차이점

    1. Java에서 기본 데이터 유형은 시스템 정의이지만 비원시 데이터 유형을 만들고 정의해야 합니다.
    2. 기본 데이터 유형에서 변수는 한 번에 하나의 값만 저장할 수 있습니다. 그러나 기본이 아닌 데이터 유형에서는 동일한 유형이나 다른 유형의 여러 값 또는 둘 다를 저장할 수 있습니다.
    3. 기본 유형 변수의 모든 데이터는 스택에 저장되는 반면 참조 유형의 경우 스택은 힙의 객체에 대한 포인터를 보유합니다.
    4. 기본 유형은 소문자로 시작하고, 비원시 유형은 대문자로 시작합니다.
    5. 기본 유형의 크기는 데이터 유형에 따라 달라지지만 기본 유형이 아닌 유형은 모두 크기가 동일합니다.