What is casting in java with example

Type Casting in Java

Type Casting in Java with Examples

In this article, I am going to discuss Type Casting in Java with Examples. Before learning Type Casting, you must know about Java Data Types. So, please read our previous article, where we discussed Literals in Java with Examples. At the end of this article, you will understand what Type Casting is and why and when we need Type Casting in Java.

What is Type Casting in Java?

The process of converting the value of one data type (int, float, double, etc.) into another data type is known as Type Casting. Type Casting is the temporary conversion of a variable from its original data type to some other data type, like being cast for a part in a play or movie.

With primitive data types if a cast is necessary from a less inclusive data type to a more inclusive data type it is done automatically. If a cast is necessary from a more inclusive data type to a less inclusive data type the cast must be done explicitly by the programmer.

Conversion between Primitive Data Types: Cast Operator

The Cast Operator manually lets you convert a value of the data type into another data type. It can be done by Writing the cast operator in front of the expression that needs to be converted.

Читайте также:  Сколько времени нужно чтобы освоить java

Syntax : (dataTypeName) expression;

When casting from a double to an int, the fractional part will be discarded.

Example :

int x;
double y = 2.5;
x = (int)y;

Here, int is the Cast Operator.

Java compiles the code with the cast operator with no problems. In this case, the variable y has a value of 2.5 (the floating-point value) which must be converted to an integer. The value that is returned and stored in variable x would be truncated, which means the fractional part of the number is lost to accommodate the integer data type. Thus, x=2 and, the value of the variable y is not changed at all: y=2.5

Types of Type Casting in Java:
  1. Widening or Automatic Type Casting
  2. Narrowing or Explicit type Casting
Widening Type Casting in Java:

Widening Type Casting is also known as Automatic Type Casting. When you assign the value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion. For better understanding, please have a look at the following image.

Type Casting in Java with Examples

It happens when the two data types are compatible and when we assign the value of a smaller data type to a bigger data type.

Example of Widening or Automatic Type Casting in Java:
package Demo; public class AutomaticTypeConversion < public static void main (String args[]) < int intVariable = 100; long longVariable = intVariable; float floatVariable = longVariable; System.out.println ("Integer Value is : " + intVariable); System.out.println ("Float Value is : " + floatVariable); System.out.println ("Long Value is : " + longVariable); >>
Output:

Example of Widening or Automatic Type Casting in Java

Narrowing Type Casting in Java:

Narrowing Type Casting is also known as Explicit type Casting. This is useful for incompatible data types where automatic conversion cannot be done. It happens when the two data types are not compatible and when we want to assign a value of a larger data type to a smaller data type.

Example of Narrowing or Explicit Type Casting in Java:
package Demo; public class ExplicitTypeCasting < public static void main (String[]args) < double doubleVariable = 100.04; long longVariable = (long) doubleVariable; int intVariable = (int) longVariable; System.out.println ("Double Value is : " + doubleVariable); System.out.println ("Long Value is : " + longVariable); System.out.println ("Integer Value is : " + intVariable); >>
Output:

Example of Narrowing or Explicit Type Casting in Java

In the next article, I am going to discuss Operators in Java with Examples. Here, in this article, I try to explain Type Casting in Java with Examples and I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Источник

Mastering Type Casting in Java: A Complete Guide with Examples

Learn about the two types of type casting in Java, its importance in efficient data storage and manipulation, and best practices for avoiding common issues. Read on for examples and tips on mastering type casting in Java.

  • Understanding Type Casting in Java
  • Types of Type Casting in Java
  • Java Type Casting Tutorial
  • Data Types in Java
  • Numeric Promotion in Java
  • Best Practices and Common Issues in Type Casting
  • Other quick code examples for type casting in Java with examples
  • Conclusion
  • What is type casting in Java with example?
  • What is type casting with example?
  • How do you explain type casting in Java?
  • What is type casting and its types?

Type casting is a critical concept in Java programming that involves converting one data type to another. In Java, there are two types of type casting: widening casting and narrowing casting. Widening casting is done automatically by the compiler, while narrowing casting is done manually by the programmer using the cast operator. This article will guide you through the concept of type casting in java , its types, and how it works with examples, to help you better understand its importance in efficient data storage and manipulation.

Understanding Type Casting in Java

Type casting is the process of converting one data type to another in Java. It is a critical aspect of Java programming as it helps in converting data types during arithmetic calculations and other operations. There are two types of type casting in Java: widening casting and narrowing casting.

Widening casting is done automatically by the compiler, and it involves converting a smaller data type to a larger data type. For instance, when you convert an integer to a float, the compiler automatically widens the integer to a float.

On the other hand, narrowing casting is done manually by the programmer using the cast operator. It involves converting a larger data type to a smaller data type. For example, when you convert a double to an integer, you need to use the cast operator to narrow the double to an integer.

Types of Type Casting in Java

As mentioned earlier, Java has two types of type casting: widening casting and narrowing casting.

Widening Casting

Widening casting is done automatically by the compiler, and it involves converting a smaller data type to a larger data type. Java supports the following types of widening casting:

  1. byte to short or int or long or float or double
  2. short to int or long or float or double
  3. char to int or long or float or double
  4. int to long or float or double
  5. long to float or double
  6. float to double

Narrowing Casting

Narrowing casting is done manually by the programmer using the cast operator. It involves converting a larger data type to a smaller data type. Java supports the following types of narrowing casting:

  1. double to float or long or int or short or byte
  2. float to long or int or short or byte
  3. long to int or short or byte
  4. int to short or byte
  5. short to byte
  6. char to int

Java Type Casting Tutorial

upcasting and downcasting in java — Full Tutorial · Java operators : type cast operators Duration: 5:50

Data Types in Java

Java has 13 data types that can be used for type casting. The data types in java are divided into two categories: primitive and object types.

Primitive Data Types

primitive data types are the basic data types in Java, and they are not objects. There are eight primitive data types in java :

Object Data Types

Object data types are data types that are created using classes. There are five object data types in Java:

Numeric Promotion in Java

Numeric promotion is used for bringing operands of a numeric operator to a common type for performing operations. Numeric promotion is essential for efficient data storage and manipulation in Java. Numeric promotion is done automatically by the compiler when performing arithmetic operations on two or more operands of different data types.

When performing arithmetic operations, the compiler promotes the operands to the largest data type before performing the operation. For example, when you add an integer to a double, the compiler promotes the integer to a double before performing the addition operation.

Best Practices and Common Issues in Type Casting

best practices for type casting in java include avoiding data loss, using the correct type casting method , and checking for compatibility before casting data types.

Data loss occurs when you convert a larger data type to a smaller data type, and the value of the larger data type is greater than the maximum value of the smaller data type. To avoid data loss, you should use the correct type casting method and check for compatibility before casting data types.

Common issues with type casting in Java include data loss, incompatible data types, and errors during arithmetic calculations. To avoid these issues, you should use the correct syntax, check for compatibility, and test code for errors.

Other quick code examples for type casting in Java with examples

In Java case in point, java type casting code sample

// You can typecast to convert a variable of one data type to another. // Wide Casting converts small data types to larger ones. // Narrow Casting converts large data types to smaller ones. // Java can automatically Wide Cast. // Java will throw an error when trying to automatically Narrow Cast. // This is because data is often lost when Narrow Casting. // Narrow Casting must be done manually.//Wide Cast: int SomeNumber = 5; double WideCastedNumber = (double)SomeNumber;//Narrow Cast: double SomeNumber = 5.39; int NarrowCastedNumber = (int)SomeNumber; //Note: The data that holds the decimal places will be lost!

In Java , for example, Java Type Casting

Narrowing Casting (manually) - larger type to a smaller size type double -> float -> long -> int -> char -> short -> byte 

In Java as proof, Java Type Casting

In Java , for instance, java type casting code sample

Lowest to highest primitive data types: Byte-->Short-->Int-->Long-->Float-->Double
Byte-->short-->char-->Int-->long-->float-->double

Conclusion

In conclusion, type casting is an essential concept in Java programming, which involves converting one data type to another. It is a process that is used to convert data types during arithmetic calculations and other operations. In Java, there are two types of type casting: widening casting and narrowing casting. Widening casting is done automatically, while narrowing casting is done manually. It is important to use the correct type casting method, check for compatibility, and test the code for errors. By following these best practices and understanding the types of type casting in Java, programmers can efficiently manipulate and store data in their programs.

Источник

Оцените статью