CodeGym /Courses /New Java Syntax /Strict typing

Strict typing

New Java Syntax
Level 12 , Lesson 1
Available

"Hi, Amigo!"

"Hi, Ellie!"

"I'm in a good mood today, so I feel like telling you something interesting. I'll start with how Java's type system deals with primitive types."

"In Java, each object and each variable has its own preset unchangeable type. A primitive variable's type is determined when the program is compiled, but an object's type is determined when it is created. The type of a newly created object and/or variable remains unchanged over the course of its lifetime. Here's an example:"

Java code Description
int a = 11;
int b = 5;
int c = a / b; // c == 2
a / b – represents integer division. The answer is two. The remainder from the division operation is simply ignored.
int a = 13;
int b = 5;
int d = a % b; // d == 3
d will store the remainder of integer division of a by b. The remainder is 3.

"There are a couple of interesting nuances that you need to remember."

"First, a reference variable doesn't always point to a value that has the same type that it has."

"Second, when variables with two different types interact, they must first be converted into the same type."

"What about division? If we divide 1 by 3, we'll get 0.333(3). Right?"

"No, that's not right. When we divide two integers, the result is also an integer. If you divide 5 by 3, the answer will be 1 with two as the remainder. And the remainder will be ignored."

"If we divide 1 by 3, we'll get 0 (with reminder 1, which will be ignored)."

"But what do I do if I want to get 0.333?"

"In Java, before performing division, it's best to convert a number to a floating-point (fractional) type by multiplying by a floating-point number one (1.0)."

Java code Description
int a = 1/3;
a will be 0
double d = 1/3;
 d will be 0.0
double d = 1.0 / 3;
d will be 0.333(3)
double d = 1 / 3.0;
d will be 0.333(3)
int a = 5, b = 7;
double d = (a * 1.0) / b;
d will be 0.7142857142857143

"Got it."

Comments (9)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Fadhil Radhian Level 18, Semarang, Indonesia
24 March 2023
nicely explained
Simphiwe Level 33, Johannesburg, South Africa
23 February 2023
I got it! 💯
Daniel Whyte Level 17
7 March 2021
This lesson has turned up way after it was initially used.
20 August 2019
When I used a 'float' variable to sum 3/5 it gave the answer = 0.0 So float discards also the remainder as well.
Петр Зинченко Level 19, Kharkiv, Ukraine
23 September 2019
You didn't understand. When you execute 3/5 this return integer type of data, because two integer 3 and 5. And then you try assign integer result to float variable. You should use 3.0/5 or 3/5.0 then result is float
Alexandru Ovcinicov Level 15, London, United Kingdom
12 August 2019
d will be 0.7142857142857143 WHY ?
Pete Latham Level 15, Swindon, United Kingdom
18 September 2019
Because 5 / 7 = 0.7142857142857143
Daniel Tinsley Level 22, United States, United States
11 October 2019
d is 0.7142857142857143, since as the equation states, if a = 5 and b = 7: double d = (a * 1.0) / b; = (5 * 1.0) / 7; = 5.0 / 7 = 0.7142857142857143 Pete up above gave the wrong equation, since 5 / 7 == 0, not 0.7142857142857143, since he divided two integers and integer division doesn't give floats as a result, only another integer.
Renat Mukhametshin Level 16, Pervouralsk, Russain Federation
7 August 2019
ok, really interesting