Skip to the content
onlineexamguide
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Type Casting in Java or Type Conversion or Type Promotions Tutorial and MCQ

Type Casting in Java

Study and learn Type Casting in Java MCQ questions and answers on Type Casting or Type Promotions. To convert from one data type to the other either Implicit or Explicit Type Conversions are required. Attend job interviews easily with these MCQs.

Type Casting in Java

Java allows developers to work with different types of data.and Java allows conversion of numeric data of type byte, short, char, int, long, float and double from one type to another type.

Automatic Type Conversion refers to both Automatic Type Promotions and Type Casting. Type Promotion is a Widening Conversion while Type Casting is a Narrowing Conversion. A widening conversion is also called Upward Casting and Narrowing Conversion is also called a Downward Casting.

Type Promotions or Type Conversions in java

Implicit type conversion of a variable from one data type to another data type is called Type Promotion. Since it is a Type Promotion, the value of a variable is promoted to a compatible higher data type. This type of conversion is also called a Widening Conversion.

Note 1: Type Promotion is a Lossless Conversion. Data is not truncated.

Note 2: Some developers call this Automatic Type Conversion as Automatic Type Casting or Implicit Type Casting. Explicit Type casting or conversion is simply called Type Casting.

Numeric arithmetic expressions are automatically promoted to a higher data type provided the following two conditions or Rules are satisfied.

  1. Target data type should be compatible with the source data type.
  2. Target data type should be bigger in size than the source data type.

Compatible Data Types

All numeric data types are compatible for either Widening Conversion or Narrowing Conversion. The list is below.

  • byte
  • char
  • short
  • int
  • long
  • float
  • double

Incompatible Data Types

Numeric to Non-numeric data types are incompatible for either Widening Conversion or Narrowing Conversions. The list is below.

  • boolean
  • byte, char, short, int, long, float, double

Automatic Type Promotion Scenarios

If one of the operands is of type long, the whole arithmetic expression gives a long value. If one of the operands is of type float, the whole arithmetic expression gives a float value. Lastly, if one of the operands is of type double, the whole arithmetic expression gives a double value.

Here is the list of possible Scenarios of how a data type is promoted automatically.

  1. byte to int
  2. char to int
  3. short to int
  4. int to long
  5. byte to long
  6. char to long
  7. short to long
  8. byte to float
  9. char to float
  10. short to float
  11. int to float
  12. byte to double
  13. char to double
  14. short to double
  15. int to double
  16. float to double

1 Example

class TypePromotion1
{
  public static void main(String args[])
  {
    short a = 120;
    byte b = 50;
    //120*50 is promoted to int automatically
    int c = a*b;
    System.out.println(c);
    
    int a = 4;
    byte d = a * 5; //ERROR
    //a*5 is converted to int
    //int to byte conversion is not automatic
  }
}

2 Example

class TypePromotion2
{
  public static void main(String args[])
  {
    short a = 120;
    float b = 50.0f;
    //120*50.0f is promoted to float automatically
    float c = a*b;
    System.out.println(c);

    float d = 65;
    //65 is converted to float
  }
}

3 Example

class TypePromotion3
{
  public static void main(String args[])
  {
    short a = 120;
    int b = 50;
    double c = 1.0;
    //120*50*1.0 is promoted to double automatically
    //short * int * 1.0 == double
    double e = a*b*c;
    System.out.println(c);

    double f = 35*1.0;
    //35*1.0 => int * double == double
    System.out.println(f);

    double g = 40;
    //40 is converted to double
  }
}

Type Casting in Java

Type Casting is nothing but manually casting or converting an operand or constant or the value of an expression from one data type to another. java Type Casting is explicit in nature and it is a Narrowing Type Conversion.

Note: Type Casting is a Lossy Conversion. Data is TRUNCATED

You should check compatibility before casting the data (constant, value of expression) to another data type. Check the above list of Compatible and Incompatible data types under Type Promotions.

Numeric arithmetic expressions are manually demoted to a lower data type provided the following two conditions or Rules are satisfied.

  1. The target data type is compatible with the source data type.
  2. The target data type is smaller in size than the source data type.

Syntax of Type Casting

Syntax for explicit type casting includes Parentheses surrounding a Target Data Type and an immediately following Source Data Type.

(Target Data Type)Source Data Type

Example:
float a = 34.56;
int b = (int)a;

1.Example

Type casting from int to short, int to byte, int to char and char to int are shown below.

class TypeCasting1
{
  public static void main(String args[])
  {
    int a = 30;
    //int to short type casting
    short b = (short)a;

    //int to byte
    byte c = (byte)a;

    //short to byte
    byte d = (byte)b;

    //int to char
    int num = 65;
    char ch = (char)num;
    System.out.println(ch); //prints A

    //char to int
    char ch2 = 'B';
    int num2 = ch2;
    System.out.println(num2); //prints 66;
  }
}

2. Example – Real World Example for Software Developers

Below example demonstrates how to retain only 2 digits of precision. You can use Format Specifiers to print only 2 digits after precision. But, the variable holds all the digits.

class TypeCasting2
{
  public static void main(String args[])
  {
    //GOAL : PRINT : 21.42
    int a = 150;
    int b = 7;
    //150/7 = 21.428572
    float c = a/b;
    printf(c);
    //Unfortunately Output: 21.0

    //TYPE PROMOTION
    //1.0f promotes int a to float value
    float d = (1.0f * a)/b; // 150.0/7
    d = d* 100; //2142.8572
    //TYPE CASTING
    d = (int)d; //lossy conversion
    d = 1.0f * d / 100; // 2142.0/100
    System.out.println(d);
    //varible d holds only two precision digits
    //Output: 21.42
  }
}

This is how Java language implements Type Casting or Type Conversions which are essential to handle different types of input data

[WpProQuiz 55]

Write a comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Atal Bihari Vajpayee | श्री अटल बिहारी वाजपेयी जीवन परिचय
  • सम्राट अशोक का जीवन परिचय (Emperor Ashoka)
  • Prithviraj Chauhan
  • बाल गंगाधर तिलक
  • स्वामी दयानंद सरस्वती
  • UPPSC Mines Inspector Recruitment 2022 Notification Out
  • AIIMS Delhi JR Vacancy 2022 [194 Post] Notification and Apply Online
  • भीमराव अम्बेडकर
  • डॉक्टर राजेंद्र प्रसाद का जीवन परिचय
  • श्रीनिवास रामानुजन का जीवन परिचय
  • Amnesty International day
  • World Economic Forum
  • UPSSSC VDO Syllabus and Exam Pattern 2022
  • RBI Officer Grade B Recruitment 2022
  • UKMSSB Assistant Professor Recruitment 2022 Apply Now 339 Post

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming Tests
  • C programming Tests
  • C++ programming Tests
  • Aptitude Tests

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2022
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2022
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2022 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.