CodeGym /Courses /New Java Syntax /Type conversion

Type conversion

New Java Syntax
Level 12 , Lesson 3
Available

"And now for the most interesting part. I'll tell you about type conversions. Variables can't change their types, but there is a place where you can convert types. That place is an assignment operation."

"You can assign variables of different types to each other. In doing so, the value of one variable (of a particular type) is converted into a value of the other type and assigned to the other variable."

"We can point out two types of conversions: widening primitive conversions and narrowing primitive conversions. Widening is like moving things from a small basket to a big one. The procedure is unremarkable and painless. Narrowing is analogous to taking things out of a big basket and putting them in a smaller one. When you do this you may not have enough space; you may have to throw something out."

"Here are the types sorted according their 'basket' size:"

Type conversion

"Just a couple of comments:

1. char's basket is the same size as short's, but you can't move things freely from one to the other. When you move values from short to char, values that are less than 0 will always be lost. When you move values from char to short, values greater than 32,000 will be lost.

2. When you transform integers into fractional numbers, the least significant digits of the number may be thrown out. However, this is acceptable, since the purpose of a fractional number is to store an approximate value."

"When performing narrowing conversions, we must explicitly tell the compiler that we haven't made an error: we are discarding part of the number on purpose. We use a cast operator (i.e. the type name in parentheses) to do this."

"This is how you should assign variables of different types:"

Java code Description
byte a = 115;
int b = a;
Widening primitive conversion. Everything's great.
int c = 10000;
byte d = (byte) c;
Narrowing primitive conversion. We must explicitly state that the extra bytes should be discarded.
int c = 10;
byte d = (byte) c;
Narrowing primitive conversion. We must explicitly state that the extra bytes should be discarded, even if they equal 0.
float f = 10000;
long l = (long) (f * f);
float f2 = l;
long l2 = (long) f2;
When assigning to a float, a widening primitive conversion takes place. When assigning a float to a long, a narrowing primitive conversion takes place. A cast operator is required.
double d = 1;
float f = (float) d;
long l = (long) f;
int i = (int) l;
short s = (short) i;
byte b = (byte) s;
Narrowing conversions in all assignment operations except for the first line. These conversions require that we explicitly indicate the type conversion.

"A cast operator must be put before a number/variable anytime part of the number will be discarded or when a narrowing primitive conversion occurs. The cast operator only affects the number/variable that directly follows it."

Java code Description
float f = 10000;
long l = (long) f * f;
Only one of the two variables is cast to a long: multiplication of a long and a float equals a float.
float f = 10000;
long l = (long) (f * f);
The entire expression is cast to a long.

"I see."

Comments (14)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
abhishe_kira Level 18, India Expert
22 June 2023
You know I was using this (type) before even I this lesson it, with the help of intellij IDEA's error indication. LOL ; )
Hoist Level 2, San Diego, United States
4 April 2023
" The CAST operator ( in parens ) only affects the number/variable that >> directly follows it " .. codegym has great docs that are human readable without too much jargon -- that's not to say that specific and precise terminology is never useful / necessary / required.
Fadhil Radhian Level 18, Semarang, Indonesia
24 March 2023
whenever narrowing happens, use cast operator : (operand).
17 March 2020
I don't get it, if I assign int b to byte c , then narrowing conversion occur, but when I assign long d to float c , then it's widening conversion, but how come? I cannot pass every variable to float, because there will be losses, can anyone explain this to me?
Gellert Varga Level 23, Szekesfehervar, Hungary
26 June 2020
Probably because of the largeness of the range that can be represented by the type.
Darcy Xu Level 19, Irvine, United States
23 October 2020
Yeah, that's right, even float and long are the same size of 4. But float have two parts, one part of fractional, and the other part of exponent, so it's bigger than long.
Jen P Level 26
22 July 2019
I am confused about this : "When assigning a float to a long, a narrowing primitive conversion takes place. A cast operator is required." Float takes 4 bytes, while long takes 8 bytes, why this is a narrowing conversion?
filchnodead Level 15
3 August 2019
Because "long" stores integers in itself. Accordingly, when we make a “long” from “float”, we discard the fractional part, that is, we narrow the type.
Renat Mukhametshin Level 16, Pervouralsk, Russain Federation
7 August 2019
..and some other: this code does not compile

float f = 10000;
long l = (long) f * f;
and this one - does:

float f = 10000;
long l = (long) (f * f);
Darcy Xu Level 19, Irvine, United States
23 October 2020
Yeah, that's right, even float and long are the same size of 4. But float have two parts, one part of fractional, and the other part of exponent, so it's bigger than long.
polbatona Level 17, Omsk, Russian Federation
30 June 2019
2. When you transform integers into fractional numbers, the least significant digits of the number may be thrown out. However, this is acceptable, since the purpose of a fractional number is to store an approximate value." Maybe on the contrary, fractional to integers?
Muhammad Vahhaaj Level 19, Rawalpindi, Pakistan
3 July 2019
not exactly, for example you have a float variable and a double variable, since double is greater then float then we know that if a float value is assigned to double it is widening primitive conversion as double stores larger numbers. Now for example you have a number 1.2748873645 now this number when stored in float will be like 0.127488(upto 6 decimal places) and 10(exponent) and when the same number is stored in double it will be 1.27488736, as you know double type uses more space. So here you can see that the actual number is decreased(converted) in both cases but there is significant precision loss in float as it can only store 6 decimal places. So this is how conversion works they modify the actual value according to their needs
polbatona Level 17, Omsk, Russian Federation
3 July 2019
I meant something else. For example, we have the number of int = 10, which we convert to float = 10.0. We have nothing to lose, because there is nothing to lose (in the lecture it is written the opposite). And if we convert float = 10.5 to int = 10, then we lose 0.5.
Muhammad Vahhaaj Level 19, Rawalpindi, Pakistan
3 July 2019
yes you are right. When data moves from large data type to small data type possible loss can occur but if it moves from small to large then no data loss