Java Integer longValue Method

JavaJavaBeginner
Practice Now

Introduction

In Java, we often need to convert one data type value to another. The longValue() method helps us in converting an Integer object into its long equivalent. This lab demonstrates how to use the longValue() method in step-by-step instructions.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117718{{"`Java Integer longValue Method`"}} java/classes_objects -.-> lab-117718{{"`Java Integer longValue Method`"}} java/class_methods -.-> lab-117718{{"`Java Integer longValue Method`"}} java/modifiers -.-> lab-117718{{"`Java Integer longValue Method`"}} java/oop -.-> lab-117718{{"`Java Integer longValue Method`"}} java/packages_api -.-> lab-117718{{"`Java Integer longValue Method`"}} java/user_input -.-> lab-117718{{"`Java Integer longValue Method`"}} java/wrapper_classes -.-> lab-117718{{"`Java Integer longValue Method`"}} java/identifier -.-> lab-117718{{"`Java Integer longValue Method`"}} java/arrays -.-> lab-117718{{"`Java Integer longValue Method`"}} java/comments -.-> lab-117718{{"`Java Integer longValue Method`"}} java/data_types -.-> lab-117718{{"`Java Integer longValue Method`"}} java/operators -.-> lab-117718{{"`Java Integer longValue Method`"}} java/output -.-> lab-117718{{"`Java Integer longValue Method`"}} java/strings -.-> lab-117718{{"`Java Integer longValue Method`"}} java/variables -.-> lab-117718{{"`Java Integer longValue Method`"}} java/system_methods -.-> lab-117718{{"`Java Integer longValue Method`"}} end

Import required classes

In the LongValueDemo.java file, import the java.lang.Integer class and java.util.Scanner class.

import java.lang.Integer;
import java.util.Scanner;

Create the main method

In the LongValueDemo.java file, create the main method.

public class LongValueDemo {
  public static void main(String[] args) {
    // Code goes here
  }
}

Get an integer input from the user

In the main method, add the following code to get an integer input from the user.

Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int input = scanner.nextInt();

Convert integer to long

In the main method, create an Integer object using the input value and then convert it to long using the longValue() method.

Integer integerObject = Integer.valueOf(input);
long longValue = integerObject.longValue();

Output the long value

In the main method, output the long value to the console using System.out.println().

System.out.println("Long value is " + longValue);

Save and compile the Java file

Save the LongValueDemo.java file and open the terminal in the ~/project directory. Use the following command to compile the Java file:

javac LongValueDemo.java

Run the Java program

After the compilation is successful, use the following command to run the Java program:

java LongValueDemo

Summary

This lab demonstrated how to use the longValue() method to convert an Integer object to its long equivalent. We created a Java program that accepts an integer input from the user, converts it to long using the longValue() method, and outputs the long value to the console.

Other Java Tutorials you may like