typecasting using Java Program - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Friday, September 1, 2017

typecasting using Java Program

Write a java program to demonstrate typecasting as an example


Sample input and output:

Conversion of int into byte:- i and b:255 and -1

Conversion of double to int:- d and i:323.142 and 323

Conversion of double to byte:- d and b:323.142 and 67

Program:

class Conversion
{
public static void main(String args[])
{
byte b;
int i=255;
double d=323.142;
System.out.println("\nConversion of int into byte:-");
b=(byte)i;
System.out.println("i and b:"+i+" and "+b);
System.out.println("\nConversion of double to int:-");
i=(int)d;
System.out.println("d and i:"+d+" and "+i);
System.out.println("\nConversion of double to byte:-");
b=(byte)d;
System.out.println("d and b:"+d+" and "+b);
}

}

No comments:

Post a Comment