long Data Type in Java Programming Language

Rumman Ansari   Software Engineer   2019-03-30   10955 Share
☰ Table of Contents

Table of Content:


long Data Type in Java

When we need big range of numbers then we need this data type.The range of a long is quite large. long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value.

  • Long data type is a 64-bit signed two's complement integer
  • Minimum value is -9,223,372,036,854,775,808(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
  • This type is used when a wider range than int is needed
  • Default value is 0L
  • Example:
     long a = 100000L, long b = -200000L 

Data TypeSize(bit)Range
short64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

long Variable Declaration and Variable Initialization:

Variable Declaration : To declare a variable , you must specify the data type & give the variable a unique name.

 long distance;  

Variable Initialization : To initialize a variable you must assign it a valid value.

 distance= 12121 ;  

long Variable Declaration and Variable Initialization in two steps:

long distance;
long = 12121;
long data type in java

Example :

Save Source File Name as : LongExample1.java
To compile : javac LongExample1.java
To Run : java LongExample1
// A Java program to demonstrate long data type
class LongExample1 {
   public static void main(String args[])
     {
           long distance;
           distance = 12121;
           System.out.println(distance);
     }
  }
  
Output
 12121
 press any key to continue ...
 
 
long data type in java

int Variable Declaration and Variable Initialization in one line:

long distance = 12121;
long data type in java

Example :

Save Source File Name as : LongExample2.java
To compile : javac LongExample2.java
To Run : java LongExample2
// A Java program to demonstrate long data type
class LongExample2 {
   public static void main(String args[])
     {
           long distance = 12121;
           System.out.println(distance);
     }
  }
Output
12121
press any key to continue ...

Example of short

 public class LongDataType {

   public static void main(String []args) {
      // this is declaration and initialization of variable a
	  // datatype is long
	  long a = 100000L;
	  // this is declaration and initialization of variable b
	  // datatype is long
       long b = -200000L;
      System.out.println(a); // it will print a variable
      System.out.println(b); // it will print b variable
   }
}

Output :
100000
-200000
Press any key to continue . . .