Type Conversion - Type Casting
Start course
Difficulty
Beginner
Duration
2h 12m
Students
97
Ratings
4/5
starstarstarstarstar-border
Description

In this course, we'll learn about Java data types and operators.

Learning Objectives

  • Variables
  • Data Types
  • Type Conversion & Type Casting
  • Operators
  • Operator Precedence 
  • Expressions, Statements and Block

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language.
  • Basic computer knowledge
Transcript

Hi there. In this video, we will talk about Type Casting in Java. So, why do we need type conversion?

For example, you will develop a calculator application and you will divide the one integer number entered by the user by another integer number entered by the user. But the result of the operation may not always be an integer. In this case, if you do not convert the result to double or float, you will lose data. Let's make this concrete with an example. The user wants to divide number 25 by 2. In this case, the result will be 12.5. If you define the result variable as an integer rather than a double or float, the Java programming language will round it to 12. In this case, we may need type conversion. When we assign the value of one data type to another, if the data types are compatible then Java will perform the conversion automatically. 

This process is known as automatic type conversion. If the conversion is not performed automatically, then they need to be cast or converted explicitly. If the two data types are compatible and when we assign a value of smaller data type to a bigger data type, automatic type conversion happens. In our first example, we define variable a with double data type and no initial value. Secondly, we define variable b with int data type and assign value 20. And we assign variable b to variable a. Because int data type is smaller than the double data type, this conversion is performed automatically and this process is known as automatic type conversion. 

If we want to assign a value of larger data type to a smaller data type, we perform explicit type casting. In our second example, contrary to the first example, we define variable a with double data type and assign value 20. Secondly, we define variable b  with int data type but no initial value. And we try to assign variable a to variable b. Unfortunately, this conversion couldn't be performed automatically, and the compiler gives an error because double data type is bigger than int data type. In such cases, you have to explicitly specify the type cast operator. This process is known as type casting. Of course, there is one thing you should pay attention to. 

If you remember, I shared the sizes of these data types in the previous lesson. I want to remind you once again here that the largest of the data types is double, then float, then long, then integer, then short, then byte. If you convert a small value to a large value, there will be no problem. In other words, if you convert a byte type variable to a double type, you will not experience any data loss. However, if you convert a large value to a small value, you may have data loss. For example, if you convert a variable of type double to type integer, you will lose data. Let's open Eclipse and try to understand Java conversion and data type casting. In exercise project, right click on the data type package and select new class. 

Specify the class name as TypeCasting and select the checkbox for the main method. Let's declare the variable a with the int type and assign 124 to it. Then, declare variable b with the long type and assign a. In this process, there is no need for type casting because long type is larger than int type. In the same way, declare the variable d with double type and assign b. There is no need for type casting again because double type is larger than long type. Let's print the values of these variables by using the print method. Let's change the variable names and other parts. Okay, let's run the code. You see the results in the console. Now, let's look at the situations that need type casting. 

Let's declare variable s with the double type and assign 132.32. Then, declare the variable m with a long type and assign s. In this process, we got a compilation error. It says, 'Type mismatch cannot convert from double to long'. So, the type casting is required because the long type is smaller than the double type. So, we choose the 'Add cast to long' option to add the casting operator with the long type. And let's declare variable c with byte type and assign m. We got an error again. Type cast is required because the byte type is smaller than the long type. 

So, we choose the 'Add cast to byte' option. Let's print the values of these variables by using the print method. Let's change the variable names and other parts. Okay, let's run the code. You see the results in the console. But the value of s is 132.32 while the value of m is only 132. The value of c is a very different result, friends. This is because we tried to convert a large number to a small number. This resulted in some data loss. You should definitely take this into consideration. Yes friends, that's all for type conversion in the Java programming language. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
1889
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics