0% found this document useful (0 votes)
4 views

#Java Lecture 1

Uploaded by

Youssef Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

#Java Lecture 1

Uploaded by

Youssef Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to Java

Programs and Execution


Java programs are written as source files with the .java extension. These programs need to
be compiled into bytecode files with the .class extension before execution. The bytecode is then run on
the Java Virtual Machine (JVM), which interprets and executes the program.

Key Points:
• Java programs must be compiled before they can be run.
• Programs can either be applets (which run in a web browser) or applications (which run on a
standalone machine).

Example: If you have a Java program like HelloWorld.java:

code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

You need to compile and run it in the following way:


1. Compile the program:

code
javac HelloWorld.java

2. Run the bytecode:


code
java HelloWorld

This will output:


Hello world!

Lexical Structure
In Java, there are several key components to understand:
• Literals: These are constant values written directly into the code, such as numbers (5, 3.14),
characters ('a'), and strings ("hello").
• Identifiers: These are names given to variables, methods, and classes (e.g., int age;).
• Reserved Words: Special keywords used by Java for control flow (if, while), operators
(instanceof, throw), and definitions (public, class, void).

Example: Here’s a simple program that uses identifiers and literals:

code
public class Test {
public static void main(String[] args) {
int age = 30; // 'int' is a reserved word, 'age' is an identifier, and '30' is a literal
System.out.println("Age: " + age); // 'Age: ' is a string literal
}
}

This will output:


Age: 30

Data Types in Java


Java has two main categories of data types:
1. Primitive types: These are the basic data types like int, boolean, char, float, etc.
2. Reference types: These include objects and arrays. Strings, lists, and user-defined objects fall
into this category.
Primitive Data Types:
• int: Represents integers, e.g., int x = 5;.
• boolean: Represents true or false, e.g., boolean isJavaFun = true;.
• char: Represents a single character, e.g., char letter = 'A';.

Example:
code
public class DataTypesExample {
public static void main(String[] args) {
int num = 10;
boolean isTrue = false;
char letter = 'C';

System.out.println("Number: " + num);


System.out.println("Boolean: " + isTrue);
System.out.println("Letter: " + letter);
}
}

Output:
Number: 10
Boolean: false
Letter: C

Control Flow: Loops and Conditionals


Java supports various control flow structures to control the execution of code based on conditions or
repetitions.

If Statements:
An if statement executes a block of code if a condition is true.
code
if (condition) {
// code to execute if the condition is true
}

Example:
code
int age = 18;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}

Output:
You are an adult.

While Loops:
The while loop executes a block of code as long as the condition is true.

Example:
code
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}

Output:
1
2
3
4
5

For Loops:
The for loop is used to execute a block of code a specific number of times.

Example:
code
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}

Output:
1
2
3
4
5

Formatted Output with printf


Java allows formatted output using the printf method, which works similarly to the printf function in C. It’s
useful when you need to control the formatting of the output, especially for numeric values.
General Format:
code
System.out.printf("%[flags][width][.precision]conversion", argument);
• %: Marks the start of a format specifier.
• conversion: A required character that specifies the type of data (%d for integers, %f for floating
points, etc.).
• width: Specifies the minimum number of characters to be printed.
• precision: Controls the number of decimal places for floating-point numbers.

Example:
code
System.out.printf("Number with 2 decimal places: %.2f\n", 3.14159);

Output:
Number with 2 decimal places: 3.14

Scope in Java
The scope of a variable refers to the part of the program where the variable can be accessed. In Java,
scope is defined by curly braces ({}).

Example of Scope:
code
public class ScopeExample {
public static void main(String[] args) {
int x = 10; // x is in scope here
if (x > 5) {
int y = 20; // y is in scope inside this block
System.out.println("x: " + x + ", y: " + y);
}
// y is out of scope here, so the following line would cause an error
// System.out.println(y);
}
}

Boolean Expressions and Logical Operators


Java supports boolean expressions using logical operators:
• ! (not)
• && (and)
• || (or)
These are used to create complex conditions within if statements or loops.

Example:
code
boolean isRaining = false;
boolean isCold = true;

if (isRaining || isCold) {
System.out.println("Stay indoors.");
} else {
System.out.println("Go outside!");
}

Output:
Stay indoors.

Loops: while, for, and do-while


This document also provides pseudocode examples for calculating the average of a set of marks using
diperent loop constructs:

For Loop Example:


code
int total = 0;
for (int i = 0; i < 5; i++) {
total += i;
}
System.out.println("Total: " + total);

While Loop Example:


code
int total = 0;
int i = 0;
while (i < 5) {
total += i;
i++;
}
System.out.println("Total: " + total);

Do-While Loop Example:


code
int total = 0;
int i = 0;
do {
total += i;
i++;
} while (i < 5);
System.out.println("Total: " + total);

You might also like