0% found this document useful (0 votes)
28 views5 pages

Type Casting.

Uploaded by

Satya Narayana
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)
28 views5 pages

Type Casting.

Uploaded by

Satya Narayana
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/ 5

A.B.N &P.R.

R COLLEGE OF
SCIENCE,KOVVUR

SUBJECT: OBJECT ORIENTED


PROGRAMMING THROUGH JAVA

CLASS :IIB.SC(M.P.CS,M.S.CS,M.C.CS)

LECTURE 15: DELIVERED BY M.MURTHY


Type casting:

Type casting is a process to convert a value of one data type to another data type.

There is a need to store a value of one type into a variable of another type. We must

cast the value to be stored by preceding it with the type name in parentheses.

Syntax:- type variable1 = (type) variable2;

There are two type of typecasting in java.

1.Implicit type casting.

2.Explicit type casting.


Implicit type casting :-The conversion of a data type which is carried out
automatically by the compiler without programmer intervention is called the
implicit type conversion. This conversion is done only when a smaller data type is
converted into bigger data type. It is also called Widening.
Widening is the process of converting small data type value to larger data type
value. when the two data types are compatible and also when we assign the value
of a smaller data type to a larger data type
byte -> short -> char -> int -> long -> float -> double
Widening automatic Conversion.
Example:- byte b=110;
int a;
b=a;//Here byte is converted to int automatically by compiler.
This conversion is widening because byte(1byte) is converted to
int(4bytes)
Narrowing or Explicit Conversion
If we want to assign a value of larger data type to a smaller data type we perform
explicit type casting or narrowing. Explicit type casting is done by the
programmer. If we don't perform casting then compiler reports compile time error.
Narrowing means converting larger data type value to smaller data type value.

Example:-
1. int a=125;
byte b;
b=a;//compile time error. Here conversion is required.
b=(int)a;
2. int x=97;
char ch=(char)x;
System.out.println(ch); prints ‘a’. Here conversion is done by the programmer.

You might also like