Java Primitive Type Conversions: Narrowing and Widening, Summaries of Software Development Methodologies

Java supports two types of conversions for primitive data types: narrowing and widening. Narrowing, also known as explicit type casting, involves converting a larger data type to a smaller one. Widening, on the other hand, is an implicit conversion from a smaller data type to a larger one. Brief descriptions and examples of each type of conversion.

Typology: Summaries

2019/2020

Uploaded on 06/21/2020

iris-storm
iris-storm 🇵🇰

1 document

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Q. In Java, for the primitive types, there are two kinds of type conversions. Please
describe each briefly with a simple example.
Ans: The two types of conversion for primitive types are
1. Narrowing:
Also known as explicit type casting.
It is conversion from data type having larger range to data type having
smaller range provided both data types are compatible.
Such conversion is explicit, that is, you have to do it yourself by casting.
Compiler won’t do it for you.
General form: destination value= (target type) source value
Example: int a; long b; a =(int) b;
2. Widening:
Also known as implicit type casting.
It is conversion from data type having smaller range to data type having
larger range provided both data types are compatible.
Such conversion is implicit, that is, the compiler will automatically do it.
General form: destination value= source value
Example: int a; long b; b = a;

Partial preview of the text

Download Java Primitive Type Conversions: Narrowing and Widening and more Summaries Software Development Methodologies in PDF only on Docsity!

Q. In Java, for the primitive types, there are two kinds of type conversions. Please describe each briefly with a simple example. Ans: The two types of conversion for primitive types are

1. Narrowing:  Also known as explicit type casting.  It is conversion from data type having larger range to data type having smaller range provided both data types are compatible.  Such conversion is explicit, that is, you have to do it yourself by casting. Compiler won’t do it for you.  General form: destination value= (target type) source value  Example: int a; long b; a =(int) b; 2. Widening:  Also known as implicit type casting.  It is conversion from data type having smaller range to data type having larger range provided both data types are compatible.  Such conversion is implicit, that is, the compiler will automatically do it.  General form: destination value= source value  Example: int a; long b; b = a;