java
Introduction
Course Title: Java CS 122
Instructor: Dr. Ben McCamish
Java Code Basics
Code Snippets and Statements
Example Java statements:
int size = 27;String name = "Fred";Dog myDog = new Dog(name, size);int x = size - 5;if (x < 15) myDog.bark(8);while (x < 3) { myDog.play(); }
Variables and Types
Java variables have explicit types:
Upper case: String
Lower case: int
User-defined: Dog
Statements in Java must end with a semicolon ( ; ).
Java Syntax Fundamentals
Conditional Statements
Conditions in Java are enclosed by parentheses.
Example:
if (x < 15)
Code Blocks
Code blocks are defined by curly braces { }, serving a similar function to indentation in Python.
Variable Declaration
All variables must be declared before use.
Variable types are fixed for the duration of their existence:
Primitive Types: Built into Java, lower-case (e.g., int, float, boolean).
Reference Types: User-defined (e.g., String, custom classes), follows CamelCase convention.
First Java Program
Basic Structure
A simple Java program contains at least one class.
public class HelloWorld { public static void main(String[] args) { String name = "Fred"; System.out.println("Hi There " + name); if (name.equals("Fred")) { System.out.println("Nice name."); } } }
Class Naming
Class filenames must match the class name ending with .java.
Each class is stored in its own file.
Visibility Modifiers and Methods
Classes can use visibility modifiers; for now, use public.
Classes may contain methods that define the behavior.
Main Method Signature
Main Method Definition
Required for program execution: public static void main(String[] args).
Breakdown:
public: accessibility
static: the method belongs to the class, not an instance
void: no return value
String[] args: accepts an array of strings as arguments.
Java Compilation Process
Source Code vs Byte Code
Source Code: Human-readable
.javafiles.Byte Code: Machine-readable
.classfiles.Compilation transforms source code into byte code using javac.
Execution is done using the java command.
Methods and Comments
Method Definition
Methods manipulate data and are defined within classes.
Multiline comments are written between
/*...*/.
Primitive Data Types
Common primitive types:
int: integer values
double: decimal values
boolean: true or false
char: single character
Less common types include float, short, long, byte.
Conditional Tests and Logical Operations
If Statements
Simple conditional test:
if (true) { System.out.println("This will always be printed"); }Using else for alternative execution.
Logical Connectives
Common logical operators:
!: NOT
&&: AND
||: OR
Loops
While Loops
Structure of a while loop:
int i = 0; while (i < 10) { System.out.println("My number is: " + i); i += 1; }Executes the code block as long as the condition is true.
For Loops
for loop structure:
for(int i = 0; i < 10; i++) { System.out.println("My number is: " + i); }Components include initialization, condition, and increment.
Arrays
Array Basics
Arrays hold a specific type of data with a predetermined length.
Array declaration and creation example:
int[] numbersILove = new int[5];
Accessing Array Elements
Access elements using the index:
numbersILove[i] = i + 3;
String Operations
Introduction to Strings
Strings utilize double quotes for literals and are stored as reference types.
Common methods include:
length(): returns the length of the string.
indexOf(String s): finds the position of a character.
String Concatenation
Strings can be combined using the + operator.
String greeting = "Hello"; String name = "Scott"; System.out.println(greeting + " " + name);
Equality Tests
Primitive and Reference Types
Primitive types use
==for equality.Reference types require both
==and.equals()to check equality.