Javatpoint Logo
Javatpoint Logo

Java long keyword

The Java long keyword is a primitive data type. It is used to declare variables. It can also be used with methods. It can hold a 64-bit two's complement integer.

Points to remember

  • The long contains minimum value of -263 and a maximum value of 263-1.
  • From Java 8, the long can represent as an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1
  • Its default value is 0L.
  • Its default size is 8 byte.
  • It is used when you need a higher range integer value.

Examples of Java long keyword

Example 1

Let's see an example to use long data type with positive and negative value.

Output:

num1: 10
num2: -10

Example 2

Let's see an example to check whether the long data type holds decimal value.

Output:

error: incompatible types: possible lossy conversion from double to long

Example 3

Let's see an example to check whether the long data type holds float value.

Output:

error: incompatible types: possible lossy conversion from float to long

Example 4

Let's see an example to check whether the long data type holds char value. In such case, the compiler typecast the character implicitly to long type and returns the corresponding ASCII value.

Output:

num: 97

Example 5

In this example, long data type holds the minimum and maximum value.

Output:

min: -9223372036854775808
max: 9223372036854775807

Example 6

Let's create a method that returns a long type value.

Output:

10
Next TopicJava Keywords





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA