Java Integer shortValue Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the Java shortValue() method, which is used to convert an Integer object into a short value. We will learn how to use this method with various examples.


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/inner_classes("`Inner Classes`") 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/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-117744{{"`Java Integer shortValue Method`"}} java/inner_classes -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/classes_objects -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/class_methods -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/modifiers -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/oop -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/packages_api -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/user_input -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/wrapper_classes -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/identifier -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/arrays -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/data_types -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/operators -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/output -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/strings -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/variables -.-> lab-117744{{"`Java Integer shortValue Method`"}} java/system_methods -.-> lab-117744{{"`Java Integer shortValue Method`"}} end

Set up the Java Development Environment

First, we need to have a Java Development Environment set up on our system. If you already have one set up, you can skip to the next step. If not, please follow these steps:

  1. Open the Terminal on your Ubuntu system.

  2. Enter the following command to update the package lists:

    sudo apt-get update
  3. Enter the following command to install the default JDK/JRE:

    sudo apt-get install default-jdk
  4. Verify the installation by entering the following command to check the version of Java installed:

    java -version

Create a Java file

  1. Open the Terminal on your Ubuntu system.

  2. Change the directory to the project directory where you want to keep your Java file by entering the following command:

    cd ~/project
  3. Enter the following command to create a new Java file:

    touch ShortValue.java

Write code using the shortValue() method

  1. Start by importing the required package:

    import java.lang.Integer;
  2. Define a class named "ShortValue".

    public class ShortValue {
        public static void main(String[] args) {
        }
    }
  3. Declare the variables and assign the values you want to convert:

    Integer myInt = 10;
    short myShort = myInt.shortValue();
  4. Print the equivalent short value:

    System.out.println("Equivalent short value is: " + myShort);
  5. The final code should look like:

    import java.lang.Integer;
    
    public class ShortValue {
        public static void main(String[] args) {
            Integer myInt = 10;
            short myShort = myInt.shortValue();
    
            System.out.println("Equivalent short value is: " + myShort);
        }
    }

Test the code

  1. Save the Java file by pressing Ctrl + x, then y, then Enter.

  2. Compile the code by entering the following command in the terminal:

    javac ShortValue.java
  3. Run the code by entering the following command:

    java ShortValue
  4. You should see the output: "Equivalent short value is: 10".

User-defined example

  1. Modify the code that you wrote in Step 3 to prompt the user to enter a number to convert:

    Scanner input = new Scanner(System.in);
    System.out.print("Enter a number to convert to short: ");
    int myInt = input.nextInt();
    Integer myInteger = myInt;
    short myShort = myInteger.shortValue();
  2. The final code should look like:

    import java.util.Scanner;
    import java.lang.Integer;
    
    public class ShortValue {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter a number to convert to short: ");
            int myInt = input.nextInt();
            Integer myInteger = myInt;
            short myShort = myInteger.shortValue();
    
            System.out.println("Equivalent short value is: " + myShort);
        }
    }

Test the user-defined example

  1. Save the Java file by pressing Ctrl + x, then y, then Enter.

  2. Compile the code by entering the following command in the terminal:

    javac ShortValue.java
  3. Run the code by entering the following command:

    java ShortValue
  4. Enter a number to convert when prompted by the program.

  5. You should see the equivalent short value printed to the console.

Summary

In this lab, we learned about the Java shortValue() method, which is used to convert an Integer object into its short equivalent. We saw how to use this method with two examples: one that converts a pre-defined Integer value, and another that prompts the user to enter a value to convert. We also learned how to set up a Java Development Environment and compile and run Java code on an Ubuntu system.

Other Java Tutorials you may like