Courses / Complete Java Course by codesofrohan / Java Basics / Java Data Types Range, Different ways of Printing & Text Blocks

Java Data Types Range, Different ways of Printing & Text Blocks

When we start writing Java programs, one of the first things we need to understand is how Java stores different types of values.

A program can work with numbers, characters, decimal values, true/false conditions, and text. Java provides different data types for these different kinds of information.

In this lesson, we will take a practical look at Java's primitive data types, their minimum and maximum values, literals, and multi-line text using Java text blocks.

Understanding these concepts is important because choosing the correct data type affects how much memory your program uses and what kind of values it can store.


What Are Data Types in Java?

A data type tells Java what kind of value a variable is expected to store.

For example:

int age = 25;

Here, int tells Java that age is an integer.

Similarly:

float price = 99.99f;

Here, float tells Java that price is a floating-point number.

Java's primitive data types are:

Data Type Used For
byte Small whole numbers
short Small-to-medium whole numbers
int Whole numbers
long Very large whole numbers
float Decimal numbers
double More precise decimal numbers
char A single character
boolean true or false

For example, if we are creating a program for a shopping application, we might use:

int quantity = 5;
float price = 149.99f;
char category = 'A';
boolean available = true;

Each variable stores a different type of information.


Integer Data Types and Their Limits

Java provides four primitive integer data types:

byte
short
int
long

The main difference between them is the amount of memory they use and the range of values they can store.

byte

A byte occupies 8 bits and can store values from:

-128 to 127

For example:

byte age = 25;

But:

byte value = 128;

will cause a compilation error because 128 is outside the valid range of byte.

short

A short occupies 16 bits and can store:

-32,768 to 32,767

Example:

short population = 30000;

int

An int occupies 32 bits and is the most commonly used integer data type.

Its range is:

-2,147,483,648 to 2,147,483,647

For example:

int salary = 50000;

long

A long occupies 64 bits and can store much larger integer values.

Its range is:

-9,223,372,036,854,775,808
to
9,223,372,036,854,775,807

When writing a large long literal, we can use the L suffix:

long population = 8000000000L;

The L tells Java that the number should be treated as a long literal.


How Can We Check the Maximum and Minimum Values?

Java provides constants through wrapper classes that allow us to check the boundaries of primitive numeric types.

For example:

System.out.println(Byte.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);

System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);

System.out.println(Long.MIN_VALUE);
System.out.println(Long.MAX_VALUE);

For floating-point types, we can use:

System.out.println(Float.MIN_VALUE);
System.out.println(Float.MAX_VALUE);

System.out.println(Double.MIN_VALUE);
System.out.println(Double.MAX_VALUE);

Notice the correct syntax:

Float.MAX_VALUE

not:

float.max value

Primitive types such as float and int are written in lowercase when declaring variables, but their corresponding wrapper classes use names such as Float and Integer when accessing constants.


Important Difference: Float.MIN_VALUE

There is an interesting detail about floating-point limits.

Float.MAX_VALUE represents the largest finite positive float value.

However, Float.MIN_VALUE does not mean the most negative float.

Instead, it represents the smallest positive non-zero float value.

For the most negative finite value, you can use:

-Float.MAX_VALUE

This distinction is important when working with floating-point values.


Floating-Point Data Types

Java provides two primitive data types for decimal numbers:

float
double

A float uses 32 bits, while a double uses 64 bits.

For example:

float temperature = 36.5f;
double distance = 12345.6789;

When assigning a decimal literal to a float, we generally use the f suffix:

float price = 99.99f;

Without the suffix:

float price = 99.99;

Java treats 99.99 as a double literal by default, which cannot be directly assigned to a float without an explicit conversion.


What Are Literals in Java?

A literal is a fixed value directly written in the source code.

For example:

int age = 25;

Here:

25

is an integer literal.

Similarly:

float price = 99.99f;

contains a floating-point literal.

Some common literal examples are:

int number = 100;
long bigNumber = 100000L;
float decimal = 10.5f;
double preciseValue = 10.5;
char letter = 'R';
boolean result = true;
String name = "Rohan";

The value itself is a literal, while the variable is used to store that value.


Character Literals and String Literals

One common beginner mistake is confusing char and String.

A char stores a single character:

char letter = 'R';

Single quotes are used for character literals.

A String stores text:

String name = "Rohan";

Double quotes are used for strings.

So:

char letter = 'A';

is valid.

But:

char letter = "A";

is not valid because "A" is a String, not a char.

Similarly:

String name = "Rohan";

is valid, while:

String name = 'Rohan';

is not valid.


Multi-Line Text in Java

Normally, when we want to store text in Java, we use a String:

String message = "Hello World";

If the text becomes large or needs to span multiple lines, writing it using normal string syntax can become inconvenient.

Modern Java provides a feature called a text block for this purpose.

A Java text block uses three double quotes:

String message = """
        Hello
        World
        Welcome to Java
        """;

The syntax is:

"""
multiple lines of text
"""

Important Correction

You may sometimes see or hear three single quotes being mentioned for multi-line text. That is not Java text block syntax.

Java text blocks require:

"""

Three single quotes:

'''

are not valid Java text block delimiters and will result in a compilation error.

Single quotes in Java are reserved for character literals, such as:

char letter = 'A';

Why Are Text Blocks Useful?

Suppose we want to print an HTML structure from Java.

Without a text block, the String can become difficult to read:

String html = "<html>\n" +
              "  <body>\n" +
              "    <h1>Hello</h1>\n" +
              "  </body>\n" +
              "</html>";

With a text block, the same content is much easier to read:

String html = """
        <html>
            <body>
                <h1>Hello</h1>
            </body>
        </html>
        """;

The second version looks much closer to the actual text we want to represent.

Text blocks are especially useful when working with:

  • HTML

  • JSON

  • SQL queries

  • XML

  • formatted messages

  • configuration-style text


Why Do Data Type Limits Matter?

Understanding the range of a data type becomes especially important when working with real-world applications.

Imagine an application that stores the number of products in a warehouse:

byte quantity = 120;

At first, this might work because 120 fits inside the byte range.

But if the warehouse grows and the quantity becomes:

500

a byte can no longer store that value.

In such a situation, using an int would be more appropriate:

int quantity = 500;

Similarly, if an application needs to store extremely large integer values, long may be more appropriate.

The important lesson is that a data type should be selected according to the value and requirements of the program.


Quick Reference

Type Size Approximate/Exact Range
byte 8-bit -128 to 127
short 16-bit -32,768 to 32,767
int 32-bit -2³¹ to 2³¹ - 1
long 64-bit -2⁶³ to 2⁶³ - 1
float 32-bit Floating-point values
double 64-bit Floating-point values
char 16-bit \u0000 to \uFFFF
boolean true or false

For exact boundary constants, Java provides wrapper-class constants such as:

Integer.MIN_VALUE
Integer.MAX_VALUE

Float.MAX_VALUE
Double.MAX_VALUE

Long.MIN_VALUE
Long.MAX_VALUE

Key Takeaways

By the end of this lesson, you should understand:

  • Java has different primitive data types for different kinds of values.

  • byte, short, int, and long are used for integer values.

  • float and double are used for floating-point values.

  • char stores a single character.

  • boolean stores either true or false.

  • Every numeric type has a defined range.

  • Boundary constants can be accessed through wrapper classes such as Integer.MAX_VALUE and Float.MAX_VALUE.

  • Float.MIN_VALUE represents the smallest positive non-zero float, not the most negative value.

  • A literal is a fixed value written directly in Java source code.

  • char uses single quotes, while String uses double quotes.

  • Java text blocks use three double quotes ("""), not three single quotes.

  • Text blocks are useful for representing multi-line content such as HTML, JSON, and SQL.

Understanding these fundamentals will make it much easier to work with Java variables and values as we move toward more advanced programming concepts.

🔖 Bookmark saved successfully!