Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Core Java Basics. 6 Comments on Java type conversion and casting
4
(4)

In this article you will learn about Java type conversion and casting with appropriate examples. Converting a value from one type to another type (data type) is known as type conversion.

 

Introduction

 

Type conversion is of two types based on how the conversion is performed: 1) Implicit conversion (also known as automatic conversion or coercion), 2) Explicit conversion (also known as type casting).

java type conversion and casting

 

Implicit Conversion or Coercion

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


This type of conversion is performed automatically by Java due to performance reasons. Implicit conversion is not performed at all times.

 

There are two rules to be satisfied for the conversion to take place. They are:

  1. The source and destination types must be compatible with each other.
  2. The size of the destination type must be larger than the source type.

 

For example, Java will automatically convert a value of byte into int type in expressions since they are both compatible and int is larger than byte type.

 

Since a smaller range type is converted into a larger range type this conversion is also known as widening conversion. Characters can never be converted to boolean type. Both are incompatible.

 

Explicit Conversion or Casting

 

There may be situations where you want to convert a value having a type of size less than the destination type size. In such cases Java will not help you. You have do it on your own explicitly. That is why this type of conversion is known as explicit conversion or casting as the programmer does this manually.

 

Syntax for type casting is as shown below:

 

(destination-type) value

 

An example for type casting is shown below:

 

int a = 10;
byte b = (byte) a;

 

In the above example, I am forcing an integer value to be converted into a byte type. For type casting to be carried out both the source and destination types must be compatible with each other. For example, you can’t convert an integer to boolean even if you force it.

 

In the above example, size of source type int is 32 bits and size of destination type byte is 8 bits. Since we are converting a source type having larger size into a destination type having less size, such conversion is known as narrowing conversion.

 

A type cast can have unexpected behavior. For example, if a double is converted into an int, the fraction component will be lost.

 

Type promotion in expressions

 

In addition to assignment statements, there is another place where type conversion can occur. It is in expressions. An expression is a collection of variables, values, operators and method calls which evaluate to a single value.

 

Type promotion rules of Java for expressions are listed below:

  • All char, short and byte values are automatically promoted to int type.
  • If at least one operand in an expression is a long type, then the entire expression will be promoted to long.
  • If at least one operand in an expression is a float type, then the entire expression will be promoted to float.
  • If at least one operand in an expression is a double type, then the entire expression will be promoted to double.

 

To understand the above type promotion rules let’s consider the following example of expression evaluation:

class Sample
{
	public static void main(String[] args)
	{
		int i = 1000000;
		char c = 'z';
		short s = 200;
		byte b = 120;
		float f = 3.45f;
		double d = 1.6789;
		double result = (f * b) + (i / c) - (d * s);
		System.out.println("Result = "+result);
	}
}

 

Output of the above program is: Result = 8274.22

 

In the above program the expression is (f * b) + (i / c) – (d * s). In the first sub expression (f * b), as one operand is float, the result of the expression will be a float. In the second sub expression (i / c), char type will be promoted to int and the result of the expression will be an int. In the third sub expression (d * s), as one operand is double, the result of the expression is a double.

 

So the results of the sub expressions are float, int and double. Since one of them is a double, the result of the entire expression is promoted to a double.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

6 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

int a = 10;
byte b = (byte) a;
Please don’t teach wrongly.

    Thanks for pointing that out. I might have made a wrong while typing it.

no teja that is not the proper answer…
the proper answer is java compiler by default chooses some data types among primitive data types ….
for non decimal it chooses int
for decimal it chooses double… that is by default….

    The question (why did we get double as result?) asked was regarding the expression in the post. In the expression implicit conversion took place.

why the result of the sub expression of float,int,double is double?difference between float n double?

    Precision of double is greater than float. If you take float and double, float gets converted to double.

Leave a Reply

Your email address will not be published. Required fields are marked *