INTRODUCTION
In today's free Java lesson, we will stay on Integer Data Types to take a detailed look at the Long data type.
We will also see how we can convert a variable from one data type to another (e.g., from int to Long and vice versa) using the casting process. Finally, we will learn how to define constants—variables whose initial value cannot be changed—and explore what type inference is.
DEFINING A LONG DATA TYPE
As we mentioned in the previous free Java lesson, long
is a 64-bit Java primitive data type. It is typically used when we need to work with very large integer numbers that cannot be represented using the 32-bit int
data type.
The reason we are focusing on this data type is that, when it comes to integral data types, Java uses int
as the default. Simply put, this means that Java tries to interpret all integer numbers as int
by default. Therefore, to declare a long
data type, it is not enough to just specify long
as the data type; we also need to append the letter l
or L
at the end of the number. Since the lowercase l
can be easily mistaken for the number 1
, it is common practice to use the uppercase L
.
long num = 123456789L;
Let's take a look at a simple example where we use variables with long
data types. After creating a class and adding the main()
method, we define a variable named spaceshipSpeed
. The program calculates how many kilometers the spaceship will travel after three years.
Here’s the Java code for this example:
App.java
Output
If you want to find out how large a long
number can be, you can assign the value Long.MAX_VALUE
to a long
variable and print its value in the terminal. Similarly, you can use Long.MIN_VALUE
to determine the lowest possible value a long
number can have.
Let's add these two variables to our program.
App.java
Output
CASTING
In programming, it is very common to assign the value of one variable to another. If the data types of the two variables are compatible, Java will automatically perform the conversion from one data type to another. For example, an int
variable can assign its value to a long
variable without requiring any intervention from us. This automatic process is called widening conversion, and it is possible because an int
variable, which refers to a 32-bit number, can fit and be accurately represented by a 64-bit long
variable without losing precision.
However, there are cases where we want to perform the exact opposite operation—i.e., assign a long
variable to an int
variable. In this case, Java does not automatically handle the conversion because it could distort the precision of the result. However, we can manually perform the narrowing conversion by using casting.
With casting, we specify the data type we want to convert a number to before assigning it to the variable. The syntax for performing a cast is simple—inside parentheses, we write the desired data type, followed by the variable name whose value we want to convert.
Let's look at a simple example to better understand the usefulness and functionality of casting.
App.java
Output
In this line of code, the var1
variable will be checked first and found to be of type int
(32-bit). Then, the var2
variable will be checked and found to be of type long
(64-bit). A 32-bit number can be assigned to a variable with more bits of the same data type without the risk of getting an incorrect result. Here, Java performs the conversion automatically and accepts the assignment.
In this line of code, even though the var4
variable contains the value 25, which comfortably fits in an int
variable, Java does not consider this. What Java checks—and consequently prevents the automatic conversion—is the assignment of a 64-bit number to a 32-bit variable. Even though the variable holds the value 25, Java assumes that the number could easily be the maximum value a long
variable can hold. With this logic in mind, Java does not perform the automatic conversion from long
to int
but instead leaves the responsibility to the programmer.
In this specific line of code, Java first reads the value of var4
and then converts it to int
using the casting type we have specified inside the parentheses. Once the casting is performed, the number is assigned to the var3
variable.
With the same logic, we can convert a variable from short
to byte
or from int
to short
. The concept of casting will appear frequently, especially when we discuss objects later on.
long
variables represent very large numbers. Java allows us to write these numbers in a more readable format by using the underscore (_
) symbol. This feature can be quite useful when you need to quickly read a number that contains many digits. Internally, Java handles the number exactly as it would in its standard form. This style of declaring a long
number is entirely a personal preference.
App.java
Output
TYPE INFERENCE
Since Java version 10, local variables can be declared using the var
keyword, and the Java compiler will automatically determine the data type of the variable based on the value that is assigned to it. This feature simplifies variable declarations and makes the code more concise.
To use var
with variables, Java requires that the variable be both declared and assigned a value at the same time, as shown in the following example.
App.java
Output
CONSTANTS
At some point in our code, we may have values that we don't want to change programmatically. For example, the sales tax rate, the conversion factor from miles to kilometers, etc. To achieve this, Java requires us to add the final
keyword before the data type of the variable.
When a variable is assigned its first value, it will not allow any further changes to it. In the simple example below, we define the discount on the initial price of a product as final
. This means that programmatically, we cannot change its value—i.e., we cannot attempt to assign a new value within the code. If we want to change the value, it must be done by modifying the initial declaration of the variable.
App.java
Output
If you programmatically try to assign a new value to the discount_coupon
variable, you will receive the following error:
App.java
Output
It's great to support platforms that offer free educational content! A donation can definitely help improve resources and expand the range of services offered.
full-width
0 Comments
What do you think about Ground of Code?