java - What is the value of the variable? int iNum = 10/3 -
Is there not a data type int that is used only for integer? 10/3 is a decimal because it is a decimal, does it mean that it will create an error? It depends on the language for your example (Java), this zero (called "trunk" Operation is also called) to be rounded. So the value becomes 3. Try it yourself:
int i = 10/3; Println (i); // Display 3 inch J-10/3; Println (j); // shows -3 double k = 10/3; System.out.println (k) // Display 3.00 Double M = ((double) 10) / 3; System.out.println (k) // shows 3.33 double mee = 10.0 / 3; System.out.println (k) // shows 3.33
In each case, two operations are actually running. Considering the first example,
Step 1: Calculate the value of [integer 10]
divided by [integer 3]
. The answer is [integer 3]
as described above.
Step 2: Assign the [integer 3]
to the RHS value from the left hand side. [integer 3]
is assigned to [integer i]
. So I'm equal to 3.
Because the operation is done in two steps, the third operation using double
also gives 3.00, does not give 3.33. The precision is already lost when stage 2 starts.
Comments
Post a Comment