codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

7 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Data Types
☕Java Programming

Java Data Types

Updated 2026-05-12
30 min read

Java Data Types

In the world of programming, data types are fundamental building blocks that define the kind of data a variable can hold. Just like how you categorize your belongings into different boxes (e.g., clothes, books, electronics), Java categorizes its data into various types to efficiently manage memory and operations.

Understanding Java's data types is crucial because it affects how you store, manipulate, and process information in your programs. In this tutorial, we'll explore both primitive and non-primitive data types, their characteristics, and practical examples of when to use each.

Introduction

Java provides a rich set of data types that cater to different needs. These are divided into two main categories:

  1. Primitive Data Types: Basic data types provided by Java itself.
  2. Non-Primitive Data Types (Reference Types): More complex data types created using classes or interfaces.

Knowing the differences and appropriate uses of these types will help you write efficient and error-free code. Let's dive into each category in detail.

Primitive Data Types

Primitive data types are the most basic data types provided by Java. They are not objects, but they have corresponding wrapper classes that allow them to be used as objects when needed.

1. Integer Types

Integer types store whole numbers without any fractional part.

TypeSize (bits)Range
byte8-128 to 127
short16-32,768 to 32,767
int32-2,147,483,648 to 2,147,483,647
long64-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Example: Using Integer Types

IntegerTypes.java
1public class IntegerTypes {
2 public static void main(String[] args) {
3 byte b = 10;
4 short s = 1234;
5 int i = 123456;
6 long l = 1234567890L;
7
8 System.out.println("Byte: " + b);
9 System.out.println("Short: " + s);
10 System.out.println("Int: " + i);
11 System.out.println("Long: " + l);
12 };
13}
Output
Byte: 10
Short: 1234
Int: 123456
Long: 1234567890

2. Floating-Point Types

Floating-point types store fractional numbers.

TypeSize (bits)Range
float32Approximately ±3.4028235E+38F
double64Approximately ±1.7976931348623157E+308

Example: Using Floating-Point Types

FloatingPointTypes.java
1public class FloatingPointTypes {
2 public static void main(String[] args) {
3 float f = 12.34f;
4 double d = 123.456;
5
6 System.out.println("Float: " + f);
7 System.out.println("Double: " + d);
8 }
9}
Output
Float: 12.34
Double: 123.456

3. Character Type

The char type is used to store a single character.

TypeSize (bits)Range
char16Unicode characters

Example: Using Character Type

CharacterType.java
1public class CharacterType {
2 public static void main(String[] args) {
3 char c = 'A';
4
5 System.out.println("Char: " + c);
6 }
7}
Output
Char: A

4. Boolean Type

The boolean type is used to store true or false values.

TypeValues
booleantrue, false

Example: Using Boolean Type

BooleanType.java
1public class BooleanType {
2 public static void main(String[] args) {
3 boolean b = true;
4
5 System.out.println("Boolean: " + b);
6 }
7}
Output
Boolean: true

Non-Primitive Data Types

Non-primitive data types, also known as reference types, are created using classes or interfaces. They store references to objects in memory.

1. Strings

Strings are used to represent text and are a part of the java.lang package.

Example: Using Strings

StringExample.java
1public class StringExample {
2 public static void main(String[] args) {
3 String str = "Hello, World!";
4
5 System.out.println("String: " + str);
6 }
7}
Output
String: Hello, World!

2. Arrays

Arrays are used to store multiple values of the same type in a single variable.

Example: Using Arrays

ArrayExample.java
1public class ArrayExample {
2 public static void main(String[] args) {
3 int[] numbers = {1, 2, 3, 4, 5};
4
5 for (int number : numbers) {
6 System.out.println("Number: " + number);
7 }
8 }
9}
Output
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

3. Classes and Objects

Classes are blueprints for creating objects, which encapsulate data and behavior.

Example: Using Classes and Objects

ClassExample.java
1class Car {
2 String model;
3 int year;
4
5 public Car(String model, int year) {
6 this.model = model;
7 this.year = year;
8 }
9
10 public void displayInfo() {
11 System.out.println("Model: " + model);
12 System.out.println("Year: " + year);
13 }
14}
15
16public class ClassExample {
17 public static void main(String[] args) {
18 Car myCar = new Car("Toyota", 2020);
19
20 myCar.displayInfo();
21 }
22}
Output
Model: Toyota
Year: 2020

Practical Example

Let's create a simple program that calculates the average of three numbers using both primitive and non-primitive data types.

AverageCalculator.java
1public class AverageCalculator {
2 public static void main(String[] args) {
3 // Using primitive data types
4 int num1 = 10;
5 int num2 = 20;
6 int num3 = 30;
7
8 double averagePrimitive = (num1 + num2 + num3) / 3.0;
9 System.out.println("Average using primitives: " + averagePrimitive);
10
11 // Using non-primitive data types
12 Integer[] numbers = {10, 20, 30};
13
14 int sum = 0;
15 for (int number : numbers) {
16 sum += number;
17 }
18
19 double averageNonPrimitive = (double) sum / numbers.length;
20 System.out.println("Average using non-primitives: " + averageNonPrimitive);
21 }
22}
Output
Average using primitives: 20.0
Average using non-primitives: 20.0

Summary

Type CategoryTypes
Primitivebyte, short, int, long, float, double, boolean, char
Non-PrimitiveString, arrays, classes

Key Takeaways

  • Primitive types are basic data types provided by Java and have fixed sizes.
  • Non-primitive types (reference types) are more complex and are created using classes or interfaces.
  • Choose the appropriate data type based on the requirements of your program to optimize memory usage and performance.

What's Next?

Now that you understand the basics of Java data types, the next step is to learn about Java Type Casting, which allows you to convert one data type into another. This will help you manage different data types more effectively in your programs. Stay tuned for the next tutorial!


PreviousJava VariablesNext Java Type Casting

Recommended Gear

Java VariablesJava Type Casting