×

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put inside of it.Java's long keyword is used to hold values for 64-bit integers, a Java primitive type. In essence, keywords are terms that are used exclusively in a compiler's context and are therefore reserved.

Declaring astatement, method return value, or variable of data type long integer in Java also implements the term long. Java's Long class turns a primitive value of the long type into an object. Only one field with the long type is present in an object of the long type.

To convert long primitive values into long type objects, a wrapper class is considered. Along with this, the long wrapper class has methods for implementing a long to a String and vice versa as well as additional providing the constants and methods for dealing.

  • The long has a value range of 263-1 to -263-263, with -263-1 being the smallest value.
  • The long can now be shown as an unsigned 64-bit long with a minimum value of 0 and a maximum value of 264-1.
  • The default setting for the long keywrd is 0L.
  • 8-byte default size of the long keyword.
  • When a larger range of integer values is required, it is employed.
  • In the Java programming language, the long class acts as a wrapper class to handle the long primitive type. The range of values for this type is represented by the constants MIN VALUE and MAX VALUE.
  • Excepting the value that is followed by l or L, as in 235L, which declares that the value should be considered as a long, all integer literals in the Java programming language are 32-bit int values only.

Syntax for declaring Long:

Long var_ name = value;

Here are some more examples.

long anotherNumber = 34590L;

long octalNumber = 0377;

long hexNumber = 0xffl;

Examples of the Java Long Keyword

Example 1:

This is the program to implement the long Keyword for printing positive and negative values

public class LongEx1  
{  
public static void main (String...s)  
{  
long R=5L;
long S=-15L;
System.out.println("R: "+R);
System.out.println("S: "+S);
}  
}  

Output of the given program is:

R: 5
S: -15

Example 2:

Program to illustration for determining whether a long data type can store decimal values.

public classLongEx2  
{  
public static void main (String...s)  
{  
long n=11.5;
System.out.println("n: "+n);
}  
}  

Output:

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

Example 3:

public class LongEx3  
{  
public static void main (String...s)  
{  
long n=111f;
System.out.println("n: "+n);
}  
}  

Output:

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

Example 4:

public class long_Key 1{
        public static void main (String s []){
sum (34656,45634);
        }
            static long sum (long l1, long l2){
            //long keyword is used to method as return type
            //long keyword is used as parameter
            long ans=l1+l2;
            System.out.println("The result is :"+ans);
            return ans;
                    }


            }

Output:

The result is :80290

Example 5:

Let's look at an illustration to see if the long data type supports char values. When this occurs, the compiler implicitly converts the character to long type and outputs the associated ASCII value.

public class LongEx5
{  
public static void main (String...s)  
{  
long m='b';
System.out.println("m: "+m);
}     
}  

Output of the program is given by:

m: 98

Example 6:

Program for checking the larger and smaller value by using long data type

public class LongEx 6
{  
public static void main (String...s)  
{  
long low=-9223372036854775808L;
        long high=9223372036854775807L;
        System.out.println("low: "+low);
        System.out.println("high: "+high);
}  
}  

Output:

low: -9223372036854775808
high: 9223372036854775807

Example 7:

Program to create a method that returns a value of the long type.

public class LongEx 7{  
    public long disp()  
    {     
        return 15L;
    }  


    public static void main (String [] s) {  


        LongEx7 bj=new LongEx7();
        System.out.println(bj. disp ());
    }  


}  

Output:

15

Related Topics

Java Comparator Interface

Java comparator interface is used in a situation when we have to sort an object which does not implement Comparable or do sorting in a different way than the Comparable....

1 minute read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

Bottom view of a binary tree in Java

The lowest nodes in their horizontal distance are present and referred to as the bottom view of a binary tree. The horizontal distance between the nodes of a binary tree...

4 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

How to Create a Generic List in Java?

Generics are types that have parameters. The goal is to make it possible for methods, classes, and interfaces to take type (Integer, String, etc., and user-defined types) as a parameter....

4 minutes read.

How to Update Java

As we all know that java can be installed in all operating systems like windows, Linux, macOS. We are available with the java 17 and java 18 versions in the...

3 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

User Defined Custom Exceptions in Java

In this tutorial, we will discuss user-defined custom exceptions with examples. Introduction In Java, we have proactively characterised, Exception classes, for example, ArithmeticException, NullPointerException, ArrayoutOfBound and so on. These built-in exceptions are...

3 minutes read.

Java Math log10() Method

The log10() method of Math class returns the base 10 logarithmic value for the specified double argument. Syntax: public static double log10(double a) Parameters: The parameter ‘a’ represents a double value. Return Value: The log10() method...

2 minutes read.

Spliterator in Java 8

In this tutorial, we will understand the meaning of spliterator in java 8. It is just like any other iterator available in java used to traverse the elements of either...

4 minutes read.