Development

자바의 원시 유형(Primitive Type).

sonpro 2023. 3. 15. 08:10
반응형

Primitive types

Primitive Types in Java

Java is a popular programming language that is widely used for developing web, mobile, and desktop applications. One of the fundamental concepts in Java is primitive types. In this article, we will explore what primitive types are, their characteristics, and how they are used in Java programming.

Summary

Primitive types in Java are the basic building blocks of data types. They are simple, non-objective data types that are used to store values. Java has eight primitive types, including byte, short, int, long, float, double, char, and boolean. Each primitive type has its own range of values and memory size. Primitive types are used to declare variables, perform arithmetic operations, and store data in arrays.

Characteristics of Primitive Types

Primitive types in Java have the following characteristics:

  • They are simple data types that are not objects.
  • They are predefined by the Java language and are not created by the programmer.
  • They are stored in the stack memory instead of the heap memory.
  • They have a fixed size and range of values.
  • They are faster and more efficient than objects.

Types of Primitive Types

Java has eight primitive types, which are:

  1. byte: A byte is a 8-bit signed integer that can store values from -128 to 127.
  2. short: A short is a 16-bit signed integer that can store values from -32,768 to 32,767.
  3. int: An int is a 32-bit signed integer that can store values from -2,147,483,648 to 2,147,483,647.
  4. long: A long is a 64-bit signed integer that can store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  5. float: A float is a 32-bit floating-point number that can store values from 1.4E-45 to 3.4028235E38.
  6. double: A double is a 64-bit floating-point number that can store values from 4.9E-324 to 1.7976931348623157E308.
  7. char: A char is a 16-bit Unicode character that can store values from '\u0000' to '\uffff'.
  8. boolean: A boolean can store only two values, true or false.

How to Declare and Use Primitive Types

To declare a primitive type in Java, you need to specify the type of the variable and assign a value to it. For example, to declare an int variable named age and assign it a value of 25, you can write:

int age = 25; 

You can also declare multiple variables of the same type in a single line, like this:

int x = 10, y = 20, z = 30; 

To perform arithmetic operations on primitive types, you can use the standard arithmetic operators, such as +, -, *, /, and %. For example, to add two int variables x and y and store the result in a third variable z, you can write:

int x = 10, y = 20, z; z = x + y; 

To store multiple values of the same type, you can use arrays. For example, to declare an array of int values named numbers and assign it some values, you can write:

int[] numbers = {1, 2, 3, 4, 5}; 

Conclusion

Primitive types are an essential part of Java programming. They are simple, non-objective data types that are used to store values. Java has eight primitive types, each with its own range of values and memory size. Primitive types are faster and more efficient than objects and are used to declare variables, perform arithmetic operations, and store data in arrays. Understanding primitive types is crucial for any Java programmer, and we hope this article has helped you gain a better understanding of them.

반응형