"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 


a / b – represents integer division. The answer is two. The remainder from the division operation is simply ignored. 

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 floatingpoint (fractional) type by multiplying by a floatingpoint number one (1.0)."
Java code  Description 


a will be 0 

d will be 0.0 

d will be 0.333(3) 

d will be 0.333(3) 

d will be 0.7142857142857143 
"Got it."
GO TO FULL VERSION