Primitives Flashcards

1
Q

Conversions

A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Char

A
  • A char can be used as un unsigned integer!
  • Can hold chacters and integral values as well (0 to 2^16 -1.)
    However: Integer 0 or 1, 2 etc. is not same as char ‘0’, ‘1’ or ‘2’ (which hold the UNICODE/ASCII values)!!!!!!
    Eg:
    char a = 0;
    char b = ‘0’;
    System.out.println(“a=”+ a +”, b=”+ b +”, a==b –> “+ (a == b)); a
    prints: a=empty space; b=0; a!=b !!!!!!!!!!!!
    And:
    char a = 48;
    char b = ‘0’;
    prints: a=0; b=0; a==b–> true!!!!!!!!!!!!!!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Operations with primitives

A
  1. Anything bigger than an int can NEVER be assigned to an int or anything smaller than int ( byte, char, or short) without explicit cast.
  2. CONSTANT values up to int can be assigned (without cast) to variables of lesser size ( for example, short to byte) if the value is representable by the variable.( that is, if it fits into the size of the variable).
  3. operands of mathematical operators are ALWAYS promoted to AT LEAST int. (i.e. for byte * byte both bytes will be first promoted to int.) and the return value will be AT LEAST int.!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  4. Compound assignment operators ( +=, *= etc) have strange ways so read this carefully:

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Initialisation

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Primitive Initialisation

A
  1. the default values that are given to variables of various primitive types. You should remember that all numeric types, including char, get the value of 0 (or 0.0 for float and double) and boolean gets the value of false.
  2. how the value is printed by System.out.print method - java.lang.System class has a public static variable named out, which is of class java.io.PrintStream. The PrintStream class has multiple print/println methods for printing out primitive values as well as objects.

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A

OVERLOADING:

  1. The compiler always tries to choose the most specific method available with least number of modifications to the arguments.
  2. Java designers have decided that old code should work exactly as it used to work before boxing-unboxing functionality became available.
  3. !!!!!!!!!!!!Widening is preferred to boxing/unboxing (because of rule 2), which in turn, is preferred over var-args!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly