chapter 2 Flashcards
(37 cards)
constant
data type that cannot be changed when program is running
literal constant
value taken literally at each use, e.g. numeric or boolean
unnamed constant
no identifies associated with it
variable
a named memory location
Used to store a value
Can hold only one value at a time
Its value can change
Data type
A type of data that can be stored
How much memory an item occupies
What types of operations can be performed on data
Primitive data type
a simple data type, 8 of these exist:
byte, short, int, long, float, double, char, booleon
Reference types
more complex data type such as string
Name variables
named memory locations used for legal class identifiers
variable declaration
a statement that reserves a named memory location, this includes:
Data type
Identifier
Optional assignment operator and assigned value
Ending semicolon
Assignment operator
The equal sign (=)
The value to the right is assigned to the variable on the left
Initialization
An assignment made when declaring a variable
Assignment
An assignment made after a variable is declared
Associativity
The order in which operands are used with operators
Whats the difference between declaring variables of the same type vs variables of a different type
variables of the same type may be declared on a single line whereas variables of different types must be declared on separate lines
Named constant
- Should not change during program execution
- Has a data type, name, and value
- Has a data type preceded by the keyword final
- Can be assigned a value only once
- Conventionally is given identifiers using all uppercase letters
Why use named constants
- Make programs easier to read and understand
- Enable you to change a value at one location within a program
- Reduce typographical errors
- Stand out as separate from variables
- Eliminates magic numbers
Scope
- The area in which a data item is visible to a program, and in which you can refer to it using its simple identifier
- A variable or constant is in scope from the point it is declared
- Until the end of the block of code where the declaration lies
What is a booleon
a true or false value
what is a relational operator
an operator that compares two item
what is a char data type
a data type which holds a single character
what is a String
- built in class
- stores and manipulates character strings
- string constants are written between double quotes “”
what is the issue of using a numeric scanner such as nextInt()
Keyboard buffer:
- This is a location in memory that stores all keystrokes, including enter
- As a result if the first scanner is an Integer scanner it may not pickup the enter keystroke from the buffer
- Then a scanner that registers enter may pick it up and register it as a keystroke and as a result not pick up any useful input
- To solve this you can add an extra nextLine() in order to retrieve and abandoned Enter key strokes following numeric or next() inputs
dialog box
register user input i.e. entering something via keyboard or confirming something
Input dialog box
asks question
provides text field for user response