- Trending Categories
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
Java Type Casting Examples
We can convert one data types into another data type using casting when narrowing happens in case widening happens, no casting is required.
Narrowing Conversion
Narrowing refers to passing a higher size data type like int to a lower size data type like short. It may lead to data loss. Following program output will be 44.
public class MyFirstJavaProgram { public static void main(String []args) { int a = 300; byte b = (byte)a; // narrowing System.out.println(b); } }
Widening/Promotion Conversion
Widening refers to passing a lower size data type like int to a higher size data type like long.
public class MyFirstJavaProgram { public static void main(String []args) { int a = 300; long b = a; System.out.println(b); } }
- Related Articles
- Type casting in JavaScript.
- Difference Between Type casting and Type Conversion
- Type Casting operators in C++
- Explicit Type Casting in Python Language
- const_cast in C++ - Type casting operators
- Explicit type casting operator in C++
- What is Type casting in C#?
- What are up-casting and down-casting in Java?
- HTML5 data-* attribute type casting strings and numbers
- What is the difference between type conversion and type casting in C#?
- PHP Casting Variable as Object type in foreach Loop
- What is the difference between up-casting and down-casting in Java?
- What are the differences between Widening Casting (Implicit) and Narrowing Casting (Explicit) in Java?
- How is down-casting possible in Java?
- What is a casting expression in Java?

Advertisements