CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Data 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

Data 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 · 2 min read · 0 Comments
Share: in X

Java is a strongly-typed language, meaning every variable must be declared with a data type. This ensures type safety, which prevents unintended behavior in your programs. In this post, we’ll explore the different types of data in Java, their use cases, and some example code to get you started.

Table of Contents

  • Types of Data in Java
    • Primitive Data Types
    • Non-Primitive (Reference/User Defined) Data Types
      • Use Case
      • Example Code
  • Conclusion

Types of Data in Java

Java data types are divided into two main categories:

Primitive Data Types

These are the most basic data types and directly represent data values. Java has 8 primitive data types:

  • byte: 1 byte, range -128 to 127
  • short: 2 bytes, range -32,768 to 32,767
  • int: 4 bytes, range -2,147,483,648 to 2,147,483,647
  • long: 8 bytes, range -2^63 to 2^63-1
  • float: 4 bytes, single-precision 32-bit floating point
  • double: 8 bytes, double-precision 64-bit floating point
  • char: 2 bytes, a single 16-bit Unicode character
  • boolean: 1 bit, values true or false

Non-Primitive (Reference/User Defined) Data Types

These include objects, arrays, interfaces, and strings. Non-primitive or User Defined Data types are created by the programmer and can hold multiple values or more complex data structures.

Use Case

Suppose you’re creating a simple Java program to calculate the area of a rectangle. You’ll need to declare variables for the length and width of the rectangle and calculate the area using an appropriate data type.

Example Code

Following is an example to demonstrate the use of primitive data types in calculating the area of a rectangle:

public class RectangleArea {
    public static void main(String[] args) {
        // Declare variables
        double length = 5.5; // Length of the rectangle
        double width = 3.2;  // Width of the rectangle

        // Calculate area
        double area = length * width;

        // Print the result
        System.out.println("The area of the rectangle is: " + area);
    }
}

In this example:

  • The variables length and width are declared as double to accommodate decimal values.
  • The area is calculated and printed using the System.out.println statement.

Conclusion

Understanding data types in Java is fundamental for writing efficient and error-free code. Choosing the right data type ensures optimal memory usage and program performance. Whether you’re dealing with simple numbers or complex objects, Java’s data types provide the flexibility to handle a wide variety of tasks.

Related Posts:

  • Primitive data types in Java
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Control Statements in Java
  • User Defined Data Type in Java
  • MySQL Commands for Developers
  • User Defined Exception in Java
Tags:java-tutoriallanguage-fundamentals
Was this article helpful?
← Previous ArticleHow to Define FeignClient URL in Spring Boot for Local Testing
Next Article →How to Install Ollama on Windows, Linux, and macOS: A Step-by-Step Guide

Leave a Comment Cancel reply

You must be logged in to post a comment.

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