You are on page 1of 95

Introduction to Java

Outline

Environment setup

Basic Syntax

Data types

Operations

Input & Output

Decision making

Loops

Methods

Java ( ROBOTO Academy )


Java - Environment Setup

Java SE is freely available from the link.

https://www.oracle.com/technetwork/java/javas
e/downloads/jdk8-downloads-2133151.html

You can download a version based on your
operating system.

Java ( ROBOTO Academy )


Java - Environment Setup

Follow the instructions to download Java and
run the .exe to install Java on your machine.

Once you installed Java on your machine, you
will need to set environment variables to point
to correct installation directories.

Java ( ROBOTO Academy )


Setting Up the Path for Windows

Assuming you have installed Java in c:\Program Files\java\jdk directory

Right-click on 'My Computer' and select 'Properties'.

Click the 'Environment variables' button under the 'Advanced' tab.

Now, alter the 'Path' variable so that it also contains the path to the
Java executable.

Example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then
change your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\
java\jdk\bin'.

Java ( ROBOTO Academy )


Popular Java Editors

To write your Java programs, you will need a text editor.

Netbeans − A Java IDE that is open-source and free
which can be downloaded from
https://www.netbeans.org/index.html

Eclipse − A Java IDE developed by the eclipse open-
source community and can be downloaded from
https://www.eclipse.org/

Java ( ROBOTO Academy )


Java - Basic Syntax

When we consider a Java program, it can be
defined as a collection of objects that
communicate via invoking each other's
methods.

Let us now briefly look into what do class,
object, methods, and instance variables mean.

Java ( ROBOTO Academy )


Java - Basic Syntax

Object − Objects have states and behaviors.
– Example: A dog has states - color, name, breed as well as behavior such as wagging
their tail, barking, eating. An object is an instance of a class.

Class − A class can be defined as a template/blueprint that describes the
behavior/state that the object of its type supports.

Methods − A method is basically a behavior. A class can contain many
methods. It is in methods where the logics are written, data is manipulated
and all the actions are executed.

Instance Variables − Each object has its unique set of instance variables. An
object's state is created by the values assigned to these instance variables

Java ( ROBOTO Academy )


Java - Basic Syntax
It is very important to keep in mind the following points:

Case Sensitivity − Java is case sensitive, which means
identifier Hello and hello would have different meaning in Java.

Class Names − For all class names the first letter should be in
Upper Case. If several words are used to form a name of the
class, each inner word's first letter should be in Upper Case.
– Example: class MyFirstJavaClass

Java ( ROBOTO Academy )


Java - Basic Syntax

Method Names − All method names should start with a Lower Case
letter. If several words are used to form the name of the method,
then each inner word's first letter should be in Upper Case.
– Example: public void myMethodName()

Program File Name − Name of the program file should exactly
match the class name.

public static void main(String args[]) − Java program processing
starts from the main() method which is a mandatory part of every
Java program.

Java ( ROBOTO Academy )


Java - Basic Syntax

When saving the file, you should save it using
the class name (Remember Java is case
sensitive) and append '.java' to the end of the
name

if the file name and the class name do not
match, your program will not compile.

Java ( ROBOTO Academy )


Java - Basic Syntax

Example :


Output:

Java ( ROBOTO Academy )


Java Identifiers

All Java components require names.

Names used for classes, variables, and methods are called identifiers.

In Java, there are several points to remember about identifiers. They are as follows −
– All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an
underscore (_).
– After the first character, identifiers can have any combination of characters.
– A key word cannot be used as an identifier.
– Most importantly, identifiers are case sensitive.

Examples of legal identifiers: age, $salary, _value, __1_value.

Examples of illegal identifiers: 123abc, -salary.

Java ( ROBOTO Academy )


Java Variables

Following are the types of variables in Java −
– Local Variables
– Class Variables (Static Variables)
– Instance Variables (Non-static Variables)

Java ( ROBOTO Academy )


Java Keywords

The following list shows the reserved words in
Java.

These reserved words may not be used as
constant or variable or any other identifier
names.

Java ( ROBOTO Academy )


Java Keywords

Java ( ROBOTO Academy )


Java - Basic Datatypes

Variables are nothing but reserved memory locations to store values.

This means that when you create a variable you reserve some
space in the memory.

Based on the data type of a variable, the operating system allocates
memory and decides what can be stored in the reserved memory.

Therefore, by assigning different data types to variables, you can
store integers, decimals, or characters in these variables.

Java ( ROBOTO Academy )


Java - Basic Datatypes

There are two data types available in Java −
– Primitive Data Types
– Reference/Object Data Types

Java ( ROBOTO Academy )


Primitive Data Types

There are eight primitive datatypes supported
by Java:

byte short int long

float double boolean char

Java ( ROBOTO Academy )


Primitive Data Types
Int

Integer is generally used as the default data
type for integral values unless there is a
concern about memory.

The default value is 0

Example: int a = 100000, int b = -200000
Java ( ROBOTO Academy )
Primitive Data Types
float

Float is mainly used to save memory in large
arrays of floating point numbers

Default value is 0.0f

Example: float f1 = 234.5f

Java ( ROBOTO Academy )


Primitive Data Types
double

This data type is generally used as the default
data type for decimal values, generally the
default choice

Default value is 0.0d

Example: double d1 = 123.4
Java ( ROBOTO Academy )
Primitive Data Types
boolean

There are only two possible values: true and false

This data type is used for simple flags that track
true/false conditions

Default value is false

Example: boolean one = true

Java ( ROBOTO Academy )


Primitive Data Types
char

Char data type is used to store any character

Example: char letterA = 'A'

Java ( ROBOTO Academy )


Java - Variable Types

A variable provides us with named storage that our
programs can manipulate.

Each variable in Java has a specific type, which
determines
– the size and layout of the variable's memory
– the range of values that can be stored within that memory
– the set of operations that can be applied to the variable.

Java ( ROBOTO Academy )


Java - Variable Types

You must declare all variables before they can
be used.

Following is the basic form of a variable
declaration
data_type variable = value ;

Java ( ROBOTO Academy )


Java - Variable Types

Variable declaration form consists of 3 parts :
data_type variable = value ;
1) data_type : One of Java's data types such as int for
numbers , boolean for true or false etc..
2) Variable : The name of the variable such as x
3) Value : the value that the variable will store on memory.

Java ( ROBOTO Academy )


Activity

Open your IDE

Create new project and give it the name :
MyFirstJavaProgram

Create new class , what will you name the class?

Create the main function :
public static void main(String []args)

Java ( ROBOTO Academy )


Activity

Inside your main function define 4 variables :
– One of type int called number and give it a value.
– One of type boolean called flag and give it a value.
– One of type char (give it a name and a value)
– One of type float (also give it a name and a value)

Don't forget the semi colons ! ( ; )

Java ( ROBOTO Academy )


Activity

Solution :

Java ( ROBOTO Academy )


Java - Basic Operators

Java provides a rich set of operators to manipulate variables.

We can divide all the Java operators into the following groups −
– Arithmetic Operators
– Relational Operators
– Bitwise Operators
– Logical Operators
– Assignment Operators
– Misc Operators

Java ( ROBOTO Academy )


The Arithmetic Operators

Arithmetic operators are used in mathematical
expressions.
Operator Description

+ (Addition) Adds values on either side of the operator.

- (Subtraction) Subtracts right-hand operand from left-hand operand.

* (Multiplication) Multiplies values on either side of the operator.

Java ( ROBOTO Academy )


The Arithmetic Operators

/ (Division) Divides left-hand operand by right-hand operand.

% (Modulus) Divides left-hand operand by right-hand operand and returns


remainder.

++ (Increment) Increases the value of operand by 1.

-- (Decrement) Decreases the value of operand by 1.

Java ( ROBOTO Academy )


The Arithmetic Operators

Assume integer variable A holds 10 and
variable B holds 20 :
– A + B will give 30 - B % A will give 0
– A - B will give -10 - B++ gives 21
– A * B will give 200 - B-- gives 19
– B / A will give 2

Java ( ROBOTO Academy )


The Relational Operators

There are following relational operators
supported by Java language.
Operator Description

== (equal to) Checks if the values of two operands are equal or not,
if yes then condition becomes true.

!= (not equal to) Checks if the values of two operands are equal or not,
if values are not equal then condition becomes true.

> (greater than) Checks if the value of left operand is greater than the
value of right operand, if yes then condition becomes
true.Java ( ROBOTO Academy )
The Relational Operators

< (less than) Checks if the value of left operand is less than the value of
right operand, if yes then condition becomes true.

>= (greater than or Checks if the value of left operand is greater than or equal to
equal to) the value of right operand, if yes then condition becomes true

<= (less than or equal Checks if the value of left operand is less than or equal to the
to) value of right operand, if yes then condition becomes true.

Java ( ROBOTO Academy )


The Relational Operators

Assume integer variable A holds 10 and
variable B holds 20 :
– (A == B) is not true(false)
– (A != B) is true.
– (A > B) is not true. - (A < B) is true.
– (A >= B) is not true. - (A <= B) is true.

Java ( ROBOTO Academy )


The Logical Operators

The following table lists the logical operators.
Operator Description

&& (logical and) Called Logical AND operator. If both the operands are
non-zero, then the condition becomes true.

|| (logical or) Called Logical OR Operator. If any of the two operands


are non-zero, then the condition becomes true.

! (logical not) Called Logical NOT Operator. Use to reverses the


logical state of its operand. If a condition is true then
Logical NOT operator will make false.
Java ( ROBOTO Academy )
The Logical Operators

Assume Boolean variables A holds true and
variable B holds false :
– (A && B) is false
– (A || B) is true
– !(A && B) is true

Java ( ROBOTO Academy )


The Assignment Operators

Following are the assignment operators
supported by Java language.
Operator Description

= Simple assignment operator. Assigns values from right side


operands to left side operand.

+= Add AND assignment operator.


It adds right operand to the left operand and assign the
result to left operand.
-= Subtract AND assignment operator.
It subtracts right operand from the left operand and assign
the resultJava
to (left
ROBOTO Academy )
operand.
The Assignment Operators

*= Multiply AND assignment operator.


It multiplies right operand with the left operand and assign the
result to left operand.
/= Divide AND assignment operator.
It divides left operand with the right operand and assign the
result to left operand.
%= Modulus AND assignment operator.
It takes modulus using two operands and assign the result to
left operand.

Java ( ROBOTO Academy )


The Assignment Operators

Assume integer variable A and variable B:
– C = A + B will assign value of A + B into C
– C += A is equivalent to C = C + A
– C -= A is equivalent to C = C – A
– C *= A is equivalent to C = C * A
– C /= A is equivalent to C = C / A
– C %= A is equivalent to C = C % A

Java ( ROBOTO Academy )


Precedence of Java Operators

Operator precedence determines the grouping of
terms in an expression.

This affects how an expression is evaluated.

Certain operators have higher precedence than
others.

for example, the multiplication operator has higher
precedence than the addition operator

Java ( ROBOTO Academy )


Precedence of Java Operators
1) expression++ , expression--
2) * , / , %
3) + , - 4) == , !=
5) & 6) ||
7)= , += , -= , *= , /= , %=

Java ( ROBOTO Academy )


Java Input and Output

All the programming languages provide support
for standard I/O where the user's program can :

Take input from a keyboard

And then produce an output on the computer
screen.

Java ( ROBOTO Academy )


Java Input and Output
Java provides the following three standard streams −

Standard Input − Usually a keyboard is used as standard input
stream and represented as System.in.

Standard Output − This is used to output the data produced by the
user's program and usually a computer screen is used for standard
output stream and represented as System.out.

Standard Error − This is used to output the error data produced by
the user's program and usually a computer screen is used for
standard error stream and represented as System.err.

Java ( ROBOTO Academy )


Java Input and Output

Example:

Java ( ROBOTO Academy )


Java Input and Output

Import : allows you to use InputStreamReader
built in library.

Throws IOException : to avoid program crash if
any reading error occurred.

Java ( ROBOTO Academy )


Java Input and Output

System.out.println() : used to print a certain
value to the screen.

Java ( ROBOTO Academy )


Java - Decision Making

Decision making structures have:
– One or more conditions to be evaluated or tested by
the program
– a statement or statements that are to be executed if
the condition is determined to be true
– optionally, other statements to be executed if the
condition is determined to be false.

Java ( ROBOTO Academy )


Java - Decision Making

Following is the general form of a typical decision
making structure found in most of the programming
languages

Java ( ROBOTO Academy )


Java - Decision Making
1)if statement : An if statement consists of a boolean
expression followed by one or more statements.
2)if...else statement : An if statement can be followed by
an optional else statement, which executes when the
boolean expression is false.
3)Nested if statement : You can use one if or else if
statement inside another if or else if statement(s).

Java ( ROBOTO Academy )


if statement in java

An if statement consists of a Boolean expression
followed by one or more statements.

Syntax :
if(Boolean_expression) {
// Statements will execute if the Boolean expression
is true
}

Java ( ROBOTO Academy )


if statement in java

If the Boolean expression evaluates to true then
the block of code inside the if statement will be
executed.

If not, the first set of code after the end of the if
statement (after the closing curly brace) will be
executed.

Java ( ROBOTO Academy )


if statement in java

Example:


What is the final value of x after execution ?

Java ( ROBOTO Academy )


if statement in java

Answer:


x will be 50.

Because x was 10 so the if condition is true
x < 20 10 < 20

So will execute the statement x = 50;

Java ( ROBOTO Academy )


if-else statement in java

An if statement can be followed by an optional else statement, which
executes when the Boolean expression is false.

Syntax :
if(Boolean_expression) {
// Executes when the Boolean expression is true
}else {
// Executes when the Boolean expression is false
}

Java ( ROBOTO Academy )


if-else statement in java

If the boolean expression evaluates to true,
then the if block of code will be executed,

otherwise else block of code will be executed.

Java ( ROBOTO Academy )


if-else statement in java

Example:


What is the final value of x after execution ?

Java ( ROBOTO Academy )


if-else statement in java

Answer:


x will be 30.

Because x was 10 so the if condition is false
x > 20 10 > 20

So will execute else block of code which is x=30;

Java ( ROBOTO Academy )


Nested if statement in java

Means you can use one if or else if statement inside another if or else if
statement.

Syntax :
if(Boolean_expression 1) {
// Executes when the Boolean expression 1 is true
if(Boolean_expression 2) {
// Executes when the Boolean expression 2 is true
}
}

Java ( ROBOTO Academy )


Nested if statement in java

Example:


What is the final value of x after execution ?

Java ( ROBOTO Academy )


Nested if statement in java

Answer:


x is 15.

Because x was 5 so the first if is true

The inner if is false because y is 10

Then the inner else is executed which is x = 15;

Java ( ROBOTO Academy )


Java - Loops

There may be a situation when you need to execute a
block of code several number of times.

In general, statements are executed sequentially: The first
statement in a function is executed first, followed by the
second, and so on.

Programming languages provide various control structures
that allow for more complicated execution paths.

Java ( ROBOTO Academy )


Java - Loops

A loop statement allows us to execute a
statement or group of statements multiple times
based on a condition.

Java ( ROBOTO Academy )


Java - Loops
1)While loop
2)For loop
3)Do .. while loop

Java ( ROBOTO Academy )


while Loop in java

A while loop statement in Java programming language
repeatedly executes a target statement as long as a
given condition is true.

Syntax :
while(Boolean_expression) {
// Statements
}

Java ( ROBOTO Academy )


while Loop in java

When executing, if the boolean_expression result is
true, then the actions inside the loop will be executed.

This will continue as long as the expression result is
true.

When the condition becomes false, program control
passes to the line immediately following the loop.

Java ( ROBOTO Academy )


while Loop in java

Here, key point of the while loop is that the loop
might not ever run.

When the expression is tested and the result is
false, the loop body will be skipped and the first
statement after the while loop will be executed.

Java ( ROBOTO Academy )


while Loop in java

Example :


Guess what the output is.

Java ( ROBOTO Academy )


while Loop in java

Output :

Java ( ROBOTO Academy )


for loop in java

A for loop is a repetition control structure that
allows you to efficiently write a loop that needs
to be executed a specific number of times.

A for loop is useful when you know how many
times a task is to be repeated.

Java ( ROBOTO Academy )


for loop in java

Syntax
for(initialization; Boolean_expression;
update) {
// Statements
}

Java ( ROBOTO Academy )


for loop in java
Here is the flow of control in a for loop −

The initialization step is executed first, and only once.

This step allows you to declare and initialize any loop control
variables and this step ends with a semi colon ( ; ).

Next, the Boolean expression is evaluated.

If it is true, the body of the loop is executed.

If it is false, the body of the loop will not be executed and control
jumps to the next statement past the for loop.

Java ( ROBOTO Academy )


for loop in java

After the body of the for loop gets executed, the control jumps back
up to the update statement.

This statement allows you to update any loop control variables.

This statement can be left blank with a semicolon at the end.

The Boolean expression is now evaluated again.

If it is true, the loop executes and the process repeats (body of loop,
then update step, then Boolean expression).

After the Boolean expression is false, the for loop terminates.

Java ( ROBOTO Academy )


for loop in java

Example:


What is the output ?

Java ( ROBOTO Academy )


for loop in java

Output:

Java ( ROBOTO Academy )


do while loop in java

A do...while loop is similar to a while loop, except that a
do...while loop is guaranteed to execute at least one
time.

Syntax
do {
// Statements
}while(Boolean_expression);

Java ( ROBOTO Academy )


do while loop in java

Notice that the Boolean expression appears at the end of
the loop, so the statements in the loop execute once
before the Boolean is tested.

If the Boolean expression is true, the control jumps back
up to do statement, and the statements in the loop
execute again.

This process repeats until the Boolean expression is false.

Java ( ROBOTO Academy )


do while loop in java

Example:

Java ( ROBOTO Academy )


do while loop in java

Output:

Java ( ROBOTO Academy )


do while loop in java

What is the output of the following code?

Java ( ROBOTO Academy )


do while loop in java

Output :


Although the condition x < 10 is not true as x
started with value 10 but the body executed as we
mentioned that the body executes at least once

Java ( ROBOTO Academy )


Java - Methods

A Java method is a collection of statements that
are grouped together to perform an operation.

For example,when you call the
System.out.println() method, the system
actually executes several statements in order to
display a message on the console.

Java ( ROBOTO Academy )


Java - Methods

Syntax :
public static int methodName(int a, int b) {
// body
}

public static − modifier

int − return type

methodName − name of the method

a, b − formal parameters

int a, int b − list of parameters

Java ( ROBOTO Academy )


Java - Methods

Modifier − It defines the access type of the method and it is optional to use.

Return type − Method may return a value.

Name of method − This is the method name.

Method signature − consists of the method name and the parameter list.

Parameter List − The list of parameters, it is the type, order, and number of
parameters of a method. These are optional, method may contain zero
parameters.

Method body − The method body defines what the method does with the
statements.

Java ( ROBOTO Academy )


Java - Methods

Example:


This method takes 2 numbers and return the
sum of the two numbers.

How to call a method ?

Java ( ROBOTO Academy )


Java - Methods

How to call a method :


Type the method’s name and give it the parameters it needs.

The method’s return value is now stored in the variable sum.

Java ( ROBOTO Academy )


Method Calling

For using a method, it should be called.

There are two ways in which a method is called : method returns a value or
returning nothing (no return value).

The process of method calling is simple.

When a program invokes a method, the program control gets transferred to the
called method.

This called method then returns control to the caller in two conditions, when −
– the return statement is executed.
– it reaches the method ending closing brace.

Java ( ROBOTO Academy )


Method Calling

The methods returning void is considered as
call to a statement. Example :
System.out.println("This is tutorialspoint.com!");

The method returning value can be understood
by the following example :
int result = sum(6, 9);
Java ( ROBOTO Academy )
The void Keyword

The void keyword allows us to create methods
which do not return a value.

Example :
● Notice:

No return statement.

Java ( ROBOTO Academy )


Activity

Write a method that calculates the area of a
rectangle given the width and the height of it.

Write your main function and define 2 variables
holding the width and the height.

Call your method and print the result.

Make sure your variables can hold fractions.
Java ( ROBOTO Academy )
Activity

Solution :

Java ( ROBOTO Academy )


Activity

Output :

Java ( ROBOTO Academy )


Thank you !

Java ( ROBOTO Academy )

You might also like