Java Identifiers, Primitivies, & Reference Types Flashcards
Legal Java identifies can only being with the following characters:
A letter
The $ character
The _ character
Can numbers be used in Java identifiers?
Yes but it can’t be the first character in the variable name.
If a Java identifier is composed only of letters, is it always a legal variable name?
No. Java reserved words can not be used as variable names.
With the exception of the first character, legal Java identifies can only contain the following characters:
Letters
Numbers
The $ character
The _ character
What structures in Java are named using identifiers?
Variables
Methods
Classes
Fields
What is a variable?
A variable is a name for a piece of memory that stores
data.
What are the minimum requirements when declaring a variable in Java?
At a minimum, a variable must have a variable type and an identifier (or name).
Java has two types of data variables, what are they?
Primitive Types
Reference Types
How many primitive types (or primitives) does Java have?
8
What are the Java primitive types?
boolean byte short int long float double char
How much memory is required for a byte? What values can it hold? What is its default value?
8-bits or 1 byte
-128 to 127
0
How much memory is required for a short?What values can it hold?What is its default value?
16-bits or 2 bytes
-32,768 to 32,767
0
How much memory is required for an int? What values can it hold? What is its default value?
32-bits or 4 bytes
-2^31 to (2^31)-1
0
How much memory is required for a long? What values can it hold? What is its default value?
64-bits or 8 bytes
-2^63 and a maximum value of (2^63)-1
0L
How much memory is required for a float?
32-bits or 4 bytes
How much memory is required for a double?
64-bits or 8 bytes
How much memory is required for a char in Java 8? What values can it hold? What is its default value?
16-bits or 2 bytes
A Unicode character
‘\u0000’
What is the default value of a boolean?
false
Does the following line of code compile?
long max = 3123456789;
Why or why not?
No. When using literal values, Java treats numbers like ints. This value is greater than the maximum int value. To fix the problem append ‘L’ to make Java treat this is a long.
long max = 3123456789L;
How do you specify that a literal is an octal value?
Prefix the number with a 0 and only use characters 1 - 7.
Example: 017
How do you specify that a literal is a hexadecimal value?
Prefix the number with a 0x or 0X followed by the values 0 - 9 and/or A - F.
Example: 0xA4
How do you specific that a literal is a binary value?
Prefix the number with a 0b or 0B followed by the values 0 - 1.
Example: 0b10110101
Does the following code compile in Java 8?
int million = 1_000_000;
Why or why not?
Yes. As of Java 7, there was a feature added to numeric literals allowing you to add underscores to the number to make them easier to read.
You can add underscores anywhere except at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point.
Does the following code compile in Java 8?int million = _1000000;Why or why not?
No. As of Java 7, there was a feature added to numeric literals allowing you to add underscores to the number to make them easier to read.
You can add underscores anywhere except at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point. This has an underscore at the beginning.