Java long type
In this chapter you will learn:
- What is Java long type
- Size and value for Java long type
- How to mark an integer as long type
- Example - long literal
- Example - Java long type
Description
Java long type is used when an int type is not large enough.
Size and value
long is a signed 64-bit type and . The range of long type is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Literals
To specify a long literal, you need to tell the compiler that the
literal value is of type long
by appending an upper- or lowercase L
to the literal.
For example, 0x7ffffffffffffffL
or 123123123123L
.
Example
The following code creates a long
type literal and assigns the value to a
long
type variable.
public class Main {
public static void main(String args[]) {
long l = 0x7ffffffffffffffL;
//from w w w.j av a 2 s . c o m
System.out.println("l is " + l);
}
}
The output generated by this program is shown here:
Example 2
Here is a program that use long type to store the result.
public class Main {
public static void main(String args[]) {
long result= (long)Integer.MAX_VALUE * (long)10;
System.out.println(result);//21474836470
/*from w ww . j av a 2 s.co m*/
}
}
The result could not have been held in an int variable.
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- What is Java float type
- Value and size for Java float type
- How to create float literals
- Example - Java float literal