Essential Java Data Types

Java Data Types

Java could be a statically-typed programming language, meaning each variable must have a declared type. Java data types are classified into Primitive Data Types and Non-Primitive (Reference) Data Types.

Data types

  • Data type is used in a declaring a variable.
  • Data type will decide which data need to be stored inside the variable.
  • We can say data type will assign restriction to the variable.
  • Data type will decide capacity or memory size of an variable.

We have a following two types of java data types

Java Data Types
Java Data Types
  1. Primitive data type
  2. Nonprimate data type

1. Primitive Data Types

These are the most basic data types, predefined by Java, and are directly stored in memory.

a) Numeric Data Types

Used for storing numbers.

TypeSizeDefault ValueDescriptionExample Values
byte1 byte0Stores small integers from -128 to 127.byte a = 100;
short2 bytes0Stores integers from -32,768 to 32,767.short b = 20000;
int4 bytes0Stores integers from -2,147,483,648 to 2,147,483,647.int c = 500000;
long8 bytes0LStores larger integers from -2^63 to 2^63-1.long d = 10000000000L;
float4 bytes0.0fStores fractional numbers with 6-7 decimal digits of precision.float e = 5.75f;
double8 bytes0.0dStores fractional numbers with up to 15 decimal digits of precision.double f = 19.99;

b) Character Data Type

TypeSizeDefault ValueDescriptionExample Values
char2 bytes‘\u0000’Stores a single character (uses Unicode).char g = 'A';

c) Boolean Data Type

TypeSizeDefault ValueDescriptionExample Values
boolean1 bitfalseStores a value of either true or false.boolean h = true

 

2. Non-Primitive Data Types

These include objects and arrays. Unlike primitives, non-primitive data types are references to objects in memory.

a) Strings

A String is a sequence of characters.

  • Example:
    String name = "Java Programming";

b) Arrays

Used to store multiple values of the same data type.

  • Example:
    int[] numbers = {10, 20, 30};

c) Classes and Objects

Used to define user-defined data types.

  • Example:
    class Car {
    String model;
    int year;
    }

Key Differences Between Primitive and Non-Primitive Data Types

FeaturePrimitive Data TypesNon-Primitive Data Types
DefinitionBasic, predefined by Java.User-defined or built-in (e.g., String).
StorageStores actual values.Stores references to objects.
Default ValuesHave predefined default values.Null for all non-primitive types.
Examplesint, char, boolean.String, Array, Class.

 

Choosing the Right Data Type

  • Performance: Use int or long for numeric calculations and float/double for decimals.
  • Storage Constraints: Use byte or short for memory-critical applications.
  • Use Case: Use non-primitive types for more complex objects like lists or user-defined data types.

 

Java Data Types Example

Here’s a Java program demonstrating different data types:

public class DataTypesExample {
public static void main(String[] args) {

// Primitive Data Types
byte byteValue = 127; // Byte range: -128 to 127
short shortValue = 32000; // Short range: -32,768 to 32,767
int intValue = 100000; // Int range: -2^31 to 2^31-1
long longValue = 10000000000L; // Long range: -2^63 to 2^63-1
float floatValue = 5.75f; // Float for fractional numbers
double doubleValue = 19.99; // Double for high precision
char charValue = ‘J’; // Single character
boolean booleanValue = true; // True/False

// Non-Primitive Data Types
String stringValue = “Hello, Java!”; // Sequence of characters
int[] arrayValue = {1, 2, 3, 4, 5}; // Array of integers

// Displaying Values
System.out.println(“Byte Value: ” + byteValue);
System.out.println(“Short Value: ” + shortValue);
System.out.println(“Integer Value: ” + intValue);
System.out.println(“Long Value: ” + longValue);
System.out.println(“Float Value: ” + floatValue);
System.out.println(“Double Value: ” + doubleValue);
System.out.println(“Character Value: ” + charValue);
System.out.println(“Boolean Value: ” + booleanValue);
System.out.println(“String Value: ” + stringValue);

System.out.println(“Array Values: “);
for (int i : arrayValue) {
System.out.print(i + ” “);
}
}
}

Explanation of the Program:

  1. Primitive Data Types:
    • The program initializes each primitive type with a valid value.
    • Examples include byte, int, float, char, and boolean.
  2. Non-Primitive Data Types:
    • A String object is used for textual data.
    • An integer array demonstrates how multiple values of the same type can be stored.
  3. Output:
    • The program uses System.out.println() to display primitive values.
    • For the array, a for-each loop iterates through its elements.

Expected Output:

Byte Value: 127
Short Value: 32000
Integer Value: 100000
Long Value: 10000000000
Float Value: 5.75
Double Value: 19.99
Character Value: J
Boolean Value: true
String Value: Hello, Java!
Array Values:
1 2 3 4 5

 

Leave a Comment