Java 1 Study Guide
Essay Preview: Java 1 Study Guide
Report this essay
Chapter 2
Java Fundamentals
Common escape Sequences
(Newline),
(Horizontal tab), (Backspace),
(Return)
Identifiers – a programmer defined name that represents some element of a program
First character must be a letter, an underscore, or a dollar sign.
Identifiers cannot include spaces.
Java Key Words – core of the java language and show up blue when typing code.
Primitive Data Types (in order of precedence)
Short
Float
double
The boolean Data Type allows you to create variables that may hold one of two possible values: True or False.
The char data type is used to store characters (letters).
The final key word can be used in a variable declaration to make the variable a named constant. Named constants are initialized with a value, and that value cannot change during the execution of the program.
The String class allows you to create objects for holding strings. It also has various methods that allow you to work with strings.
A variables Scope is the part of the program that has access to the variable.
Reading Keyboard Input – Objects of the Scanner class can be used to read input from the keyboard. Example:
import java.util.Scanner;
Chapter 3
Decision Structures
The if statement causes one or more statements to execute only when a Boolean expression is true.
The if-else-if Statement is a chain that performs a series of tests until one of the tests is found to be true.
A nested if statement is an if statement in the conditionally executed code of another if statement.
Logical Operators connect two or more relational expressions into one or reverse the logic of an expression. Example: && operator connects two Boolean expressions requiring that they are both true to pass the testing statement.
The Conditional Operator can be used to create short expressions that work like if-else statements. Example: number = x > 100 ? 20 : 50 Read as if (x > 100) number = 20; else number = 50;
The DecimalFormat class can be used to format the appearance of floating-point numbers rounded to a specified number of decimal places. Example: import java.text.DecimalFormat;
Chapter 4
Loops and Files
The Increment and Decrement Operators ++ and – are operators that add and subtract one from their operands. Example: number = 1; number++
Increment and decrement operators can be written in postfix mode (number++) or prefix mode (++number). With prefix mode, the original value of number will never be tested because it will add 1 immediately.
The while Loop is part of a program that repeats. It has two important parts: (1) a Boolean expression that is tested for a true or false value, and (2) a statement or block of statements that is repeated as long as the expression is true.
The while Loop is a pretest loop, which means it tests its expression before each iteration.
Infinite Loops are loops that do not contain a way to terminate within themselves.
Using the while Loop for input validation. The while loop can be used to create input routines that repeat until acceptable data is entered.
The do-while Loop is a posttest loop, which means its Boolean expression is tested after each iteration. The do-while loop always performs at least one iteration.
The for Loop is ideal for performing a known number of iterations.
For (Initialization; Test; Update)
The for Loop is a pretest loop.
You can declare the variable in the for Loops Initialization Expression.
for (int number = 1; number <=10; number++)
for (Initialization; Test; Update)
You can use multiple statements in the Initialization and Update Expressions. Example: for (x = 1, y = 1; x <= 5; x++)
A running total is a sum of numbers that accumulates with each iteration of a loop. The variable used to keep the running total is called an accumulator. A sentinel is a value that signals when the end of a list of values has been reached.
The accumulator usually must be set to 0.0 for the program to run correctly.
A sentinel value is a special value chosen that must not be able to be mistaken as a member of the looping list, such as a -1 in a list of soccer game scores.
A loop that is inside another loop is called a Nested loop.
An inner loop goes through all of its iterations for each iteration of an outer loop (seconds within minutes)
Inner loops complete their iterations before outer loops do.
Deciding which Loop to use:
The while loop is a pretest loop. It is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning. It is also ideal if you want to use a sentinel value to terminate the loop.
The do-while loop is a posttest loop. It is ideal in situations where you always want the loop to iterate at least once.
The for loop is a pretest loop that has built-in expressions for initializing, testing, and updating. These expressions make it very convenient to use a loop control variable as a counter. The for loop is ideal in situations where the exact number of iterations is known.
Introduction to