Conversions
Widening primitive conversions:
*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
Narrowing primitive conversions:
*short to byte or char
*char to byte or short
*int to byte, short, or char
*long to byte, short, char, or int
*float to byte, short, char, int, or long
*double to byte, short, char, int, long, or float
!!integral types (byte, short, int, long and char) are promoted to floating point (flaot and double)
!!!!!!!when it comes to implicit promotion of primitive data types, the range is the deciding factor. Even though ‘long’ has 8 bytes and ‘float’ only 4, a ‘long’ can be implicitly promoted into a ‘float’ but not vice versa (you need an explicit cast to assign a ‘float’ into a ‘long’). This is because ‘float’ has a larger range than ‘long’
Char
Operations with primitives
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
Note that the implied cast to type T may be either an identity conversion or a narrowing primitive conversion.
For example, the following code is correct:
short x = 3;
x += 4.6;
and results in x having the value 7 because it is equivalent to:
short x = 3;
x = (short)(x + 4.6);
Initialisation
Elements of Arrays of primitive types are initialized to their default value ( i.e. 0 for integral types, 0.0 for float/double and false for boolean)
Elements of Arrays of non-primitive types are initialized to null.
Primitive Initialisation
For byte, short, and int, these print/println methods simply print the integer value as it is.
For char, the print/println methods translate the character into one or more bytes according to the platform’s default character encoding. That is why while printing a char value of 0, a blank space is printed instead of 0 (even though the char’s integral value is 0).
For long, float, and double values, these print/println methods use the respective primitive wrapper class’s toString method to get the String representation of that primitive. For example, to print the value of a float variable f, it internally calls Float.toString(f). Now, this method doesn’t append an “f” at the end of a float value. That is why a float value of 0.0 is printed as 0.0 and not 0.0f.
OVERLOADING:
EG:
class TestClass{
void probe(int… x) { System.out.println(“In …”); } //1
void probe(Integer x) { System.out.println("In Integer"); } //2
void probe(long x) { System.out.println("In long"); } //3
void probe(Long x) { System.out.println("In LONG"); } //4
public static void main(String[] args){
Integer a = 4; new TestClass().probe(a); //5
int b = 4; new TestClass().probe(b); //6
} }Result:
1.
probe(Integer) will be bound to probe(Integer) (exact match). If that is not available, it will be bound to probe(long), and then with probe(int…) in that order of preference.
probe(long) is preferred over probe(int…) because unboxing an Integer gives an int and in pre 1.5 code probe(long) is compatible with an int (Rule 2).
It is never bound to probe(Long ) because Integer and Long are different object types and there is no IS-A relation between them. (This holds true for any two wrapper classes).
It could, however, be bound to probe(Object ) (if it existed), because Integer IS-A Object.
2.
probe(int) is bound to probe(long) (because of Rule 2) , then to probe(Integer ) because boxing an int qives you an Integer, which matches exactly to probe(Integer), and then to probe(int…).
It is never bound to probe(Long ) because int is not compatible with Long.