CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Arrays of Primitive Types in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Arrays of Primitive Types in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 4 min read · 0 Comments
Share: in X

An array of primitive types in Java is a data structure that stores a fixed-size collection of values of simple data types like integers, floats, or characters. An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created and cannot be changed afterward. Arrays of primitive types in Java store actual values (e.g., int, char, double), while arrays of objects store references to objects.

Table of Contents

  • Declaring Arrays of Primitive Types in Java
    • Syntax:
  • Initializing Arrays of Primitive Types in Java
    • Using the new Keyword
    • Using an Array Literal
  • Accessing Array Elements
  • Modifying Array Elements
  • Iterating Over Arrays
    • Using a for Loop:
    • Using an Enhanced for Loop:
  • Best Practices for Using Arrays
  • Conclusion
  • Frequently Asked Questions
    • What is the default value for elements in an uninitialized array of primitive types?
    • Can I change the size of an array of primitive types after initialization?
    • How do I access elements in an array of primitive types?

Declaring Arrays of Primitive Types in Java

To declare an array of a primitive type, specify the data type followed by square brackets [] and the array name.

Syntax:

data_type[] array_name;

Examples:

int[] numbers; // Array of integers
double[] prices; // Array of doubles
char[] letters; // Array of characters
boolean[] flags; // Array of booleans

Initializing Arrays of Primitive Types in Java

Arrays can be initialized in two ways:

Using the new Keyword

Specify the size of the array, and Java will initialize the elements with default values.

    int[] numbers = new int[5]; // Array of 5 integers (default value: 0)
    double[] prices = new double[10]; // Array of 10 doubles (default value: 0.0)

    Using an Array Literal

    Provide the values directly at the time of declaration.

    int[] numbers = {1, 2, 3, 4, 5}; // Array of 5 integers
    char[] letters = {'a', 'b', 'c'}; // Array of 3 characters

    Accessing Array Elements

    Array elements are accessed using their index, which starts from 0. The syntax for accessing an element is:

    array_name[index]

    Example:

    int[] numbers = {10, 20, 30, 40, 50};
    System.out.println(numbers[0]); // Output: 10
    System.out.println(numbers[2]); // Output: 30

    Modifying Array Elements

    You can modify the value of an array element by assigning a new value to it using its index.

    Example:

    int[] numbers = {10, 20, 30, 40, 50};
    numbers[1] = 25; // Change the second element to 25
    System.out.println(numbers[1]); // Output: 25

    Iterating Over Arrays

    You can iterate over an array using a for loop or an enhanced for loop (for-each loop).

    Using a for Loop:

    int[] numbers = {10, 20, 30, 40, 50};
    for (int i = 0; i < numbers.length; i++) {
        System.out.println(numbers[i]);
    }

    Using an Enhanced for Loop:

    int[] numbers = {10, 20, 30, 40, 50};
    for (int num : numbers) {
        System.out.println(num);
    }

    Best Practices for Using Arrays

    1. Use Descriptive Names: Choose meaningful names for arrays to make your code more readable.
    2. Initialize Arrays Properly: Always initialize arrays before using them to avoid NullPointerException.
    3. Check Array Bounds: Ensure that you do not access elements outside the array’s bounds to avoid ArrayIndexOutOfBoundsException.
    4. Use Enhanced for Loops: Prefer enhanced for loops for iterating over arrays when you do not need the index.

    Remember that arrays in Java are zero-indexed, which means the first element is at index 0, the second element is at index 1, and so on. Also, be cautious about accessing elements outside the valid index range, as it may result in an ArrayIndexOutOfBoundsException.

    Conclusion

    Arrays of primitive types are a powerful and efficient way to store and manipulate collections of data in Java. By understanding how to declare, initialize, and use arrays, you can write cleaner, more efficient, and maintainable code. Whether you’re working with integers, doubles, characters, or booleans, mastering arrays is essential for becoming a proficient Java developer.

    Frequently Asked Questions

    What is the default value for elements in an uninitialized array of primitive types?

    Elements in an uninitialized array of primitive types are set to default values: 0 for numeric types, ‘\u0000’ for char, and false for boolean.

    Can I change the size of an array of primitive types after initialization?

    No, the size of an array in Java is fixed upon initialization and cannot be changed. You would need to create a new array if a different size is required.

    How do I access elements in an array of primitive types?

    You can access elements using their index within square brackets, like this:
    int value = numbers[0];
    It retrieves the first element of the numbers array.

    Related Posts:

    • Arrays in Java
    • Primitive data types in Java
    • Control Statements in Java
    • Character data type in Java
    • Identifiers in Java
    • Variable in Java
    Tags:javalanguage-fundamentals
    Was this article helpful?
    ← Previous ArticleRoute traffic from AWS Application Load Balancer to EC2 instance running in different VPC
    Next Article →Comments in Java

    Recent Posts

    • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
    • How to Use AWS CloudFront Signed URLs in Spring Boot?
    • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
    • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
    • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
    CoderSathi

    Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

    Linkedin

    Quick Links

    • About
    • Contact

    Popular Topics

    • Java
    • Spring Boot
    • AWS
    • DevOps
    • MongoDB
    • Linux
    • Git
    • How to
    © 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap