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

Lec 02- Introduction to Java Part 01

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

Lec 02- Introduction to Java Part 01

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

CS SE 1209 & IT 1210

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 owned by Oracle, and more than 3 billion devices run Java.

• It is used for:

✓ Mobile applications (Specially Android apps)


✓ Desktop applications
✓ Web applications
✓ Web servers and application servers
✓ Games
✓ Database connection
✓ And much, much more!

2
Lecturer Name Saegis Campus
Why use Java?
• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)

• It is one of the most popular programming language in the world

• It has a large demand in the current job market

• It is easy to learn and simple to use

• It is open-source and free

• It is secure, fast and powerful

• 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

Some PCs might have Java already installed.

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

• Any code inside the main() method will be executed.

• 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

You can also use the println() method to print numbers.

However, unlike text, we don't put numbers inside double quotes:

11
Lecturer Name Saegis Campus
Java Comments
Comments can be used to explain Java code, and to make it more readable.

It can also be used to prevent execution when testing alternative code.

Single-line Comments
• Single-line comments start with two forward slashes
(//).

• Any text between // and the end of the line is


ignored by Java (will not be executed).

• This example uses a single-line comment before a


line of code.

12
Lecturer Name Saegis Campus
Java Comments

Java Multi-line Comments


• Multi-line comments start with /* and
ends with */.

• Any text between /* and */ will be


ignored by Java.

• This example uses a multi-line comment


(a comment block) to explain the code.

13
Lecturer Name Saegis Campus
Variables are containers for storing data values.

In Java, there are different types of variables, for example:

• 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

Declaring (Creating) Variables

To create a variable, you must specify the type and assign it a


value:

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 can contain letters, digits, underscores, and dollar signs

• Names must begin with a letter

• Names should start with a lowercase letter and it cannot contain whitespace

• Names can also begin with $ and _

• 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.

A String variable contains a collection of characters surrounded by double quotes:

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:

• Use if to specify a block of code to be executed, if a specified condition is true


• Use else to specify a block of code to be executed, if the same condition is false
• Use else if to specify a new condition to test, if the first condition is false
• Use switch to specify many alternative blocks of code to be executed

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.

Java While Loop

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 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

38
Lecturer Name Saegis Campus
Java for loop

• Statement 1 sets a variable before the loop starts (int i =


0).

• Statement 2 defines the condition for the loop to run (i


must be less than 5). If the condition is true, the loop will
start over again, if it is false, the loop will end.

• Statement 3 increases a value (i++) each time the code


block in the loop has been executed.

39
Lecturer Name Saegis Campus
Questions??

40
Lecturer Name Saegis Campus
41
Lecturer Name Saegis Campus

You might also like