Conversion from int to short

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

 
 

Share on Google+Share on Google+

Conversion from int to short

Ads

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

Advertisements

Posted on: September 2, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles