Java An Introduction to Problem Solving and Programming Chapter 2.1 (Notes)

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/14

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

15 Terms

1
New cards

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.

2
New cards

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.

3
New cards

Primitive Types

• byte
• short
• int
• long
• float
• double
• char
• boolean

4
New cards

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.

5
New cards

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;

6
New cards

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.

7
New cards

Simple Input

Use the standard class Scanner to accept keyboard input.

8
New cards

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

9
New cards

Named Constants

• Name important constants all in capital letters and underscores.

10
New cards

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.

11
New cards

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.

12
New cards

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.

13
New cards

Parentheses and Precedence Rules

• From highest precedence to lowest precedence:
- +=; -=; !; ++; and --;
- *; /; and %.
- + and -.
• Precedence rules and parentheses determine the order of operations.

14
New cards

Specialized Assignment Operators

• You can combine an arithmetic operator with = as a shorthand notation.
example: amount += 5; is the same as amount = amount + 5;

15
New cards

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.