chapter 2 Flashcards
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
showinputdialog() method
-six overloaded versions
-returns a string representing the user response
One version requires:
Parent component
Message
Title
Type of dialog box
Type-wrapper classes
- Primitive data types have corresponding classes contained in java, lang package
- These include methods to process primitive values e.g.
- Integer.parseInt()
- Double.ParseDouble()
What five arguments are needed to confirm a dialog box
- Parent component
- Promp message
- Title
- Integer that indicated which option button to show
- Integer that describes the kind of dialog box
Standard Arithmetic operators
preform calulations with values in programs
operand
a value used either side of an operator
integer division
two integers are divided and the result is also an integer meaning any fractional part of the result is lost,
What is operator precedence
rules for the order in which mathematical expressions are evaluated
Multiplication, division, remainder (modulus), additon or subtraciton
What are the rules of Type conversion
- Artithmetic variables or constants of same type return results of the same variable/constant type
- Arithmetic operations with unlike operands will return a result of a unifying type which Java chooses, basically java converts the values to a common type in order for it to work
Automatic Type conversion
the automatic conversion of nonconforming operands into a unifying operand type
what is the order of establishing unifying types betweeen two variables from highest to lowest
Double
Float
Long
Int
what is type casting
forcing a value of one data type to be usef as value of another data type
how do you use a cast operator
place desired value in parentheses
using a cast operator is an example of an explicit conversoin
when do you not need to use a cast operator
when assigning a value to a higher unifying type e.g. Float to Double