Java Notes (Complete Ref &) : Get Snapshots From Notebook
Java Notes (Complete Ref &) : Get Snapshots From Notebook
com/java-tutorial)
3) WHY JAVA / Features of JAVA? (In Complete Ref P-2, Java Buzz words & https://www.javatpoint.com/features-of-java)
4) C++ vs JAVA
8) NULL
Or
byte short Int float double
char
b) Explicit / narrowing -
i) (when) Types are incompatible due to destination type being smaller than source type.
ii) (how) in two ways the actual explicit conversion operation can take place -
(1) Modulo - If the integer’s value is larger than the range of a byte, it will be reduced
modulo (the remainder of an integer division by the) byte’s range. This is mostly
performed.
(2) Truncation – when a floating-point value is assigned to an integer (including char)
type.
(3) (NOTE) even after truncation, if the size of the whole number component is too
large to fit into the target integer type, then that value will be reduced modulo the
target type’s range. For example, consider –
byte b;
double d = 323.142;
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
When d is converted to a byte, its fractional component is lost, and the value is
reduced modulo 256, which in this case is 67.
iii) (NOTE) If types are incompatible because they can’t be represented in numerical format,
then they can’t be converted in any way –
boolean a = true;
int b = (int) a; //error
boolean can’t be converted to int even though int (destination) is larger than boolean
(source) because boolean has a true or false value which, in java, is not represented in
numerical form. They are literals.
b) Rules -
i) all byte, short, and char values are promoted to int (whether int is present in expression
or not).
ii) if one operand is a long, the whole expression is promoted to long.
iii) If one operand is a float, the entire expression is promoted to float.
iv) If any of the operands is double, the entire expression is promoted to double.
c) (NOTE) As useful as the automatic promotions are, they can cause confusing compile-time
errors. For example, this seemingly correct code causes a problem:
byte b = 50;
byte a = 2
b = b * a; // Error! Cannot assign an int to a byte!
The code is attempting to store 50 * 2, a perfectly valid byte value, back into a byte variable.
However, because the operands were automatically promoted to int (even though no int
alue was present) when the expression was evaluated, the result has also been promoted to
int. Thus, the result of the expression is now of type int, which cannot be assigned to a byte
without the use of a cast.