Chapter 2 Flashcards
What are identifiers in Java?
Identifiers are the names that identify elements such as classes, methods, and variables in a program.
What can an identifier be made up of?
An identifier can be made up of letters, digits, the underscore character (_), and the dollar sign ($).
How must an identifier start in Java?
An identifier must start with a letter (A-Z, a-z), an underscore (_), or a dollar sign ($). It cannot begin with a digit.
Is Java case sensitive when it comes to identifiers?
Yes, Java is case sensitive. Total
, total
, and TOTAL
are different identifiers.
What is the convention for naming class identifiers in Java?
By convention, class names use title case (e.g., Lincoln
).
What is the convention for naming constants in Java?
By convention, constants are written in upper case (e.g., MAXIMUM
).
What is lower camel case in Java?
Lower camel case is when the first letter of each word is uppercase, except the first one (e.g., dayOfWeek
).
What is the convention for naming method and variable identifiers in Java?
Methods and variables start with a lowercase letter (e.g., myMethod()
).
What do variables do in Java programs?
Variables store data in memory and are used to “remember” information in the program.
What are the components of a variable in Java?
A variable has a name, data type, and value.
Example: int count = 1;
What must a variable declaration specify in Java?
A variable declaration must specify the variable’s name and the type of information it will hold.
What is an example of declaring multiple variables in one statement?
```java
int total = 0, count = 0, temp = 0, result = 0;
~~~
What are global variables in Java?
Global variables exist throughout the entire program’s life and are accessible from any part of the program.
What are local variables in Java?
Local variables exist only temporarily, until the block or bracket in which they were declared is closed.
What is the general programming practice regarding global variables in Java?
It is generally good programming practice to avoid using global variables, but sometimes they are necessary.
This is for the sake of preserving memory
what does it mean to initialize a variable?
it means to give it its initial value
Why do we need to specify a data type when creating a variable in Java?
A data type is required so the system can reserve the appropriate amount of memory for the variable.
What are the two basic groupings of data types in Java?
The two basic groupings of data types are:
- Primitive Data Types
- Class Data Types (e.g., Strings)
What are primitive data types in Java?
Primitive data types, also known as ‘Built In’ data types, are the building blocks of data manipulation in Java. They are stored in memory directly with their value.
What is the default value for primitive data types in Java?
The default value for primitive data types is 0.
Are primitive data types in Java case sensitive?
Yes, primitive data types in Java are all lowercase. String is not a primitive type.
How many primitive data types are there in Java?
There are 8 primitive data types: byte, char, short, int, long, float, double, and boolean.
What are the two categories of primitive numeric data types in Java?
The two categories are:
- Integers: byte, short, int, long
- Floating-point numbers: float, double
What is the difference between float
and double
in Java?
float
: Stores decimal numbers with single precision (e.g., 3.14159).double
: Stores decimal numbers with double precision and more decimal places.