Title: Data and Expressions (Java Software Solutions Foundations of Program Design, 10th Edition)
Authors: John Lewis, William Loftus
Copyright: © 2024 Pearson Education, Inc.
Character strings
Primitive data
Declaration and use of variables
Expressions and operator precedence
Data conversions
Accepting input from the user
Definition: A string literal is enclosed in double quotes.
Example:
"This is a string literal."
"123 Main Street"
"X"
String Class: Every character string is an object defined by the String class in Java.
String Literal: Each string literal creates a String object.
Usage: Invokes the println method to print output; it advances to the next line after printing.
Example:System.out.println("Whatever you are, be a good one.");
Object: System.out
Method: println
Parameters: Information to print.
Functionality: Similar to println, but does not advance to the next line.
Example Code: Countdown program demonstrates this difference.
Key Point: Output following print appears on the same line.
Operator: +
is used to concatenate strings and append numbers.
Example:
"Peanut butter " + "and jelly"
Limitations: String literals cannot span multiple lines within the program.
Definition: A variable is a memory location name that holds a value.
Declaration: Specifies variable name and data type.
Example: int total;
Initialization: Assigning an initial value during declaration, e.g., `int sum = 0;
Multiple variables can be declared at once, like int count, temp, result;
.
Changes the value of a variable using the =
operator.
Example: total = 55;
Restrictions: Value assigned must match variable's declared type.
Definition: Identifier that holds a constant value throughout.
Declaration: Use final
modifier, e.g., final int MIN_HEIGHT = 69;
Purpose:
Clarifies literal values.
Eases program maintenance by setting values in one place.
Prevents inadvertent changes to variable.
Categories:
Integers: byte, short, int, long
Floating-point: float, double
Characters: char
Boolean values: boolean
Size and Value Details: Numeric types vary in size and range.
Definition: Combination of operators and operands.
Operators: Includes arithmetic operations and evaluations.
Operator Precedence: Determines evaluation order, with multiplication and division having higher precedence than addition.
Purpose: To handle different data types conveniently.
Types of Conversion:
Widening Conversion: Safe conversion from smaller to larger types.
Narrowing Conversion: Risks loss of information, converting larger to smaller types.
Methods: Assignment, promotion, casting procedures.
Utility: Facilitates reading user input conveniently from various sources, including keyboard.
Example: Declaring a Scanner to read input and methods to capture various data types.
Example Usage in Code:
Scanner scan = new Scanner(System.in);
Input Reading: Utilize Scanner to obtain user input and print results.
Example: Reading miles and gallons for calculations (GasMileage program).
Focused on foundational programming concepts, including character strings, primitive data types, variables, expressions, and user input.