Lec 02- Introduction to Java Part 01
Lec 02- Introduction to Java Part 01
Lecture 02
Introduction to Java
07/05/2024
Bachelor of Science (Hons) in Computer Science | Software Engineering | Information Technology
Department of Computing
Faculty of Computing and Technology
Saegis Campus
Nugegoda
1
Lecturer Name Saegis Campus
What is Java?
• Java is a popular programming language, created in 1995.
• It is used for:
2
Lecturer Name Saegis Campus
Why use Java?
• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• Java is an object oriented language which gives a clear structure to programs and allows code to be
reused, lowering development costs
• As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa
3
Lecturer Name Saegis Campus
Getting Started
To check if you have Java installed on a Windows PC, search in the start bar for Java or type the following in Command
Prompt (cmd.exe):
If Java is installed, you will see something like this (depending on version):
4
Lecturer Name Saegis Campus
Getting Started
Java Install
If you do not have Java installed on your computer, you can download it for free at oracle.com.
5
Lecturer Name Saegis Campus
Java Syntax
6
Lecturer Name Saegis Campus
Java Syntax
• The main() method is required, and you will see it in every Java program
• The curly braces {} marks the beginning and the end of a block of code.
• System is a built-in Java class that contains useful members, such as out, which is short for "output". The
println() method, short for "print line", is used to print a value to the screen (or a file).
• You should also note that each code statement must end with a semicolon (;).
• every Java program has a class name which must match the filename, and that every program must
contain the main() method.
7
Lecturer Name Saegis Campus
Java Output/Print
Inside the main() method, we can use the println() method to print a line of text to the screen:
8
Lecturer Name Saegis Campus
Java Output/Print
You can add as many println() methods as you want. Note that it will add a new line for each method:
9
Lecturer Name Saegis Campus
The print() method
10
Lecturer Name Saegis Campus
Java Output Numbers
Print Numbers
11
Lecturer Name Saegis Campus
Java Comments
Comments can be used to explain Java code, and to make it more readable.
Single-line Comments
• Single-line comments start with two forward slashes
(//).
12
Lecturer Name Saegis Campus
Java Comments
13
Lecturer Name Saegis Campus
Variables are containers for storing data values.
• String - stores text, such as "Hello". String values are surrounded by double quotes
• int - stores integers (whole numbers), without decimals, such as 123 or -123
• float - stores floating point numbers, with decimals, such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
• boolean - stores values with two states: true or false
14
Lecturer Name Saegis Campus
Java Print Variables
• The println() method is often used to display variables.
• Create a variable called name of type String and assign it the value "John":
To create a variable that should store a number, look at the following example:
15
Lecturer Name Saegis Campus
Java Print Variables
You can also declare a variable without assigning the value, and assign the value later:
16
Lecturer Name Saegis Campus
Java Print Variables
To combine both text and a variable, use the + character:
17
Lecturer Name Saegis Campus
Java Variables
The general rules for naming variables are:
• Names should start with a lowercase letter and it cannot contain whitespace
• Names are case sensitive ("myVar" and "myvar" are different variables)
• Reserved words (like Java keywords, such as int or boolean) cannot be used as names
18
Lecturer Name Saegis Campus
Data Types
19
Lecturer Name Saegis Campus
Java Strings
Strings are used for storing text.
20
Lecturer Name Saegis Campus
Java User Input
• The Scanner class is used to get user input, and it is found in the java.util package.
• To use the Scanner class, create an object of the class and use any of the available
methods found in the Scanner class documentation. In our example, we will use the
nextLine() method, which is used to read Strings:
21
Lecturer Name Saegis Campus
Java User Input In the example below, we used the nextLine() method,
which is used to read Strings.
22
Lecturer Name Saegis Campus
Java User Input Types
23
Lecturer Name Saegis Campus
Activity
1. Write a program to take two integer inputs from user and print sum of them.
2. Ask user to give two float input for length and breadth of a rectangle and print
area of rectangle.
24
Lecturer Name Saegis Campus
If-Else
Java has the following conditional statements:
25
Lecturer Name Saegis Campus
If Statement
Use the if statement to specify a block of Java code to be executed if a condition is true.
26
Lecturer Name Saegis Campus
If Statement
27
Lecturer Name Saegis Campus
The else statement
Use the else statement to specify a block of code to be executed if the condition is false.
28
Lecturer Name Saegis Campus
The else statement
29
Lecturer Name Saegis Campus
The elseif statement
Use the else if statement to specify a new condition if the first condition is false.
30
Lecturer Name Saegis Campus
The elseif statement
31
Lecturer Name Saegis Campus
Activity
Write a java program to input a number and check if it is positive, negative, or zero using
if conditions.
32
T.L.Navodi Sithara Saegis Campus
While Loop
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more readable.
The while loop loops through a block of code as long as a specified condition is true:
33
Lecturer Name Saegis Campus
While loop
In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:
34
Lecturer Name Saegis Campus
Activity
Write a java program to find the sum of numbers from 1 to 100 using
a while loop
35
Lecturer Name Saegis Campus
The Do/While loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the
condition is true, then it will repeat the loop as long as the condition is true.
36
Lecturer Name Saegis Campus
The Do/While loop
The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false,
because the code block is executed before the condition is tested:
37
Lecturer Name Saegis Campus
Java for loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been executed.
38
Lecturer Name Saegis Campus
Java for loop
39
Lecturer Name Saegis Campus
Questions??
40
Lecturer Name Saegis Campus
41
Lecturer Name Saegis Campus