long datatype is one of the eight primitive datatype supported by Java. It provides means to create long type variables which can accept a long value. Following are the characteristics of a long data type.
A long variables represents a reserved memory locations to store long values. This means that when you create a variable you reserve some space in the memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store long values in long variables.
Example 1Following example shows the usage of long primitive data type we've discussed above. We've created a long variable as longValue and assigned it a long value. Then this variable is printed.
package com.tutorialspoint; public class JavaTester { public static void main(String args[]) { long longValue = 2; System.out.println("long: " + longValue); } }Output
long: 2Example 2
Following example shows the usage of long primitive data type in an expression. We've created two long variables and assigned them long values. Then we're creating a new long variable longResult to assign it the sum of long variables. Finally result is printed.
package com.tutorialspoint; public class JavaTester { public static void main(String args[]) { long longValue1 = 2; long longValue2 = 4; long longResult = longValue1 + longValue2; System.out.println("long: " + longResult); } }Output
long: 6Example 3
Following example shows the usage of long variable with an invalid value. We've created a long variable as longValue and assigned it an out of range value.
package com.tutorialspoint; public class JavaTester { public static void main(String args[]) { long longValue = 9223372036854775808; System.out.println("long: " + longValue); } }Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The literal 9223372036854775808 of type int is out of range at com.tutorialspoint.JavaTester.main(JavaTester.java:5)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4