1/14
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Variables
ā¢ A variable is a program component used to store or represent data.
ā¢ variables represent memory locations.
ā¢ Choose meaningful variable names.
ā¢ Declare variables before using them.
Data Types
ā¢ A data type specifies a set of values and operations.
ā¢ Class types and primitive types.
ā¢ A floating-point number has a fractional part.
ā¢ Single quotes enclose a character.
Primitive Types
ā¢ byte
ā¢ short
ā¢ int
ā¢ long
ā¢ float
ā¢ double
ā¢ char
ā¢ boolean
Java Identifiers
ā¢ Java is case sensitive.
ā¢ Legal identifiers:
- Can contain only letters.
- Can contain digits from 0 - 9.
- And underscore character ( _ ).
ā¢ Illegal identifiers:
- First first character can not be a digit.
- The name may not have a space.
- can not contain characters such as dot ( . ) or an asterisk ( * ).
ā¢ Java keywords have special meanings.
Assignment Statements
ā¢ An assignment statement gives a value a variable.
ā¢ " * " means mulitply
ā¢ The same variable can occur on both sides of the " = "
Example: count = count + 10;
Programmer Tip
ā¢ You can initialize a variable when you declare it.
ā¢ Variables that are not been given a value when declared is known as uninitialized.
ā¢ All primitive variables have a default value, it has been known to change over time and should not be depended on.
Simple Input
Use the standard class Scanner to accept keyboard input.
Constants
ā¢ A constant does not change in value.
ā¢ Java's e notation is like scientific notation.
example: 8650000000 is the same as
- 8.65 x 10^8 or 8.65e8
- the e8 resembles 10^8
Named Constants
ā¢ Name important constants all in capital letters and underscores.
Assignment Compatibilities
ā¢ A value can be assigned to a variable whose type allows more precision.
- byte -> short -> int -> long -> float -> double
- small cups can fit in bigger cups, but bigger cups can't fit in smaller cups.
Type Casting
ā¢ A type cast changes the data type of a value.
- Remember the cups.
- To make a big cup fit a smaller cup use type casting.
example: double distance = 9.9;
int points = distance;
points value will be 9.
ā¢ Truncation discards the fractional part.
- 9.9 will be 9, it doesn't round up or down, but instead removes that after the dot.
Arithmetic Operators
ā¢ An arithmetic expression combines operands, operators, and parentheses.
ā¢ Operands in an arithmetic expression can have mixed data types.
ā¢ Integer division truncates the result.
ā¢ The % operator gets the remainder after division.
Parentheses and Precedence Rules
ā¢ From highest precedence to lowest precedence:
- +=; -=; !; ++; and --;
- *; /; and %.
- + and -.
ā¢ Precedence rules and parentheses determine the order of operations.
Specialized Assignment Operators
ā¢ You can combine an arithmetic operator with = as a shorthand notation.
example: amount += 5; is the same as amount = amount + 5;
Increment and Decrement Operators
ā¢ Increase operator ++.
ā¢ Decrease operator --.
ā¢ Be careful if you use the operators ++ and -- in expressions.
- int n = 3;
int m = 4;
int result = n * (++m);
will have value of 15.
- int n = 3;
int m = 4;
int result = n * (m++);
will have value of 12.