Home Tutorial Java Corejava Dataconversion Int Conversion from int to short

 
 

Conversion from int to short
Posted on: September 2, 2010 at 12:00 AM
In this tutorial we will learn how to convert an int type value to short type data.

Conversion from int to short:

In this tutorial we will learn how to convert an int type value  to short type data.

Description:

This program will take an int value from console and provide a conversion to short type data. The line int myint = Integer.parseInt(buffreader.readLine()); is used to read the int value from console and stored in myint variable. The line short myshort = (short)(myint); converts the myint int type value to myshort short type data.

Code:

import java.io.*;

class inttoshort {
	public static void main(String[] args) {
		try {
			BufferedReader buffreader = new BufferedReader(
					new InputStreamReader(System.in));
			System.out.println("Enter the int Value:");
			int myint = Integer.parseInt(buffreader.readLine());
			short myshort = (short) (myint);
			System.out
					.println("Convert value from int to short is: " + myshort);
		} catch (Exception e) {
			System.out.println(" Error " + e);
		}
	}
};
 

Output:

Download this code

Related Tags for Conversion from int to short:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.