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.