Data Types Flashcards
What happens at compile time if a local variable remains uninitialized?
Compile time error.
Local variables must be explicitly initialized.
What is the default value for a boolean instance variable if it is not explicitly initialized?
False.
What are the primitive data types in Java
boolean
char
byte, short, int, long,
float and double.
The ~
operator
Bitwise negation.
Also called the unary operator, it operates only on integral types.
Maybe think of it as the proofreading symbol for swapping two words.
integral data type
a type that can contain integers.
They correspond to the primitive types:
byte, short, int and long
What data integrity risk is present when converting an int
to a float
?
Loss of precision.
A value of float type is only precise out to nine digits.
Integer.MIN_VALUE
A constant of the Integer class in the java.lang package representing the smallest value an integer variable can store.
-2^31 = -2147483648
Integer.MAX_VALUE
A constant of the Integer class in the java.lang package representing the largest value an integer variable can store.
2^31 - 1 = 2147483647
(That number is a Mersenne prime)
What are the two Boolean class constructors?
Boolean(String) and Boolean(boolean)
What does the Boolean(String) constructor do?
It allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string “true”.
Otherwise, it allocates a Boolean object representing the value false.
What are the Boolean class static helper methods?
parseBoolean
and valueOf
T/F: Conversion from char to long does not need a cast
True.
Conversion from a smaller primitive to a larger does not need a cast.
T/F: Conversion from int to float needs a cast.
False.
However, conversion from float to int does need a cast.
T/F: A final variable that fits into a smaller type still needs a cast for it to be assigned.
False. The compiler already knows its value. This is called implicit narrowing.
Does implicit narrowing apply to all primitive types?
No. It only applies to byte, short, int and char.
It does not apply to long, float, or double.
Can a long, float, or double be implicitly narrowed using the final keyword?
No.
Do wrapper classes have a no-args constructor?
No.
This means the statement new Integer()
is invalid.
What does this code do:
String foo = “145”;
Long.valueOf(foo).longValue();
Long.valueOf(foo) returns a Long object containing “145”
longValue()
returns 145.
Can chaining be used to assign a value to a variable at its time of declaration?
e.g. int a = b = c = 42;
No.
In this example, if c
was not previously declared, this will not compile:
int a = b = c = 42;
Is this code valid?
String s = 'a';
No.
‘a’ is a char.
“a” is a string.
What class does every class directly or indirectly extend?
Object
What happens to a number that falls exactly between two numbers when rounding?
It always rounds up.
Is this a valid assignment:
Runnable r = new Thread( );
Yes.
Thread implements the interface Runnable.
What is the legal range for a byte?
-128 to 127
Byte is an 8-bit signed integer.
Is this a valid integer variable declaration:
Int x = 10;
No.
Int with capital ‘I’ is not valid.
The wrapper class that can hold an int
is Integer.
What does the equals method of a wrapper class first check?
It first checks if the two objects are of the same class or not. If not, it returns false.
How do you make an object eligible for garbage collection?
By making sure there are no references pointing to it.
What conditions must be met for primitive conversion be used?
When all of the following are met:
- Expression is compile time constant of type byte, short, int or char.
- Variable type is byte, short or char.
- Value of expression can be represented in the type of variable.
T/F: All local variables are initialized automatically and given default values if not explicitly initialized.
False.
Note: this is the case for all instance and static variables.
T/F: It’s okay to leave a local variable not initialized if it is not used in the code.
True.
But why do it?
What does equals( )
return?
It can be overridden, so you can’t tell unless you look at the code.
What does hashCode( )
return?
It can be overridden, so you can’t tell unless you look at the code.
Does the following code compile?
System.out.println(null + true);
No. The operator +
cannot be applied to or boolean.