Skip to main content

Command Palette

Search for a command to run...

Arrays & Bitwise Operators in Java – A Complete Beginner Guide

Published
•4 min read•View as Markdown
Arrays & Bitwise Operators in Java – A Complete Beginner Guide

Java is not just about writing code — it’s about writing efficient, readable and logical code.
Two core concepts every Java developer must master early are:

  • Arrays

  • Bitwise Operators

This blog explains both from zero to interview level, with clear examples and internal working.


🔹 1. ARRAY IN JAVA

✅ What is an Array?

An array is a collection of similar data types stored in continuous memory locations, accessed using an index.

📌 Important rule
👉 Index always starts from 0


❓ Why Do We Need Arrays?

❌ Without Array

int a1 = 10;
int a2 = 20;
int a3 = 30;

Problems:

  • Too many variables

  • Hard to manage

  • Not scalable

✅ With Array

int[] a = {10, 20, 30};

✔ Less code
✔ Easy looping
✔ Better memory management
✔ Cleaner logic


🧠 How Array Works Internally

  • Array name → Reference variable

  • Actual values → Heap memory

  • Size is fixed once created ❌ (cannot grow/shrink)

📌 This is why arrays are fast but not flexible.


🧩 Array Declaration Methods

1️⃣ Declaration only

int[] a;

2️⃣ Declaration + size

a = new int[5];

3️⃣ Declaration + initialization

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

🔍 Accessing Array Elements

int[] a = {10, 20, 30};
System.out.println(a[0]); // 10

🔁 Looping an Array

🔹 Using for loop

for(int i = 0; i < a.length; i++) {
    System.out.println(a[i]);
}

🔹 Enhanced for-each loop

for(int x : a) {
    System.out.println(x);
}

📌 for-each is simple but index is not accessible.


🔹 2. TYPES OF ARRAYS IN JAVA

✅ 1️⃣ One-Dimensional Array

A simple list of values.

int[] a = {1, 2, 3};

Memory View

Index:  0   1   2
Value:  1   2   3

✅ 2️⃣ Two-Dimensional Array (Matrix)

Used for rows and columns.

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

Access:

System.out.println(m[1][0]); // 3

✅ 3️⃣ Multi-Dimensional Array

More than 2 dimensions (rare in real projects).

int[][][] a = new int[2][2][2];

Used mostly in scientific & research applications.


⭐ 4️⃣ Jagged Array (IMPORTANT)

Arrays with different column sizes.

int[][] a = new int[3][];

a[0] = new int[2];
a[1] = new int[3];
a[2] = new int[1];

Or directly:

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

✔ Saves memory
✔ Very common interview question
✔ Real-world friendly


🔹 3. BITWISE OPERATORS IN JAVA 🔥

Bitwise operators work on binary values (0s & 1s).

🔢 Binary Basics

5 = 0101
3 = 0011

✅ 1️⃣ Bitwise AND (&)

Both bits must be 1.

int a = 5, b = 3;
System.out.println(a & b); // 1
0101
0011
----
0001

✅ 2️⃣ Bitwise OR (|)

Any one bit is 1.

System.out.println(a | b); // 7

✅ 3️⃣ Bitwise XOR (^)

Different bits → 1.

System.out.println(a ^ b); // 6

🔥 XOR Tricks

x ^ x = 0
x ^ 0 = x

Used in swap without temp variable.


✅ 4️⃣ Bitwise NOT (~)

Inverts all bits.

int a = 5;
System.out.println(~a); // -6

📌 Formula:

~n = -(n + 1)

✅ 5️⃣ Left Shift (<<)

Shifts bits left → multiply by 2.

System.out.println(5 << 1); // 10

📌 Formula:

a << n = a × 2ⁿ

✅ 6️⃣ Right Shift (>>)

Shifts right → divide by 2 (keeps sign).

System.out.println(8 >> 1); // 4

⭐ 7️⃣ Unsigned Right Shift (>>>)

Always fills with 0, even for negative numbers.

int n = -8;
System.out.println(n >>> 1);

Binary (32-bit):

-8  = 11111111 11111111 11111111 11111000
>>>1
      01111111 11111111 11111111 11111100

✅ Output

2147483644

🔥 Interview favorite question!


📊 Bitwise Operator Summary Table

OperatorMeaning
&AND
OR
^XOR
~NOT
<<Left shift
\>>Right shift
\>>>Unsigned right shift