Data Types in Java: Primitive and Non-Primitive with Ranges

Data types in Java define what kind of value a variable can hold, how much memory to reserve, and what operations are allowed on it. Java has 8 primitive data types — byte, short, int, long, float, double, char, and boolean — plus non-primitive types like String, arrays, and classes that store references to objects rather than values directly.

Java’s type system is statically typed, meaning every variable must be declared with a type before use. There are exactly 8 primitive types built into the language, each with a fixed size and a defined range, and a set of non-primitive (reference) types that store memory addresses pointing to heap-allocated objects.

  • Java has 8 primitive data types: byte, short, int, long, float, double, char, and boolean.
  • Primitive types have fixed memory sizes and ranges defined by the Java Language Specification, regardless of the operating system.
  • Non-primitive types like String, arrays, and classes store references, not values, so they can be null.
  • Choosing the wrong type is one of the most common causes of integer overflow bugs in production Java code.
  • String is not a primitive in Java; it’s a class in the java.lang package, even though it looks and behaves like one in many situations.

What a Data Type Is

A data type is a classification that specifies what kind of value a variable can hold and what operations can be performed on it. In Java, every variable must be declared with a type before it’s used. This is called static typing, and it’s one of the reasons Java catches so many errors at compile time rather than at runtime.

Think of it this way: if you’re building a student marks tracker for a BTech programme, you’d store roll numbers as int, percentage scores as double, grade letters as char, and student names as String. Using the wrong type wastes memory, causes precision errors, or breaks the program entirely. According to the Stack Overflow Developer Survey 2024, Java remains one of the top five most-used programming languages globally, which makes getting its fundamentals right genuinely valuable.

Java’s type system is also platform-independent. An int is always 32 bits whether you run your program on a Windows laptop in Mumbai or a Linux server in Bengaluru. That guarantee comes from the JVM, not the hardware. If you’re studying BSc Computer Science subjects, data types are usually one of the first Java topics you’ll cover, and for good reason.

Java’s dominance in the Indian job market is well-documented: according to the JetBrains State of Developer Ecosystem 2024, Java is used by 48% of professional developers surveyed worldwide, and it consistently ranks as the most-requested language in Indian IT hiring portals for backend and enterprise roles.

Eight Primitive Data Types and Ranges

The 8 primitive data types in Java are the building blocks of every Java program. Each one is stored directly on the stack, which makes access fast. None of them are objects, so they don’t have methods, and they can’t be null.

Here’s the complete reference table with sizes, default values, and ranges, sourced from the Oracle Java Documentation:

Type Size Default Value Minimum Value Maximum Value Use Case
byte 8 bits 0 -128 127 Raw binary data, file streams
short 16 bits 0 -32,768 32,767 Saving memory in large arrays
int 32 bits 0 -2,147,483,648 2,147,483,647 General-purpose integers, loop counters
long 64 bits 0L -9,223,372,036,854,775,808 9,223,372,036,854,775,807 Timestamps, Aadhaar-scale IDs
float 32 bits 0.0f ~1.4e-45 ~3.4e+38 Graphics, approximate decimals
double 64 bits 0.0d ~4.9e-324 ~1.8e+308 Scientific calculations, financial figures
char 16 bits ‘\u0000’ 0 (\u0000) 65,535 (\uffff) Single Unicode characters
boolean JVM-defined false false true Flags, conditionals

int Data Type in Java

The int data type is the default choice for whole numbers in Java. Its range runs from -2,147,483,648 to 2,147,483,647, which covers most everyday counting tasks. When you write int marks = 95; in your code, Java stores that value in 32 bits on the stack.

One practical mistake beginners make: they use int to store phone numbers or Aadhaar numbers. A 12-digit Aadhaar number is 999,999,999,999, which is well beyond int’s maximum. That causes a silent overflow and produces a completely wrong value. Use long for anything that could exceed about 2.1 billion. Indian fintech applications processing UPI transaction IDs or GSTIN-linked records face exactly this issue when developers default to int without checking the range.

short Data Type in Java

The short data type holds 16-bit signed integers, ranging from -32,768 to 32,767. It’s rarely used in modern Java because the memory savings are minimal unless you’re working with very large arrays of small integers, like pixel values in image processing. In most everyday code, int is a safer and more readable choice.

char Data Type in Java

To define the character data type in Java: char stores a single Unicode character using 16 bits. That means it can represent any character from ‘\u0000’ (null) to ‘\uffff’, which covers the entire Basic Multilingual Plane, including Hindi Devanagari script characters. You declare it with single quotes: char grade = ‘A’;. Unlike C, Java’s char is unsigned, so it can’t hold negative values.

float vs double: A Common Source of Bugs

Both float and double store decimal numbers, but double is twice as precise. In practice, always prefer double unless you have a specific reason to use float, like GPU memory constraints in graphics code. A float literal needs the suffix f, so you’d write float price = 199.99f;. Without the f, Java treats the literal as a double and won’t compile it as a float assignment.

Understanding how Java handles runtime errors tied to these types, like NumberFormatException, is covered in detail in our guide to Java exceptions and common runtime errors.

Non-Primitive Data Types in Java

Non-primitive data types in Java, also called reference types, don’t store a value directly. They store a memory address that points to where the actual data lives on the heap. This distinction matters a lot: two variables can point to the same object, and modifying one affects the other.

The main non-primitive types in Java are String, arrays, classes, and interfaces. Each one is created using the new keyword (though String has a shortcut syntax), and each one can hold a value of null, meaning it points to nothing. That’s why you’ll see NullPointerException in Java programs when someone tries to use a reference that was never assigned an object.

String Data Type in Java

String is a class in the java.lang package, not a primitive. That said, Java gives it special treatment: you can create a String without the new keyword, like String name = “Priya”;, and the compiler handles the object creation behind the scenes using the String pool. The TIOBE Index (June 2025) ranks Java consistently in the top three programming languages, and String manipulation is cited as one of the most common Java interview topics across Indian product and service companies.

Because String is an object, it comes with methods like .length(), .toUpperCase(), and .substring(). A char array and a String are not the same thing, even though both deal with text. String objects are also immutable in Java, meaning once created, their content can’t be changed. Any operation that looks like it modifies a String actually creates a new one.

Arrays

An array in Java is a fixed-size, ordered collection of elements of the same type. You declare one like this: int[] scores = new int[5];. Arrays are objects in Java, stored on the heap, and they have a .length property. You can have arrays of primitives or arrays of objects, but you can’t mix types in a single array.

One thing that trips up beginners: array size is fixed at creation. If you need a resizable list, you’d use Java’s ArrayList from the Collections framework, which is a separate topic worth its own deep dive.

Classes as Data Types

Every class you define in Java is itself a data type. When you create a class called Student with fields for name, roll number, and marks, you’re defining a new type. You then declare variables of that type: Student s1 = new Student();. This is the foundation of object-oriented programming in Java, and it’s what separates Java from simpler scripting languages.

If you’re coming from a C background, the shift in thinking is significant. Our guide on how to write, compile and run a C program can help you see the contrast between C’s type system and Java’s.

Choosing the Right Data Type

Picking the right data type isn’t just an academic exercise. It directly affects your program’s memory usage, speed, and correctness. Here’s a practical decision guide:

  • Use int for whole numbers that fit within roughly plus or minus 2.1 billion (loop counters, ages, marks).
  • Use long when your integer could exceed that range (timestamps in milliseconds, large IDs, transaction amounts in paise).
  • Use double for any decimal calculation where precision matters, like currency conversion or scientific data.
  • Use boolean for any true/false flag, like isLoggedIn or hasPermission.
  • Use char only for single characters. For text, always use String.
  • Use byte when reading raw binary data from files or network streams.

On the job, you’ll often see developers use Integer, Double, and Boolean (with capital letters) instead of primitives. These are wrapper classes that let you use primitives in collections and handle null values. The choice between primitive and wrapper type is a common Java interview question, especially for roles in full-stack development. If you’re weighing your career options, the comparison between a full stack developer vs software developer role is worth reading before you specialise.

Strong type discipline also matters in security-sensitive code. If you’re storing cryptographic keys or session tokens, choosing the wrong type or allowing null references can introduce vulnerabilities. This is one reason why developers moving into ethical hacking or secure coding need solid programming fundamentals, not just tool knowledge. Professionals exploring the software development tools to learn first will find that understanding types is a prerequisite for almost everything else.

If you’re a BCA or MCA student trying to figure out where Java fits in your career, check what jobs and salaries look like after MCA in India right now. Java remains a dominant hiring requirement across product companies and service firms alike.

Here’s the key point: get your data types right early, and you’ll avoid an entire class of bugs that are genuinely hard to debug later. Your next steps this week:

  1. Open your IDE and write a short Java program that declares one variable of each of the 8 primitive types, prints them, and deliberately causes an integer overflow to see what actually happens.
  2. Read the Oracle Java primitive types documentation to understand the exact JVM specifications behind each type.
  3. Explore structured learning paths at 3.0 University’s online certification courses to build on these fundamentals, especially if your goal is secure coding, AI development, or ethical hacking. The CEH v13 AI course is a strong next step once you’re comfortable with programming basics.

Frequently Asked Questions

What are the data types in Java?

Data types in Java define what kind of value a variable can hold and how much memory it uses. Java has two main categories: primitive data types (byte, short, int, long, float, double, char, boolean) and non-primitive data types (String, arrays, classes, interfaces). Every variable in Java must be declared with a specific type before use. Learn more about how these fit into broader programming study in our BSc Computer Science subjects guide.

What are the 8 primitive data types in Java?

The 8 primitive data types in Java are byte, short, int, long, float, double, char, and boolean. Each is defined by the Java Language Specification with a fixed size in bits and a specific value range. They store values directly in memory and are not objects, so they can’t be null.

What are non-primitive data types in Java?

Non-primitive data types in Java store references to objects rather than the values themselves. The main examples are String, arrays, and user-defined classes. They’re created on the heap, can be null, and come with methods you can call. Unlike primitives, their memory size isn’t fixed by the language spec and depends on the object they reference.

What is the range of int in Java?

The int data type in Java is a 32-bit signed integer with a range of -2,147,483,648 to 2,147,483,647. If a calculation exceeds this range, Java wraps around silently without throwing an error, which can cause hard-to-spot bugs. For values beyond this range, use the long data type, which supports up to roughly 9.2 quintillion.

Is String a data type in Java?

Yes, String is a data type in Java, but it’s a non-primitive type, specifically a class in the java.lang package. It’s not one of the 8 primitives. Java gives String special syntax support so you can write String name = “Arjun”; without using the new keyword explicitly, but behind the scenes it’s still an object with methods and heap allocation. For related runtime behaviour, see our guide on Java exceptions and common runtime errors.

Last updated: June 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