Courses / Complete Java Course by codesofrohan / Java Basics / Literals, Type Conversion & Type Casting in Java

Literals, Type Conversion & Type Casting in Java

In this lecture of the Complete Java Course by Codes of Rohan, we will learn three important concepts:

  • Literals in Java

  • Type Conversion

  • Type Casting

These concepts are important because while writing Java programs, we frequently assign values from literals or variables to other variables, and sometimes we need to convert one data type into another.


1. What Are Literals in Java?

Whenever we create a variable in Java, we generally use the following syntax:

int a = 4;

Here:

  • int → Data Type

  • a → Variable Name

  • = → Assignment Operator

  • 4 → Literal

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

For example:

int a = 4;

Here, 4 is an integer literal.

If we write 4 somewhere else in the program, its value is still 4. It does not suddenly become 5 or 6.

On the other hand, the value stored inside a variable can change.

int a = 4;

a = 5;
a = 6;

The variable a can contain different values at different points in the program. This is why it is called a variable.

Literal vs Variable

Consider these two examples:

int a = 4;

and:

int a = 4;
int b = a;

In the first line, 4 is a literal.

In the second line, we are not directly assigning a literal to b. Instead, we are using another variable, a.

Since a currently contains 4, the value assigned to b will also be 4.

a → 4
b → 4

So, remember:

A literal is a value written directly in the source code, while a variable is a named storage location whose value can change.


2. Real-World Example: Literals vs Variables

Let's understand this with a simple real-world example.

Suppose we are creating an application for calculating the price of sugar.

Assume the price of sugar is fixed at:

int sugarPrice = 500;

Here, 500 is directly written in our code, so 500 is a literal.

But the quantity of sugar purchased by a customer can change.

One customer might purchase:

3 kg

while another customer might purchase:

2 kg

The quantity therefore needs to be stored in a variable.

For example:

int quantity = 3;

Later, the value of quantity could be different depending on user input.

We could then calculate the total price:

int totalCost = sugarPrice * quantity;

The important point here is:

  • 500 is a value directly written in the program → Literal

  • quantity is a variable whose value can change → Variable

In later lectures, when we learn about user input, the value of quantity can come from the user instead of being directly written in the program.


3. Types of Literals in Java

Java provides different types of literals.

The important types covered in this lecture are:

  1. Integer Literals

  2. Floating-Point Literals

  3. Character Literals

  4. String Literals

  5. Boolean Literals

  6. Null Literal

Let's understand each one.


3.1 Integer Literals

An integer literal represents a whole-number value without a decimal point.

Examples:

10
25
100
-5
0

For example:

int age = 25;

Here:

25

is an integer literal.

Integer literals can represent positive numbers, negative numbers, and zero.


3.2 Floating-Point Literals

A floating-point literal represents a number containing a decimal point.

Examples:

1.321
6.213
34.632

For example:

double price = 34.632;

Here:

34.632

is a floating-point literal.

When using a float, Java normally requires the f suffix:

float xyz = 34.632f;

Here 34.632f is a floating-point literal of type float.


3.3 Character Literals

A character literal represents a single character and is written inside single quotes.

For example:

char a = '5';

Here, '5' is a character literal.

Notice the difference between:

int a = 5;

and:

char b = '5';

Although both visually contain 5, they represent different things.

5 vs '5'

5   → Integer value
'5' → Character

The single quotes tell Java that we are treating 5 as a character rather than as an integer value.

Similarly:

char letter = 'A';

Here 'A' is a character literal.


3.4 String Literals

A String literal represents a sequence of characters and is written inside double quotes.

Examples:

"Hello"
"Welcome to Codes of Rohan"

Both are String literals.

For example:

String txt = "I have 48 mangoes";

Here, the entire text is treated as a String.

Even though the text contains the number 48, that 48 is part of the String. It is not treated as an integer value separately.

So:

48

and:

"48"

are different.

48   → Integer literal
"48" → String literal

3.5 Boolean Literals

Java has two Boolean literals:

true

and:

false

For example:

boolean isLoggedIn = true;

Here, true is a Boolean literal.

Similarly:

boolean isAvailable = false;

Here, false is a Boolean literal.

There are only two possible Boolean values:

true
false

3.6 Null Literal

Java also has a special literal:

null

null represents the absence of an object reference.

For example:

String name = null;

Here, name does not currently refer to a String object.

The concept of null becomes more useful when we learn about classes, objects, and reference variables.

For now, remember:

null represents no object reference.


4. Type Conversion in Java

Now let's move to our second major topic:

Type Conversion.

To understand type conversion, imagine that we have two containers.

Suppose one container has a capacity of 1 litre, while another container has a capacity of 5 litres.

If the 1 litre of water from the smaller container is transferred into the 5-litre container, there is no problem because the larger container has enough space.

The same basic idea applies to data types.

For example, Java has a byte data type and an int data type.

A byte uses 1 byte of memory, while an int uses 4 bytes.

So we can generally move a byte value into an int variable:

byte a = 34;
int b = a;

This is possible because an int can represent a much larger range of integer values than a byte.

This type of conversion is called type conversion, commonly referred to as widening conversion or implicit conversion.


5. Widening Type Conversion

Widening conversion happens when a value is converted from a smaller compatible data type to a larger compatible data type.

For example:

byte a = 34;
int b = a;

The value stored in a can be assigned to b without explicitly telling Java to convert it.

Another example:

int a = 34;
float b = a;

The result will be:

34.0

For example:

int a = 34;
float b = a;

System.out.println(b);

Output:

34.0

No information is lost in this conversion.

The important idea is:

When Java can safely convert a smaller compatible type into a larger compatible type, explicit casting is generally not required.


6. Type Casting in Java

Now consider the opposite situation.

Suppose we have:

float xyz = 34.632f;

and we want to store it inside an int variable:

int abc = xyz;

Java will not allow this directly.

Why?

Because a float can contain a decimal portion, while an int cannot.

For example:

34.632

cannot be completely represented as an integer.

Therefore, Java requires us to explicitly tell it that we want this conversion.

This is called type casting.


7. Explicit Type Casting

We can explicitly cast a value using the following syntax:

int abc = (int) xyz;

Here:

(int)

is the cast.

Java first converts the value of xyz into an int, and then stores the resulting value in abc.

For example:

float xyz = 34.632f;

int abc = (int) xyz;

System.out.println(abc);

Output:

34

8. Data Loss During Type Casting

Type casting can result in data loss.

Consider:

float xyz = 34.632f;
int a = (int) xyz;

The value of xyz is:

34.632

After casting to int:

34

The .632 portion is removed.

Java does not round the number to the nearest integer.

For example:

float xyz = 34.999f;
int a = (int) xyz;

System.out.println(a);

Output:

34

Even though 34.999 is very close to 35, casting does not perform normal mathematical rounding.

Instead, the fractional portion is discarded.

Therefore:

34.632 → 34
34.999 → 34

This process is commonly described as truncation.


9. Conversion vs Casting

Let's compare both concepts.

Type Conversion

When Java can automatically convert a compatible value without requiring an explicit cast:

int a = 34;
float b = a;

Output:

34.0

This is a widening conversion.

Type Casting

When we explicitly tell Java to convert a value to another type:

float a = 34.632f;
int b = (int) a;

Output:

34

This is an explicit narrowing conversion.

Simple Difference

Type Conversion Type Casting
Usually automatic Explicitly performed
Often widening Often narrowing
Generally safer Can cause data loss
Example: int → float Example: float → int

10. Byte Overflow During Type Casting

Now let's look at an interesting example involving byte.

A Java byte can store values from:

-128 to 127

So what happens if we try to cast a larger integer into a byte?

Consider:

int a = 128;
byte b = (byte) a;

System.out.println(b);

Output:

-128

At first, this may look surprising.

Why does 128 become -128?

To understand this, we need to understand how a Java byte represents values.


11. Visualizing Byte Overflow

A byte contains 8 bits, giving us:

2⁸ = 256

possible bit patterns.

These 256 patterns represent values from:

-128 to 127

You can imagine these values as a circular sequence.

After reaching:

127

the next value wraps around to:

-128

Then:

-127
-126
-125
...

This is why converting:

int a = 128;
byte b = (byte) a;

produces:

-128

Similarly, if we cast:

int a = 129;
byte b = (byte) a;

the result becomes:

-127

The values wrap around because a byte has only 256 possible bit patterns.


12. Calculating Large Byte Overflow

Drawing the complete circular sequence is useful for understanding the concept, but it would be inconvenient for very large numbers.

For an 8-bit byte, the range repeats every:

256

So we can use modulo-style reasoning.

For example:

int a = 726;
byte b = (byte) a;

System.out.println(b);

Output:

-42

Why?

Because the byte keeps only the lower 8 bits of the integer representation.

Mathematically, we can reduce the value using the byte's 256-value range:

726 mod 256 = 214

The unsigned 8-bit representation of 214 corresponds to the signed byte value:

214 - 256 = -42

Therefore:

726 → 214 → -42

So the output is:

-42

This is an example of the kind of result that can occur when narrowing a larger integer type into a smaller integer type.


13. Not Every Type Can Be Cast to Every Other Type

Type casting is not universally possible between every pair of Java data types.

For example, consider:

boolean bool = false;
int ab = (int) bool;

This will result in an error because Java does not allow a boolean to be directly cast to an int.

The compiler will report an error similar to:

Cannot cast boolean to int

So, while Java allows many numeric conversions and casts, some type combinations are simply not compatible.


14. Quick Summary

Let's quickly revise everything we learned.

Literals

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

Examples:

10
10.5
'A'
"Hello"
true
null

Integer Literal

int a = 10;

10 is an integer literal.

Floating-Point Literal

float a = 10.5f;

10.5f is a floating-point literal.

Character Literal

char a = 'A';

'A' is a character literal.

String Literal

String a = "Hello";

"Hello" is a String literal.

Boolean Literal

boolean a = true;

true is a Boolean literal.

Null Literal

String name = null;

null represents the absence of an object reference.


Type Conversion

Type conversion generally refers to automatically converting a value from a smaller compatible type to a larger compatible type.

Example:

int a = 34;
float b = a;

Output:

34.0

Type Casting

Type casting is explicitly converting a value into another compatible type using the cast operator.

Example:

float a = 34.632f;
int b = (int) a;

Output:

34

The fractional part is discarded.


Byte Overflow

A Java byte has a range of:

-128 to 127

Casting a value outside this range can produce a wrapped result.

int a = 128;
byte b = (byte) a;

System.out.println(b);

Output:

-128

Boolean Casting

Not every data type can be cast to every other data type.

For example:

boolean bool = false;
int a = (int) bool;

is invalid in Java.


15. Key Takeaways

After completing this lecture, you should be able to:

  • Understand what a literal is.

  • Differentiate between a literal and a variable.

  • Identify different types of literals in Java.

  • Understand integer, floating-point, character, String, Boolean, and null literals.

  • Understand type conversion.

  • Understand widening/implicit conversion.

  • Understand type casting.

  • Understand narrowing/explicit conversion.

  • Understand why casting float to int can cause data loss.

  • Understand truncation during floating-point to integer casting.

  • Understand byte range and overflow during narrowing.

  • Understand why some data types, such as boolean and int, cannot be directly cast to each other.


Practice Examples

Try predicting the output of these examples before running them.

Example 1

int a = 4;
int b = a;

System.out.println(b);

Output:

4

Example 2

float a = 34.632f;
int b = (int) a;

System.out.println(b);

Output:

34

Example 3

int a = 34;
float b = a;

System.out.println(b);

Output:

34.0

Example 4

int a = 128;
byte b = (byte) a;

System.out.println(b);

Output:

-128

Example 5

int a = 726;
byte b = (byte) a;

System.out.println(b);

Output:

-42

What's Next?

In the next lecture of the Complete Java Course, we will learn about Operators in Java and understand how operators are used to perform different types of operations in Java programs.

Watch the complete video above to see these concepts explained visually with live coding examples in VS Code.

🔖 Bookmark saved successfully!