Java Long Signum Method

JavaJavaBeginner
Practice Now

Introduction

This lab demonstrates the usage of the Java Long class' signum() method. The method returns the signum function value of a given long value.


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/break_continue("`Break/Continue`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/BasicSyntaxGroup -.-> java/while_loop("`While Loop`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117920{{"`Java Long Signum Method`"}} java/classes_objects -.-> lab-117920{{"`Java Long Signum Method`"}} java/class_methods -.-> lab-117920{{"`Java Long Signum Method`"}} java/modifiers -.-> lab-117920{{"`Java Long Signum Method`"}} java/oop -.-> lab-117920{{"`Java Long Signum Method`"}} java/packages_api -.-> lab-117920{{"`Java Long Signum Method`"}} java/user_input -.-> lab-117920{{"`Java Long Signum Method`"}} java/wrapper_classes -.-> lab-117920{{"`Java Long Signum Method`"}} java/identifier -.-> lab-117920{{"`Java Long Signum Method`"}} java/arrays -.-> lab-117920{{"`Java Long Signum Method`"}} java/break_continue -.-> lab-117920{{"`Java Long Signum Method`"}} java/data_types -.-> lab-117920{{"`Java Long Signum Method`"}} java/if_else -.-> lab-117920{{"`Java Long Signum Method`"}} java/operators -.-> lab-117920{{"`Java Long Signum Method`"}} java/output -.-> lab-117920{{"`Java Long Signum Method`"}} java/strings -.-> lab-117920{{"`Java Long Signum Method`"}} java/variables -.-> lab-117920{{"`Java Long Signum Method`"}} java/while_loop -.-> lab-117920{{"`Java Long Signum Method`"}} java/system_methods -.-> lab-117920{{"`Java Long Signum Method`"}} end

Set up the project

Create a new Java file named SignumDemo.java and open it in a code editor.

touch ~/project/SignumDemo.java

Import the required package

In this step, you need to import the java.lang package to use the Long class. Add the following code to your SignumDemo.java file:

import java.lang.Long;

Define the main method

In this step, you will define a main method that will prompt the user to enter a long value and display its signum value. Add the following code to your SignumDemo.java file:

public static void main(String[] args) {
   Scanner input = new Scanner(System.in);
   System.out.print("Enter a long value: ");
   long num = input.nextLong();
   System.out.println("Signum value of " + num + " is " + Long.signum(num));
}

Compile the program

In this step, you need to compile the SignumDemo.java file using the javac command. Run the following command in your terminal:

javac SignumDemo.java

Run the program

In this step, you will execute the program using the java command in the terminal. Run the following command in your terminal:

java SignumDemo

Test the program

In this step, you can test the signum() method by entering different values of long. The program will display the signum value for the entered number.

For example, if you enter 7 as input, the program will display the following output:

Enter a long value: 7
Signum value of 7 is 1

Modify the program

Modify the code to include a loop that will prompt the user to enter a long value until they enter 0. Add the following code to your main() method:

long num = 1;
while (num != 0) {
   System.out.print("Enter a long value (enter 0 to exit): ");
   num = input.nextLong();
   if (num == 0) {
      continue;
   }
   System.out.println("Signum value of " + num + " is " + Long.signum(num));
}

Review the code

Review the complete code for the SignumDemo.java file.

import java.lang.Long;
import java.util.Scanner;

public class SignumDemo {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      long num = 1;
      while (num != 0) {
         System.out.print("Enter a long value (enter 0 to exit): ");
         num = input.nextLong();
         if (num == 0) {
            continue;
         }
         System.out.println("Signum value of " + num + " is " + Long.signum(num));
      }
      input.close();
   }
}

Compile and Run

Compile and Run the Java code by running the following command:

javac SignumDemo.java && java SignumDemo

Summary

In this lab, you learned how to use the Java Long class' signum() method. The method returns the signum function value of a given long value. The output of the method is 1, 0, or -1, for positive, zero, and negative numbers, respectively.

Other Java Tutorials you may like