To write double literals, use one of these formats.
Digits . [Digits] [ExponentPart] [d_or_D]
. Digits [ExponentPart] [d_or_D]
Digits ExponentPart [d_or_D]
Digits [ExponentPart] [d_or_D]
Example:
0f
3.14f
9.0001e+12f
Here are examples of double literals:
2e1
8.
.5
0.0D
3.14
9e-9d
7e123D
Boolean Literals:
-The boolean type has two values, represented by literals true and false. For example, the following code declares a boolean variable includeSign and assigns it the value of true.
boolean includeSign = true;
Character Literals:
-A character literal is a Unicode character or an escape sequence enclosed in single quotes.
Primitive Conversions:
-When dealing with different data types, you often need to perform conversions.
Identity Conversion:
If both variables have the same type, the assignment will always succeed. Conversion from a type to the same type is called identity conversion.
Ex:
The Widening Conversion:
-The widening primitive conversion occurs from a type to another type whose size is the same or larger than that of the first type, such as from int (32 bits) to long (64 bits).
- The widening conversion is permitted in the following cases:
▪byte to short, int, long, float, or double
▪ short to int, long, float, or double
▪ char to int, long, float, or double
▪ int to long, float, or double
▪ long to float or double
▪ float to double
-A conversion from an int or a long to a float may result in loss of precision.
-The widening primitive conversion occurs implicitly.
Example :
int a = 10;
long b = a; // widening conversion
The Narrowing Conversion
-The narrowing conversion occurs from a type to a different type that has a smaller size, such as from a long (64 bits) to an int (32 bits).
- In general, the narrowing primitive conversion can occur in these cases:
▪ short to byte or char
▪ char to byte or short
▪ int to byte, short, or char
▪ long to byte, short, or char
▪ float to byte, short, char, int, or long
▪double to byte, short, char, int, long, or float
-Unlike the widening primitive conversion, the narrowing primitive conversion must be explicit. You need to specify the target type in parentheses.
For example, here is a narrowing conversion from long to int.
long a = 10;
int b = (int) a; // narrowing conversion
Example :
long a = 9876543210L;
int b = (int) a; // the value of b is now 1286608618
-A narrowing conversion that results in information loss introduces a defect in your program.
Operators:
In Java, there are six categories of operators.
▪Unary operators
▪Arithmetic operators
▪Relational and conditional operators
▪Shift and logical operators
▪Assignment operators
▪Other operators
Bitwise Complement Operator ~
-The operand of this operator must be an integer primitive or a variable of an integer primitive type. The result is the bitwise complement of the operand.
-we need to convert the operand to a binary number and reverse all the bits
For example:
int j = 2;
int k = ~j; // k = -3; j = 2
Shift Operators
-A shift operator takes two operands whose type must be convertible to an
integer primitive. The left-hand operand is the value to be shifted, the right-
hand operand indicates the shift distance.
- There are three types of shift
▪ the left shift operator <<
▪ the right shift operator >>
▪ the unsigned right shift operator >>>
The Left Shift Operator <<
-The left shift operator bit-shifts a number to the left, padding the right bits with 0. The value of n << s is n left-shifted s bit positions. This is the same as multiplication by two to the power of s.
For example, left-shifting an int whose value is 1 with a shift distance of 3 (1 << 3) results in 8
-Another rule is this. If the left-hand operand is an int, only the first five bits of the shift distance will be used. In other words, the shift distance must be within the range 0 and 31. If you pass an number greater than 31, only the first five bits will be used. This is to say, if x is an int, x << 32 is the same as x << 0; x << 33 is the same as x << 1.
-If the left-hand operand is a long, only the first six bits of the shift distance will be used. In other words, the shift distance actually used is within the range 0 and 63.
The Right Shift Operator >>
-The right shift operator >> bit-shifts the left-hand operand to the right. The value of n >> s is n right-shifted s bit positions. The resulting value is n/2s.
As an example, 16 >> 1 is equal to 8
The Unsigned Right Shift Operator >>>
-The value of n >>> s depends on whether n is positive or negative. For a positive n, the value is the same as n >> s.
-If n is negative, the value depends on the type of n. If n is an int, the value is (n>>s)+(2<<~s). If n is a long, the value is (n>>s)+(2L<<~s).
Promotion:
-For unary operators, if the type of the operand is byte, short, or char, the outcome is promoted to int.
-For binary operators, the promotion rules are as follows.
▪ If any of the operands is of type byte or short, then both operands will be converted to int and the outcome will be an int.
▪ If any of the operands is of type double, then the other operand is converted to double and the outcome will be a double.
▪ If any of the operands is of type float, then the other operand is converted to float and the outcome will be a float.
▪ If any of the operands is of type long, then the other operand is converted to long and the outcome will be a long.
ref: