Java Primitive Types: int, long, float and Number Types

Java primitive types are the eight built-in data types defined by the Java Language Specification: byte, short, int, long, float, double, char and boolean. Unlike objects, they are stored directly in memory, have fixed sizes and ranges, and carry no object overhead, making them the fastest and most memory-efficient way to store simple values in any Java program.

  • Java has exactly 8 primitive types, defined by the Java Language Specification and unchanged since Java 1.0.
  • Choosing the wrong numeric type causes overflow, a silent bug that does not throw an error but produces completely wrong results.
  • float is 32 bits; double is 64 bits. Use double for most decimal calculations unless memory is a hard constraint.
  • int and Integer are not the same thing. int is a primitive; Integer is a wrapper class in the java.lang package.
  • long can hold values up to 9,223,372,036,854,775,807, making it the right choice for timestamps, large IDs and financial calculations in rupees at scale.

Before diving deeper, if you are just starting out it helps to have the right environment set up. The software development tools to learn first guide walks you through exactly that.

The 8 Primitive Types in Java

The 8 primitive types in Java fall into four categories: integer types, floating-point types, a character type and a boolean type. The Oracle Java documentation formally defines them as the foundation of the type system. Every other type in Java, including String and arrays, is an object built on top of these primitives. Understanding java primitive types is non-negotiable for placements at companies like TCS, Infosys and Wipro, and for any BSc Computer Science subjects and scope programming exam.

Here is the complete reference table with sizes and value ranges, straight from the Oracle Java SE documentation:

Type Size (bits) Min Value Max Value Default Value
byte 8 -128 127 0
short 16 -32,768 32,767 0
int 32 -2,147,483,648 2,147,483,647 0
long 64 -9,223,372,036,854,775,808 9,223,372,036,854,775,807 0L
float 32 ~1.4e-45 ~3.4e+38 0.0f
double 64 ~4.9e-324 ~1.8e+308 0.0d
char 16 ‘\u0000’ (0) ‘\uffff’ (65,535) ‘\u0000’
boolean 1 (JVM-dependent) false true false

According to the Stack Overflow Developer Survey 2024, Java ranks among the top five most-used programming languages globally, and it remains the dominant language taught in Indian engineering colleges. The TIOBE Index (April 2025) still places Java in the top three most popular programming languages worldwide, driven largely by enterprise systems and Android development. According to the JetBrains Developer Ecosystem Survey 2023, Java is used by 48% of professional developers surveyed, making it one of the most commercially relevant languages to master.

Why Size Matters: The Overflow Problem

Overflow is one of the most common silent bugs beginners introduce when working with java primitive types. If you store the value 2,147,483,648 in an int, Java does not throw an exception. It wraps around and gives you -2,147,483,648 instead. That kind of bug in a billing system or a marks-calculation module causes real damage.

The practical rule: start with int for whole numbers, upgrade to long the moment your values could exceed 2.1 billion. For decimal values, default to double unless you have a specific reason to use float. If you are curious about what happens when things go wrong at runtime, the Java exceptions and common runtime errors guide covers exactly that.

Integer Types: int and long

java int is the default integer type and the one you will use in 90% of everyday programming. It is 32 bits, holds values from roughly -2.1 billion to +2.1 billion, and arithmetic on it is fast on every modern JVM. When you write int score = 95; in a student grade application, Java allocates exactly 4 bytes on the stack.

What Is long in Java?

java long is a 64-bit signed integer. You declare it with an L suffix: long aadhaarNumber = 123456789012L;. That suffix matters. Without it, Java treats the literal as an int first, and if the value exceeds int’s range, you get a compile-time error before the program even runs.

Long is the right choice for Aadhaar numbers (12 digits), Unix timestamps, database primary keys in large systems and any financial figure that counts paise across millions of transactions. These are real-world scenarios every Indian Java developer encounters in enterprise and fintech projects.

Common Mistake With long Literals

New developers frequently write long population = 1400000000; and wonder why it compiles but produces a warning or unexpected behaviour. The literal 1400000000 fits inside an int, so Java accepts it, but if you write long bigNumber = 9999999999; without the L, the compiler throws an error because 9999999999 overflows int before the assignment even happens. Always append L to long literals that exceed 2.1 billion.

Floating-Point Types

Java’s two floating-point types are float and double, both following the IEEE 754 standard. The java float size is 32 bits, giving you about 6 to 7 significant decimal digits of precision. Double is 64 bits and gives you 15 to 16 significant digits. Choosing the right one is a core skill when working with java primitive types in real applications.

float vs double: Which One Should You Use?

The short answer is: use double by default. Java itself treats all floating-point literals as double unless you add an f suffix. Writing float price = 49.99; gives a compile error; you need float price = 49.99f;. That extra step is a deliberate reminder that you are choosing lower precision.

The float value in Java is precise enough for graphics coordinates, sensor readings and situations where memory is tight, such as large float arrays in Android development. For anything involving money, GST calculations or scientific data, always use double or the BigDecimal class. BigDecimal avoids the rounding errors that make 0.1 + 0.2 return 0.30000000000000004 in both float and double.

Types of Number in Java: The Full Picture

When people ask about types of number in Java, they usually mean more than just primitives. Java’s number story has three layers. First, the primitive numeric types: byte, short, int, long, float and double. Second, the wrapper classes: Byte, Short, Integer, Long, Float and Double, all of which extend the abstract Number class in java.lang. Third, the arbitrary-precision classes: BigInteger and BigDecimal, which are essential when you cannot afford any rounding error at all.

A java number in the wrapper sense gives you utility methods like Integer.parseInt(), Double.compare() and Long.toBinaryString(). You cannot call methods on a primitive, which is why wrapper classes exist. Understanding this distinction early saves a lot of confusion when you start working with Collections, generics and APIs.

char and boolean: The Non-Numeric Primitives

The char primitive stores a single Unicode character in 16 bits. Its range covers Unicode code points from ‘\u0000’ (0) to ‘\uffff’ (65,535), which means it can represent characters from virtually every major script including Devanagari, Tamil and Bengali. The boolean primitive holds only two values: true or false. Its actual memory footprint is JVM-dependent, but it is logically treated as 1 bit. Both types are part of the complete set of java primitive types and appear constantly in control flow, string processing and flag variables.

int vs Integer in Java

This is one of the most common interview questions for freshers, and the distinction is genuinely important. int is a Java primitive. It holds a raw 32-bit value directly in memory, no object overhead. Integer is a class in java.lang that wraps an int value inside an object.

Key Differences at a Glance

Feature int (primitive) Integer (wrapper class)
Memory 4 bytes (stack) 16+ bytes (heap, object overhead)
Default value 0 null
Can be null? No Yes
Used in Collections? No (auto-boxed to Integer) Yes
Methods available? No Yes (parseInt, valueOf, etc.)
Performance Faster Slower (boxing/unboxing cost)

Autoboxing and When It Bites You

Java automatically converts between int and Integer through a process called autoboxing (primitive to object) and unboxing (object to primitive). This feels convenient until you hit a NullPointerException because an Integer variable was null when Java tried to unbox it to int. That is a real bug that shows up in production code and can be tricky to trace.

On the job, you will use int in loops, calculations and local variables. You will use Integer when working with Java Collections like ArrayList<Integer>, when a value might legitimately be null (optional fields in a database row, for example) or when you need Integer’s utility methods. Mixing them carelessly is one of the most common sources of Java exceptions and common runtime errors in beginner Java code.

If you are building a career around Java or planning to specialise in full-stack or backend development, understanding these fundamentals puts you ahead of most candidates. You can compare career paths in the full stack developer vs software developer guide to see where Java fits.

Students coming from a C background will find the type system familiar but stricter. If you have already worked through how to write, compile and run a C program, you will notice Java’s types map closely to C’s but add the wrapper-class layer on top. MCA students looking at career options after their degree can check the jobs and salary after MCA page, where Java developer roles feature prominently.

Frequently Asked Questions

What are the 8 primitive types in Java?

The 8 primitive types in Java are byte, short, int, long, float, double, char and boolean. They are defined by the Java Language Specification and are not objects. Each has a fixed size and range, and they are stored directly on the stack rather than the heap, making operations on them faster than on objects.

What is the size of float in Java?

A float in Java is 32 bits (4 bytes) and follows the IEEE 754 single-precision standard. It can represent approximately 6 to 7 significant decimal digits. Because of its limited precision, most Java developers prefer double (64 bits) for decimal calculations unless they are working in memory-constrained environments like Android or large numeric arrays.

What is the difference between int and Integer in Java?

int is a primitive type that holds a 32-bit integer value directly in memory and cannot be null. Integer is a wrapper class in java.lang that wraps an int inside an object, can be null, and provides utility methods like parseInt(). You must use Integer (not int) when working with Java Collections, generics or any API that requires an object type.

What is long in Java?

long is a 64-bit signed integer primitive in Java. It holds values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. You declare a long literal with an L suffix, for example long id = 9876543210L;. It is the right choice for large IDs, Unix timestamps, Aadhaar numbers and any whole-number value that could exceed int’s 2.1 billion limit.

What number types does Java have?

Java has six numeric primitive types: byte, short, int, long, float and double. It also has wrapper classes for each (Byte, Short, Integer, Long, Float, Double), all extending the abstract Number class. For arbitrary-precision work without rounding errors, Java provides BigInteger and BigDecimal in the java.math package.

Why should I learn java primitive types before moving to advanced topics?

Java primitive types are the foundation of every Java program. Overflow bugs, NullPointerExceptions from unboxed Integers and precision errors from float all trace back to not knowing these types deeply. Mastering them first makes Collections, generics, APIs and frameworks significantly easier to understand and debug.

Getting java primitive types right is the foundation everything else builds on. Overflow bugs, NullPointerExceptions from unboxed Integers and precision errors from float all trace back to not knowing these types deeply. Here are three things you can do this week to lock this knowledge in:

  1. Write a small Java program that deliberately causes int overflow, then fix it by switching to long. Print both results and compare them. Seeing it happen makes the lesson stick.
  2. Practise the int vs Integer distinction by creating an ArrayList<Integer>, adding values, then trying to assign one element to a plain int variable. Watch autoboxing happen and then introduce a null to see the NullPointerException in action.
  3. Explore what is next. Strong programming fundamentals are the entry point for ethical hacking, secure coding, AI development and blockchain work. If you want to turn this knowledge into a career, check out 3.0 University’s full catalogue of online certification courses or start directly with the CEH v13 AI online course, where understanding how data types and memory work gives you a real edge in vulnerability analysis and secure code review.

Last updated: May 2025. Reviewed by the 3University editorial team.

We create, review, and maintain educational content covering Cybersecurity, Ethical Hacking, Artificial Intelligence, Blockchain, Web3, and emerging technologies.

Read More Articles