GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Java Data Types - GeeksforGeeks Java Data TypesLast Updated : 30 Sep, 2026 Java data types define the type of data a variable can store in a program. They help the compiler allocate memory efficiently and ensure type safety. Java provides two main categories: primitive and non-primitive data types.
Syntax
Types of Data TypesData types in Java define the kind of data a variable can hold and the memory required to store it. They are broadly divided into two categories:
![]() 1. Primitive Data TypesPrimitive data types store simple values directly in memory. Java provides eight primitive data types, each with a fixed size and range, which are summarized below:
1. boolean Data TypeRepresents one of two logical values: true or false. It is commonly used in conditions and control statements. Syntax:
Output Is Java fun? true Is fish tasty? false 2. byte Data TypeAn 8-bit signed integer used to save memory in large numeric arrays. Syntax:
Output Age: 25 Temperature: -10 3. short Data TypeA 16-bit signed integer often used when memory is limited and values are moderate in size. Syntax:
Output Number's of Students: 1000 Temperature: -200 4. int Data TypeA 32-bit signed integer and the most commonly used numeric data type. Syntax:
Size : 4 bytes ( 32 bits )
Output Population: 2000000 Distance: 150000000 5. long Data TypeA 64-bit signed integer used when int is not sufficient for large values. Syntax:
Size : 8 bytes (64 bits)
Output World Population: 7800000000 Light Year Distance: 9460730472580800 6. float Data TypeA 32-bit single-precision floating-point type used for fractional values. Syntax:
Size : 4 bytes (32 bits)
Output Value of Pi: 3.14 Gravity: 9.81 7. double Data TypeA 64-bit double-precision floating-point type and the default for decimal numbers. Syntax:
Size : 8 bytes (64 bits). It is recommended to go through rounding off errors in java.
Output Value of Pi: 3.141592653589793 Avogadro's Number: 6.02214076E23 8. char Data TypeA 16-bit Unicode character used to store single symbols or letters. Syntax:
Size : 2 bytes (16 bits) Example: This example, demonstrates how to use char data type to store individual characters.
Output Grade: A Symbol: $ 2. Non-Primitive (Reference) Data TypesNon-primitive data types store references (memory addresses) rather than actual values. They are created by users and include types like String, Class, Object, Interface, and Array. 1. StringString represents a sequence of characters enclosed in double quotes. Unlike C/C++, Java strings are objects and are immutable. Syntax:
Output Name: Geek1 Message: Welcome to Java Explanation: The example creates two String objects, name and message, and stores text values in them. The println() statements display these values on the console.
2. ClassA class is a user-defined blueprint that defines variables and methods. It represents a type of object and forms the foundation of Object-Oriented Programming.
Output Toyota 2020 Explanation: The Car class defines two fields, model and year, and a constructor to initialize them. In main(), a Car object is created using new Car("Toyota", 2020), and the display() method prints its model and year. 3. ObjectAn Object is an instance of a class representing real-world entities. It has state (data), behavior (methods), and identity (unique reference).
Output Car Model: Honda Car Year: 2021 Explanation: The example creates a 4. InterfaceAn interface defines a contract of abstract methods that implementing classes must define. It provides a way to achieve abstraction and multiple inheritance in Java.
Output Woof Explanation: The 5. ArrayAn array stores multiple elements of the same type in a single structure. Java arrays are objects, dynamically allocated, and indexed from 0.
Output First number: 1 Second name: Geek2 Explanation: The example creates an integer array named numbers and a String array named names. The elements are accessed using their index, where indexing starts from 0; therefore, numbers[0] gives 1 and names[1] gives "Geek2". Advantages of Java Data Types
Comment Explore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |