×

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 String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

Java vs Dot Net

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Interfaces and Classes in Strings in Java

CharBuffer: CharBuffer is utilized to implement the CharSequence interface. With the help of the mentioned class, we can allow character buffers to be utilized instead of CharSequences. We can consider the illustration...

4 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Java Linters

When it comes to programming, everyone makes mistakes. Errors are bad for developers since they are difficult to handle. But handling as many as possible errors will bring out the...

6 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.

Character Array in Java

A character array is an array which holds values of character data types. It is different from a string array. The character array, string, and StringBuffer classes in the Java...

4 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Java Enum Keyword

Definition: A data type in Java called Enum has a respect to supply of constants. The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST),...

4 minutes read.

Java Case Keyword

Case keyword: In this article we are going to learn the concept of a java case keyword.Generally, java case keyword is used with the switch statements.Case keyword is implemented in conditional...

3 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.