- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is narrowing in java? Explain.
Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) as listed below −
- boolean − Stores 1-bit value representing true or, false.
- byte − Stores twos compliment integer up to 8 bits.
- char − Stores a Unicode character value up to 16 bits.
- short − Stores an integer value upto 16 bits.
- int − Stores an integer value upto 32 bits.
- long − Stores an integer value upto 64 bits.
- float − Stores a floating point value upto 32bits.
- double − Stores a floating point value up to 64 bits.
Converting one primitive data type into another is known as type conversion. There are two types of type conversions −
- Widening − Converting a lower datatype to a higher datatype is known as widening.
- Narrowing − Converting a higher datatype to a lower datatype is known as narrowing.
Whenever you assign a lower datatype to higher the Java compiler converts it implicitly and assigns to the specified variable.
Example
public class CastingExample { public static void main(String args[]){ char ch = 'C'; int i = ch; System.out.println("Integer value of the given character: "+i); float f = 10021.224f; double d = f; System.out.println("double value: "+d); } }
Output
Integer value of the given character: 67 double value: 10021.2236328125
But, when you try to assign a higher datatype to lower, at the time of compilation you will get an error saying “incompatible types: possible lossy conversion”
In the following example we are trying to assign an integer value to a character value −
Example
public class CastingExample { public static void main(String args[]){ int i = 67; char ch = i; System.out.println("Character value of the given integer: "+ch); } }
Compile time error
On compiling, the above program generates the following error.
CastingExample.java:4: error: incompatible types: possible lossy conversion from int to char char ch = i; ^ 1 error
In the following example we are trying to assign a double value to a float variable.
Example
public class CastingExample { public static void main(String args[]){ double d = 10021.224d; float f = d; System.out.println("double value: "+f); } }
Compile time error
On compiling, the above program generates the following error.
CastingExample.java:22: error: incompatible types: possible lossy conversion from double to float float f = d; ^ 1 error
Explicit conversion
Therefore, to assign a higher datatype to lower, you need to convert it using the cast operator explicitly as −
float f = (float)d;
Let us rewrite the Example2 and Exampe3 by converting explicitly.
In the following example we are trying to assign an integer value to a character value by converting it explicitly using the cast operator −
Example
public class CastingExample { public static void main(String args[]){ int i = 67; char ch = (char)i; System.out.println("Character value of the given integer: "+ch); } }
Output
Character value of the given integer: C
In the following example we are trying to assign a double value to a float variable by converting it explicitly using the cast operator.
Example
public class CastingExample { public static void main(String args[]){ double d = 10021.224d; float f = (float)d; System.out.println("double value: "+f); } }
Output
double value: 10021.224