Instructor: Chris Franson
UID: [UID details not provided]
Definition: Identifiers are names that represent elements: variables, constants, methods, classes, and packages in a program.
Character Composition: Composed of letters, digits, underscores (_), and dollar signs ($).
Rules:
Must start with a letter, underscore, or dollar sign.
Cannot start with a digit.
Cannot be a reserved word.
Cannot be true
, false
, or null
.
Can be of any length.
Case Sensitivity: Java is case sensitive; e.g., Area
, area
, and AREA
are different identifiers.
Java differentiates between upper and lower case:
Examples:
"John"
vs "john"
"car"
vs "CAR"
"int"
vs "Int"
"if"
vs "If"
, where if
is a valid Java keyword.
Variables store data used throughout a program.
Purpose: To hold user input, e.g., reading the radius from the user.
Naming: Choose descriptive names (e.g., radius
for radius).
Declaration Syntax: datatype variableName;
Definition: A variable is a symbol that can hold/change values.
Variable Example Syntax:
int radius;
// Declare an integer variable
double area;
// Declare a double variable
Primitive Data Types (8 total):
boolean: true or false.
char: 16-bit character.
Integer types:
byte
: -128 to 127.
short
: -32768 to 32767.
int
: -2147483648 to 2147483647.
long
: -9223372036854775808 to 9223372036854775807.
Floating-Point Types:
float
: 32-bit floating-point.
double
: 64-bit floating-point.
Single Declaration Example:
int count;
// Declare count to be an integer variable
Multiple Declaration Example:
double radius, area;
// Declare multiple variables of the same type.
Definition: Assigns a value to a variable after declaration.
Syntax: variable = expression;
// =
is the assignment operator.
Examples:
int x; x = 1;
double radius; radius = 1.0;
char a; a = 'A';
Example: int radius = 1;
// Declare and initialize in one step.
Others: char firstLetter = 'A';
, String aString = "A";
Represents a single character in Java (ASCII or Unicode).
Character Literal: Enclosed in single quotes.
Examples:
char aLetter = 'A';
char aDigit = '8';
char uLetter = '\u0041';
Step 1: Read the circle's radius from user input.
Step 2: Compute area: area = π * radius * radius
.
Step 3: Display the result.
System.out: Refers to output device (monitor).
System.in: Refers to input device (keyboard).
Scanner Class: Use to read input; necessitates import java.util.Scanner;
Example Creation: Scanner input = new Scanner(System.in);
Write a program to:
Convert Celsius to Fahrenheit.
Compute area from radius and length of a cylinder.
Find averages from keyboard inputs.
Undeclared Variables: Accessing variables before declaration/initialization.
Integer Overflow: Exceeding the max value for a type, e.g., int value = 2147483647 + 1;
Round-off and Division Errors: Example with 1.0 - 0.1 * 5
. Check the output to notice inaccuracies in floating-point operations.
Larger data type takes precedence in operations.
Examples: If one operand is double, other will be converted to double.
Implicit Casting: Automatic conversion from a smaller to a larger type.
Explicit Casting: For converting a larger type to a smaller or specific type, e.g., truncating a double.
Example: int i = (int) 3.9; // Result is 3
.