Java Programming Notes
Introduction to Java
Instructor: Dr. Christian Schönberg
Institution: Carl von Ossietzky University, Oldenburg
Learning Objectives
Understanding properties, terminology, and operation of Java
Variable declaration and assignment
Understanding blocks
Using conditional statements (if)
Implementing loops (while)
Writing effective comments
Recognizing common errors in Java programming
What is Java?
Java is an imperative and object-oriented programming language.
It consists of a collection of development tools and a class library.
Key concepts in Java include:
Java SE/EE: Standard Edition and Enterprise Edition.
JRE (Java Runtime Environment): Used for executing Java applications.
JDK (Java Development Kit): A set of tools for developers to create Java applications.
JVM (Java Virtual Machine): An engine that converts Java bytecode into machine code.
How Java Works
Compilation Process:
Source files (*.java)
Java Compiler -> converts to Java Bytecode (*.class)
Execution by Java Virtual Machine:
JVM for different operating systems (Windows, Linux, macOS)
This enables the same Java application to run on different operating systems.
Properties of Java
Simplicity: Easy to learn and use features.
Object-Oriented: Supports concepts of objects and classes.
Platform Independence: Write once, run anywhere (WORA).
Robust: Strong memory management.
Security: Convenient security features.
Fast: Performance comparable to native languages.
Distributed Computing: Handling distributed applications.
Multithreaded: Supports multiple threads of execution.
Dynamic: Adaptability at runtime.
Static Type Checking: Ensures type correctness at compile time.
Widely Used: Extensive global community and applicability.
Free: Open-source and free to use.
Java Program Template
public class <CLASS_NAME> {
public static void main(String[] args) {
<CODE>
}
}
Save as
<CLASS_NAME>.java
Example Program: Factorial Calculation
public class Factorial {
public static void main(String[] args) {
int number = 5;
int result = 1;
while (number > 0) {
result = result * number;
number = number - 1;
}
System.out.println(result);
}
}
Explanation of Structure:
Class named Factorial.
Main method where execution starts.
Declares integer variables to compute the factorial.
Execution of Example Program
Compiling and running:
Compile using:
javac Factorial.javaRun using:
java Factorial
Output for 5! is: 120
Java Bytecode Representation:
Each method is converted to binary for execution by the JVM, making the execution quick.
Development Environments
Example of development environment: Eclipse IDE
Project structure example:
Package Explorer structure for
Factorial.java
Variables and Instructions
Variables:
Placeholders for values.
Steps in handling variables:
Declare a variable: Specify name and type (e.g.,
int number).Assign a value: (
number = 5).Query current value: (
number > 0).Reassign new values: (
number = number - 1).
Naming Conventions:
Initial character: lowercase followed by letters/numbers.
Cannot use reserved keywords.
Examples of valid variable operations:
int number; number = 5; number > 0; number = number - 1;
Common Example Program Parts (Continued)
Arithmetic Operations:
Basic operations: Addition, Subtraction, Multiplication, Division.
Order of operations applies; use parentheses for clarity.
Example code for operations:
java int x = 5 + 4; int y = 9 - 3 * 2; int z = x / (y - 1); System.out.println(z);
Statements
Definition: A complete command in Java, such as a variable declaration or a calculation.
Always ends with a:
;Example statements:
java int x = 7; int y = 5 * x;
Structure: Blocks
Definition of a Block: A sequence of statements enclosed in
{ }.Execution: Statements are executed sequentially within blocks.
Example:
{
int number = 5;
int result = 1;
{
result = result * number;
number = number - 1;
}
System.out.println(result);
}
Scope of Variables
Definition: Variables are valid in the block where defined, and in any nested subblocks.
Example:
{
int x = 5;
{
int y = x + 3;
int z = 7;
}
x = z - 2;
}
Conditional and Repeated Statements
Conditional Statements (if): Executing code blocks depending on conditions.
Standard Structure:
if (condition) {
// statements executed if the condition is true
}
Example:
int x = ...;
if (x == 3) {
System.out.println(x);
}
Nested Conditional Statements
More sophisticated conditional logic can be implemented using
else ifblocks.Example:
int x = ...;
if (x == 0) {
System.out.println(0);
} else if (x > 10) {
System.out.println(100 / x);
} else if (x > 0) {
System.out.println(10 / x);
} else {
System.out.println(-1 * x);
}
Loop Structures (while)
Definition: Loops repeat a block of statements as long as a condition is true.
While loop example:
int x = 0;
while (x < 3) {
x = x + 1;
System.out.println(x);
}
Condition checking: The program evaluates if the condition holds true at the start of each iteration.
Code Readability
Comments:
Non-interpreted text in code to enhance understanding and explain functionality, behaviors, and conditions.
Types of comments include single line and multi-line comments.
Example of comment formatting:
/* This is a multi-line comment */
// This is a single line comment
Code conventions: Include appropriate naming patterns, spacing, and structuring to maintain clarity.
Typical Errors
Common programming errors include:
Syntax errors (incorrect syntax)
Missing initializations (uninitialized variable usage)
Runtime errors (dividing by zero)
Infinite loops due to faulty loop conditions.
Example of errors in code:
while (x < 3) {
x = x + 1;
}
Conclusion
Java is a versatile and powerful programming language used globally for various applications. Here, we covered its essentials, including syntax, properties, and common practices to navigate programming in Java.