Java Data Types

In Java, data types are fundamental concepts that determine the kind of data that can be stored and manipulated within a program. Proper understanding of data types is essential for writing efficient and error-free code. This tutorial provides a comprehensive overview of Java data types, their significance, and examples to illustrate their use.

Contents:

  1. What are Data Types in Java?
  2. Primitive Data Types in Java
  3. Table of Primitive Data Types in Java
  4. Boolean Data Type
  5. Byte Data Type
  6. Short Data Type
  7. Int Data Type
  8. Long Data Type
  9. Float Data Type
  10. Double Data Type
  11. Data Type
  12. Non-Primitive Data Types in Java
  13. Classes
  14. Object
  15. Arrays
  16. String
  17. Interface
  18. FAQs on Data Types in Java

What are Data Types in Java?

Data types in Java specify the different sizes and values that can be stored in a variable. Java is a statically-typed language, meaning that variables must be declared with a specific data type before they can be used. This helps to ensure the consistency and safety of the code by preventing errors that can occur from operations on incompatible types.

Java data types can be broadly categorized into two types:

  • Primitive Data Types: Primitive data types in Java include boolean, char, byte, short, int, long, float, and double.
  • Non-Primitive Data Types: Non-primitive data types in Java include Classes, Interfaces, Object, String and Arrays.

Primitive Data Types in Java

Primitive data types are the most basic data types available in Java. Primitive data types represent basic values, such as integers, floating-point numbers, characters, and booleans. Java provides eight primitive data types:

advertisement
  1. byte: 8-bit signed integer.
  2. short: 16-bit signed integer.
  3. int: 32-bit signed integer.
  4. long: 64-bit signed integer.
  5. float: 32-bit floating-point number.
  6. double: 64-bit floating-point number.
  7. char: 16-bit Unicode character.
  8. boolean: Represents true or false values.

Table of Primitive Data Types in Java

This table provides a quick reference for the characteristics of the primitive data types in Java, helping to understand their default values, sizes in memory, and the ranges of values they can store.

Data Type Default Value Default Size (in bytes) Typical Range
boolean false 1 bit (not strictly defined) true or false
byte 0 1 byte -128 to 127
short 0 2 bytes -32,768 to 32,767
int 0 4 bytes -2,147,483,648 to 2,147,483,647
long 0L 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 0.0f 4 bytes Approximately ±1.4E-45 to ±3.4E+38
double 0.0 8 bytes Approximately ±4.9E-324 to ±1.7E+308
char ‘\u0000’ 2 bytes ‘\u0000’ to ‘\uffff’ (0 to 65,535)

Boolean Data Type

The boolean data type in Java is used to store only two possible values: true or false. It is primarily used for flagging conditions and controlling program flow. It has a default value of false.

Syntax:

boolean variableName = true; // or false

Example:

boolean isJavaFun = true;
boolean isJavaDifficult = false;

Byte Data Type

The byte data type in Java is an 8-bit signed integer used to save memory in large arrays, with a default value of 0. It has a size of 1 byte and a range from -128 to 127.

Syntax:

byte variableName = 100;

Example:

byte age = 23;
System.out.println("Age: " + age); // Output: Age: 23

advertisement

Short Data Type

The short data type in Java is a 16-bit signed integer used to save memory in large arrays, with a default value of 0. It has a size of 2 bytes and a range from -32,768 to 32,767.

Syntax:

short variableName = 1000;

Example:

short distance = 1500;
System.out.println("Distance: " + distance); // Output: Distance: 1500

Int Data Type

The int data type in Java is a 32-bit signed integer, commonly used as the default data type for integer values. It has a default value of 0, a size of 4 bytes, and a range from -2,147,483,648 to 2,147,483,647.

Syntax:

int variableName = 100000;

Example:

int a = 45;
System.out.println("a: " + a); // Output: Population: 45

Long Data Type

The long data type in Java is a 64-bit signed integer used when a wider range than int is needed. It has a default value of 0L, a size of 8 bytes, and a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Syntax:

long variableName = 100000L;

Example:

long distanceToSun = 149600000000L; // distance to the sun in meters
System.out.println("Distance to the Sun: " + distanceToSun + " meters");

Float Data Type

The float data type in Java is a single-precision 32-bit IEEE 754 floating-point, used primarily to save memory in large arrays of floating-point numbers. It has a default value of 0.0f, a size of 4 bytes, and can represent fractional numbers.

Syntax:

float variableName = 10.5f;

Example:

float pi = 3.14f;
System.out.println("Value of pi: " + pi); // Output: Value of pi: 3.14

Double Data Type

The double data type in Java is a double-precision 64-bit IEEE 754 floating-point, used for decimal values when more precision is needed. It has a default value of 0.0d, a size of 8 bytes, and can represent fractional numbers with greater precision than float.

Syntax:

double variableName = 20.99;

Example:

double temperature = 98.6;
System.out.println("Temperature: " + temperature); 
// Output: Temperature: 98.6

Char Data Type

The char data type in Java is a single 16-bit Unicode character, used to store any character. It has a default value of ‘\u0000’, a size of 2 bytes, and can represent any character in the Unicode standard.

Syntax:

char variableName = 'A';

Example:

char grade = 'A';
System.out.println("Grade: " + grade); // Output: Grade: A

Non-Primitive Data Types in Java

In Java, non-primitive data types are also known as reference types or object types. These data types do not hold the actual data but rather references (or pointers) to the objects in memory.

Classes

Classes in Java are user-defined reference types that can have fields (variables) and methods (functions). They serve as blueprints for creating objects.

Syntax:

public class MyClass
{
    private int myField;
 
    public void myMethod()
    {
        // method body
    }
}

Object

Objects are instances of classes. They are created using the new keyword and can be manipulated, passed around, and stored in variables.

Syntax:

MyClass obj = new MyClass();

Arrays

Arrays are collections of variables of the same type that are accessed by an index. Arrays can hold either primitive data types or objects.

Syntax:

int[] myArray = new int[10];
MyClass[] objArray = new MyClass[5];

String

Strings in Java are objects of the String class, not primitive types. They represent a sequence of characters and support various operations.

Syntax:

String str = "Hello, World!";

Interface

Interfaces define a contract of methods that a class implementing the interface must provide. They can also have constants.

Syntax:

public interface MyInterface
{
    void myMethod(); // Abstract method
    int CONSTANT = 10; // Constant
}

FAQs on Data Types in Java

1. What are data types in Java?

Data types in Java specify the type of data that a variable can store. They also determine the operations that can be performed on the variables.

2. How many types of data types are there in Java?

Java data types can be divided into two categories:

  • Primitive data types: These include int, long, float, double, byte, short, boolean, and char.
  • Reference/Object data types: These include classes, interfaces, string, object and arrays.

3. What are primitive data types in Java?

Primitive data types are the most basic data types available in Java. They are predefined by the language and named by a reserved keyword. There are eight primitive data types in Java:

  • byte: Stores 8-bit signed integer values.
  • short: Stores 16-bit signed integer values.
  • int: Stores 32-bit signed integer values.
  • long: Stores 64-bit signed integer values.
  • float: Stores single-precision 32-bit IEEE 754 floating point values.
  • double: Stores double-precision 64-bit IEEE 754 floating point values.
  • boolean: Stores true or false values.
  • char: Stores a single 16-bit Unicode character.

These data types are used to declare variables and specify the type of data they can hold.

4. What are reference/object data types in Java?

Reference or Object data types are created by the programmer and can be used to call methods to perform certain operations, or to access attributes.

5. Can I create my own data types in Java?

Yes, you can create your own data types in Java using classes and interfaces. These custom data types are known as user-defined data types.

6. How are reference data types different from primitive data types?

Reference data types refer to objects and store references (memory addresses) to those objects. Primitive data types store actual values.

7. How do I choose the appropriate data type for my variables?

Choose the data type based on the range of values you need to store and the memory requirements of your application. For example, use `int` for whole numbers within a certain range, `double` for decimal numbers, and `String` for text.

8. What is the difference between `==` and `.equals()` in Java?

The `==` operator compares object references, while the `.equals()` method compares the contents of objects. For primitive data types and reference data types, `==` compares memory addresses, while `.equals()` compares values.

Key Points to Remember

Here is the list of key points we need to remember about the “Java Data Types”.

  • Java uses data types to define the type of data a variable can hold.
  • There are two main categories of data types: primitive and non-primitive.
  • Primitive data types include: byte, short, int, long, float, double, boolean, and char.
  • Non-primitive data types include: classes, objects, arrays, String, and interfaces.
  • Primitive data types store actual values, while non-primitive data types store references to objects in memory.
  • Choose the data type based on the range of values you need and memory requirements.
  • Use int for whole numbers, double for decimals, and String for text.
  • The == operator compares object references, while .equals() method compares object contents.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.