Home Tutorial Java Corejava Dataconversion Int Conversion from int to byte

 
 

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

Conversion from int to byte:

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

Description:

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

Code:

import java.io.*;

class inttobyte {
	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());
			byte mybyte = (byte) (myint);
			System.out.println("Convert value from int to byte is: " + mybyte);
		} catch (Exception e) {
			System.out.println(" Error " + e);
		}
	}
};
 

Output:

Download this code

Advertisement

Related Tags for Conversion from int to byte:


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.

Posted on: September 2, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com