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.
Does the following code compile in Java 8?int million = 1_00_0.0_0;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.This doesn’t actually make the number easier to read but it is legal.
What is a literal?
A Java literal is a syntactic representation of Boolean, character, numeric, or string data.
How do you represent boolean literals?
With the keywords true or false.
When using a numeric literal if there are no other qualifiers, what type does it default to?
An int.
Can you assign a long literal to an int if the long value is less than the maximum size of an int?
No.
Can you assign a binary literal value to an int if the binary value is less than the maximum size of an int?
Yes. This is also true of hexadecimal and octal values.
What is appended to a numerical literal to denote that it is a long?
An L. You can use either the upper or lowercase L, however the uppercase L is recommended as the lowercase L looks like the number 1 in some fonts.
What does a reference type refer to?
An object.
Do reference types refer to primitives?
Not directly. A reference type always points to an object. However, the object could be nothing but a wrapper for a primitive.
What are the 2 ways are values assigned to a reference?
A reference can be made to point to an existing object of the same type.
A reference can be made to point to a new object using the new keyword.
Can a reference be assigned a value of null?
Yes.
Can a primitive be assigned a value of null?
No.
Can a reference be used to call a method?
Yes.
Can a primitive be used to call a method?
No.
How do you declare multiple variables on a single line?
There are two ways. Example:
int a; int b; int c;
OR
int a, b, c;
How do you initialize multiple variables on a single line?
There are two ways. Example:
int a = 1; int b = 2; int c = 3;
OR
int a = 1, b = 2, c = 3;
Can you declare variables of different types on the same line without a semicolon?
No. The following is an error: int a, String b; However: int a; String b; is acceptable.
When using only a comma to declare multiple variables, how many variable type keywords can you use (between semicolons)?
Only 1. Example:
OK: double d1, d2, d3;
BAD: double d1, double d2;
What is a local variable?
A local variable is a variable defined within a method.
Do local variables have a default value?
No. They have garbage data in them. Code with local variables will not compile if the variable is used before being initialized.
What is an instance variable?
An instance variable is any variable declared outside a method that is not declared static. They are so named because they are unique to their instance of the object.
What is a class variable?
A class variable is any variable declared static and outside a method. They are so named because their is only one for all the objects made from the class.
Which variables do not need to be initialized?
instance and class variables.
What is the scope of a variable?
Scope is where in the code a variable can be accessed.
How do you determine the scope of a variable?
A variable is not directly accessible outside the code block in which it was declared. However, as in the case of the class variable, it may be accessible across multiple objects.
Stated another way:
Local Variables: in scope from declaration to end of block
Instance Variables: in scope from declaration until object garbage collected
Class Variables: in scope from declaration until program ends.