Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Long
Java Basics Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Java Basics Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of essential coding proficiency.
-->
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
The next integer-related primitive datatype we will delve into is long, which is used more commonly than byte and short because the range for long is even greater than that of int. The long primitive data type can hold integers such as eight trillion. Please review the code below for an example of declaring and using long variables.
LongDataTypeExample.java
package exlcode;
public class LongDataTypeExample {
// long can by any integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
public static long exampleVariableOne = 2147483647;
public static long exampleVariableTwo = 2;
// integers bigger than 2147483647 cannot exist in the code
// and must be parsed from a String value
// this will be explained in Unit 8
public static long exampleVariableThree = Long.parseLong("9223372036854775807");
public static void main(String[] args) {
System.out.println(exampleVariableOne);
System.out.println(exampleVariableTwo);
System.out.println(exampleVariableThree);
}
}
This example takes us through parsing a long variable from a String because the value is too big to write in the program. We will look more closely at this functionality in later sections. Similarly to what we have seen before with declaration of primitive data types, the addition of an 'l' or 'L' at the end of the line informs the program that the value is a long.
Although long holds twice the memory of int, it is an extremely useful data type when working with astronomically large whole integers such as the weight of the earth or the speed of light and sound. Similar to int, long will always be a whole, non-decimal number.