primitives
long
With this example we are going to demonstrate how to use a long
type in Java. The long
data type is a 64-bit signed two’s complement integer
. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int
.
- In short, to create a variable of
long
type you should type the long
keyword in your variable. In the example the System.currentTimeMillis()
returns a long
number that is the current time in milliseconds.Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.basics; public class LongExample { public static void main(String[] args) { long l1 = System.currentTimeMillis(); long l2 = 1000*1000; System.out.println("Value of long l1 is: " + l1); System.out.println("Value of long l2 is: " + l2); } }
Output:
Value of long l1 is: 1318967029108
Value of long l2 is: 1000000
This was an example of how to use a long
type in Java.