Primitive Data Types


🙋 Need help? Ask an expert now!

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.
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) {

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.

Edit Me on GitHub!

Application Question

Which one of these is a valid long declaration?