Java supports the followiung primitive types.
long
Values of type long are integers that are 64 bits long. Use them for integers that might be fairly large. The largest integer that can be represented is roughly 8 billion billion. |
int
Values of type int are 32 bit integers. The largest integer that can be represented is roughly 2 billion. |
short
Values of type short are 16-bit integers. The largest integer that you can store in a 16-bit integer is roughly 32,000. |
byte
Values of type byte are 8-bit integers. The largest integer that you can store in an 8-bit integer is 127. |
double
A value of type double is an approximation to a real number that uses 64 bits. That gives you about 15 digits of precision. Numbers are stored in base 2, not base 10, and you need to watch out for roundoff error. For example, 0.1 (one-tenth) cannot be represented exactly in base 2 with a finite number of digits, so it is rounded, similarly to the way 1/3 (0.33333...) is rounded in based 10. |
float
A value of type float is an approximation to a real number that uses 32 bits. That gives you about 7 digits of precision. Because computers can perform many millions of operations in a single computation, roundoff error becomes a serious problem, and most programmers prefer to use type double for most real numbers. |
234
Write an integer constant in the usual way. Do not write commas in a number. |
234.5
Write real numbers in the usual way. Note that 3 is an integer but 3.0 is a real number. |
234.5E12
An E indicates a power of 10. Constant 234.5E12 is 234.5×1012 and 1.0E−30 is 1.0×10−30. |
Integers are converted to real numbers where necessary. For example, 2 is converted to 2.0 if it occurs where a value of type double is needed.
You can request conversions explicitly. If T is a numeric type, then write (T), with the parentheses, in front of an expression to convert the value of that expression to type T. For example, expression (int)(x + y) is the value of x + y converted to type int.
x + y
The sum of x and y. |
x − y
The difference, x − y. |
− x
The negation of x. |
x * y
The product x times y. |
x / y
The quotient x divided by y. Important: If x and y are both integers then x/y is also an integer. Anything after the decimal point is ignored. So 7/4 has value 1, not 1.75. |
x % y
The remainder when you divide integer x by integer y. For example, when you divide 14 by 3 you get a quotient of 4 and a remainder of 2. So 14 % 3 = 2. You can only use the % operator on integers. |
Operator * has higher precedence than operator +. For example, expression 2 + 5 * 2 has value 12, since the multiplication is done first.
Operators *, / and % have the same precedence, and operators + and − have the same precedence. All of them are performed from left to right, except where precedence rules and parentheses force otherwise. For example, 10 − 2 − 3 has value 5. Notice that 16/2*2 has value 16 since * and / have the same precedence. The division 16/2 is done first, and then the result of that division is multiplied by 2. If you want 16/(2*2), be sure to use parentheses.
If you import java.math.Math, then you can use the following functions. In all cases, x and y have type double, n has type int and L has type long. Variables u and v can be of any numeric type.
Math.sqrt(x)
Approximately the square root of x. |
Math.pow(x, y)
Approximately xy, where x and y are real numbers. |
Math.abs(x)
The absolute value of x. |
Math.max(u, v)
The larger of u and v. For example, Math.max(3,7) = 7. This works for any numeric type. |
Math.min(u, v)
The smaller of u and v. For example, min(3,7) = 3. This works for any numeric type. |
Each size of integer has a largest allowed positive value. If you add 1 to that number, you will not get an error. Instead, the result will be a negative number. For example, the largest value that can be stored in a variable of type short is 32,767. Statement
short n = 32767; short m = n + 1;makes m = −32768, the smallest negative number that can be stored in a variable of type short.
Why do most programmers prefer to use type double instead of type float for most uses? Answer
What is the value of expression 9 − 6 + 3? Answer
What is the value of expression 3/5? Answer
What is the value of expression 25 % 4? Answer
What is the value of expression 0 % 5 Answer
What is the value of expression Math.sqrt(25.0) Answer
What is the value of expression Math.max(3,8) Answer
Is [3 + 2] * 8 an allowed Java expression? Answer