Share on Google+Share on Google+

Conversion from short to byte

Advertisement
In this tutorial we will learn how to convert a short type data to byte type.

Conversion from short to byte:

In this tutorial we will learn how to convert a short type data to byte type.

Description:

This program will take a short type value from console and provides a conversion to byte type. The line short myshort = Short.parseShort(buffreader.readLine()); reads the short type data from console. The line byte mybyte = (byte)(myshort); is used to convert the myshort short type data to mybyte byte type data.

Code:

import java.io.*;

class shorttobyte {
public static void main(String[] args) {
try {
// Reads the value from console
BufferedReader buffreader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("---Data Conversion from short type to byte type---");
System.out.println("Enter short type value: ");
// Read short type data
short myshort = Short.parseShort(buffreader.readLine());
// Convert short type data to byte type
byte mybyte = (byte) (myshort);
System.out.println("Converted value from short type to byte type is : " + mybyte);
} 
catch (Exception e) {
System.out.println(" Error " + e);
}
}
}
 

Output:

Download this code

Advertisements

Advertisement

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

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.