0% found this document useful (0 votes)
41 views12 pages

Typecatsing Datatypes

1) The document discusses various Java data types including their sizes in bits and bytes. 2) It explains widening and narrowing type casting. Widening casting converts a smaller data type to a larger one automatically while narrowing casting requires an explicit conversion from a larger to smaller data type. 3) Examples are provided to demonstrate both widening and narrowing type casting as well as type promotions that occur in expressions to prevent data loss.

Uploaded by

Isha Nigam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views12 pages

Typecatsing Datatypes

1) The document discusses various Java data types including their sizes in bits and bytes. 2) It explains widening and narrowing type casting. Widening casting converts a smaller data type to a larger one automatically while narrowing casting requires an explicit conversion from a larger to smaller data type. 3) Examples are provided to demonstrate both widening and narrowing type casting as well as type promotions that occur in expressions to prevent data loss.

Uploaded by

Isha Nigam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Data Types and Type Conversion Dr.

Bharti Sharma

Type conversion in Java with Examples


JAVA DATA TYPES

Datatype Bits Acquired In Memory

boolean 1

byte 8 (1 byte)

char 16 (2 bytes)

short 16(2 bytes)

int 32 (4 bytes)

long 64 (8 bytes)

float 32 (4 bytes)

double 64 (8 bytes)

What is Type Casting?


Type casting is nothing but assigning a value of one primitive data
type to another.

When you assign the value of one data type to another, you should
be aware of the compatibility of the data type.

If they are compatible, then Java will perform the conversion


automatically known as Automatic Type Conversion and if not, then
they need to be casted or converted explicitly.

There are two types of casting in Java as follows:


Data Types and Type Conversion Dr. Bharti Sharma

 Widening Casting (automatically) – This involves the


conversion of a smaller data type to the larger type size.

byte -> short -> char -> int -> long -> float -> double

 Narrowing Casting (manually) – This involves converting a


larger data type to a smaller size type.

double -> float -> long -> int -> char -> short -> byte

Widening Casting

This type of casting takes place when two data types are automatically
converted. It is also known as Implicit Conversion.

This happens when the two data types are compatible and also when we
assign the value of a smaller data type to a larger data type.

For Example,
The numeric data types are compatible with each other but no automatic
conversion is supported from numeric type to char or boolean. Also, char
and boolean are not compatible with each other.

public class Conversion{


public static void main(String[] args)
{
int i = 200;

//automatic type conversion


long l = i;

//automatic type conversion


float f = l;
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}
}
Data Types and Type Conversion Dr. Bharti Sharma

Output:

1 Int value 200


2 Long value 200
3 Float value 200.0

Narrowing Casting
In this case, if you want to assign a value of larger data type to a smaller
data type, you can perform Explicit type casting or narrowing. This is
useful for incompatible data types where automatic conversion cannot be
done.

Let’s understand this with the help of an example.

//Java program to illustrate explicit type conversion


public class Narrowing
1 {
2
3 public static void main(String[] args)
4 {
5 double d = 200.06;
6
7
8
//explicit type casting
9 long l = (long)d;
10
11 //explicit type casting
12
int i = (int)l;
13
14 System.out.println("Double Data type value "+d);
15
16 //fractional part lost
17 System.out.println("Long Data type value "+l);
18
19
20 //fractional part lost
21 System.out.println("Int Data type value "+i);
}
}

1 Double Data type value 200.06


Data Types and Type Conversion Dr. Bharti Sharma

2 Long Data type value 200


3 Int Data type value 200
Output:

Now that you know how to perform Explicit type casting, let’s move
further and understand how explicit casting can be performed on
Java expressions.

Practice questions:

char and number are not compatible with each other. Let’s see when we try
to convert one into another.

 Java

// Java program to illustrate Incompatible data Type

// for Explicit Type Conversion

// Main class

public class GFG {

// Main driver method

public static void main(String[] argv)

// Declaring character variable

char ch = 'c';
Data Types and Type Conversion Dr. Bharti Sharma

// Declaringinteger variable

int num = 88;

// Trying to insert integer to character

ch = num;

Output: An error will be generated

This error is generated as an integer variable takes 4 bytes while character


datatype requires 2 bytes. We are trying to plot data from 4 bytes into 2 bytes
which is not possible.

How to do Explicit Conversion?


 Java

// Java program to Illustrate Explicit Type Conversion

// Main class
Data Types and Type Conversion Dr. Bharti Sharma

public class GFG {

// Main driver method

public static void main(String[] args)

// Double datatype

double d = 100.04;

// Explicit type casting by forcefully getting

// data from long datatype to integer type

long l = (long)d;

// Explicit type casting

int i = (int)l;

// Print statements

System.out.println("Double value " + d);

// While printing we will see that

// fractional part lost

System.out.println("Long value " + l);


Data Types and Type Conversion Dr. Bharti Sharma

// While printing we will see that

// fractional part lost

System.out.println("Int value " + i);

Output
Double value 100.04
Long value 100
Int value 100
Note: While assigning value to byte type the fractional part is lost and is
reduced to modulo 256(range of byte).
Example:
 Java

// Java Program to Illustrate Conversion of

// Integer and Double to Byte

// Main class

class GFG {

// Main driver method

public static void main(String args[])

// Declaring byte variable


Data Types and Type Conversion Dr. Bharti Sharma

byte b;

// Declaring and initializing integer and double

int i = 257;

double d = 323.142;

// Display message

System.out.println("Conversion of int to byte.");

// i % 256

b = (byte)i;

// Print commands

System.out.println("i = " + i + " b = " + b);

System.out.println(

"\nConversion of double to byte.");

// d % 256

b = (byte)d;

// Print commands

System.out.println("d = " + d + " b= " + b);

}
Data Types and Type Conversion Dr. Bharti Sharma

Output
Conversion of int to byte.
i = 257 b = 1
Conversion of double to byte.
d = 323.142 b= 67

Type Promotion in Expressions

While evaluating expressions, the intermediate value may exceed the


range of operands and hence the expression value will be promoted.
Some conditions for type promotion are:
1. Java automatically promotes each byte, short, or char operand to
int when evaluating an expression.
2. If one operand is long, float or double the whole expression is
promoted to long, float, or double respectively.
Example:
 Java

// Java program to Illustrate Type promotion in Expressions

// Main class

class GFG {

// Main driver method

public static void main(String args[])

{
Data Types and Type Conversion Dr. Bharti Sharma

// Declaring and initializing primitive types

byte b = 42;

char c = 'a';

short s = 1024;

int i = 50000;

float f = 5.67f;

double d = .1234;

// The Expression

double result = (f * b) + (i / c) - (d * s);

// Printing the result obtained after

// all the promotions are done

System.out.println("result = " + result);

Output

result = 626.7784146484375

Explicit Type Casting in Expressions

While evaluating expressions, the result is automatically updated to a


larger data type of the operand. But if we store that result in any smaller
Data Types and Type Conversion Dr. Bharti Sharma

data type it generates a compile-time error, due to which we need to


typecast the result.

Example:
 Java

// Java program to Illustrate Type Casting

// in Integer to Byte

// Main class

class GFG {

// Main driver method

public static void main(String args[])

// Declaring byte array

byte b = 50;

// Type casting int to byte

b = (byte)(b * 2);

// Display value in byte

System.out.println(b);

}
Data Types and Type Conversion Dr. Bharti Sharma

Output
100
Note: In case of single operands the result gets converted to int and
then it is typecast accordingly, as in the above example.

You might also like