ch 2
Method return value
- A method can return either zero or one value . - A method's return value can be of any type (including a class type).
expression
- a number like 80, a variable name like numApples, or a simple calculation like numApples + 1. or - a combination of items, like variables, literals, operators, and parentheses, that evaluates to a value, like 2 * (x + 1)
identifier must be:
- be a sequence of letters (a-z, A-Z), underscore (_), dollar signs ($), and digits (0-9) - start with a letter, underscore, or dollar sign
x = 2 y = 3 x = x * y x = x * y
18. becuase x is first assigned with 2. Then x is assigned with 2 * 3, or 6. Finally, x is assigned with 6 * 3, or 18.
y = 30 x = y + 2 x = x + 1
33. x is first assigned with 30 + 2 or 32. Then x is assigned with 32 + 1 or 33.
identifier
A name created by a programmer for an item like a variable or method
= is not equals
In programming, = is an assignment of a left-side variable with a right-side value. = is NOT equality as in mathematics. Thus, x = 5 is read as "x is assigned with 5", and not as "x equals 5". When one sees x = 5, one might think of a value being put into a box.
x + y = y + x invalid
In programming, the left side of = must be a variable, which will be assigned with the right side's value. Thus, having x + y on the left side doesn't make sense.
incrementing
Increasing the value of a variable by 1. ex: x = x + 1
public methods
Methods that can be accessed by entities outside the object.
why x + 1 = 3 is invalid?
The left side must be a variable, not an expression like x + 1. In programming, = does not mean equal. = means assign the left-side variable with the right-side's value. Thus, the left side MUST be a variable.
Initializing variables
To assign a variable a value. Ex: int maxScore = 100
Would using two assignment statements as below have yielded the same result? Assume this declaration exists: int totalMonthly totalMonthly = paymentPerMonth * numMonths; totalCost = downPayment + totalMonthly;
Yes, The evaluation is ultimately the same, yielding the same result. Breaking larger expressions into multiple assignments with smaller expressions can sometimes improve code readability.
Would removing the parentheses as below have yielded the same result? downPayment + paymentPerMonth * numMonths
Yes, because the * would still be evaluated first, because * has higher precedence than +. But using parentheses makes the programmer's intent more clear to people reading the program.
variable declaration
a statement that reserves a named memory location
operator
a symbol that performs a built-in calculation, like +, which performs addition.
object
consists of some internal data items plus operations that can be performed on that data. An object's internal data are called private fields, as the data is private to the object.
precedence
evaluated using the order of standard mathematics,
integer literal
is an integer value that is typed into a program's code. For example: itemsOrdered = 15;In this code, 15 is an integer literal.
Scanner's nextDouble()
method is used to read a floating-point value from input. Ex: currentTemp = scnr.nextDouble() reads a floating-point value from the input and assigns currentTemp with that value.
method arguments
provide additional input values to influence the method's behavior
Compound operators
shorthand way to update a variable. Ex: userAge += 1 being shorthand for userAge = userAge + 1. or +=, -=, *=, /=, %=. Also known as combined assignment operators and arithmetic assignment operators.
Common Error
to read a variable that has not yet been assigned a value. Ex: Ex: numKids + numAdults = numPeople, or 9 = beansCount.
What memory location (address) will a compiler allocate for the variable declaration below. If appropriate, type: Unknown
unknown. The programer doesn't know the address( location).
nteger variables
used for values that are counted, like 42 cars, 10 pizzas, or -95 days.
Floating-point
used for values that are measured, like 98.6 degrees, 0.00001 meters, or -666.667 grams. Also, used when dealing with fractions of countable items, such as the average number of cars per household.