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

1 Java Introduction

Uploaded by

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

1 Java Introduction

Uploaded by

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

Java Basic for Tester

Java Introduction

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 1


Agenda

 History of Java
 Features of Java Programming
 Java Hello World
 Setting up the environment in Java
 Java JVM, JRE and JDK
 Java Data Types
 Java Operators
 Java Input and Output
 Java Expressions & Blocks
 Java Comment
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 2
History of Java

Java is a powerful general-purpose programming language. It is used to


develop desktop and mobile applications, big data processing, embedded
systems, and so on.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 3


History of Java

The Very first


version was
released on January
23, 1996. The
principal stable
J2SE 1.3 J2SE 5.0 JAVA SE 7 JAVA SE 9 JAVA SE 11
variant, JDK 1.0.2, is
May 2000 September 2004 July 2011 September 2017 September 2018
called Java 1.

1995 1998 2000 2002 2004 2006 2011 2014 2017 2018 2018 2019

JDK 1.1 J2SE 1.4 JAVA SE 6 JAVA SE 8 JAVA SE 10 JAVA SE 12


February 1997 February 2002 December 2006 March 2014 March 2018 March 2019

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 4


Features of Java Programming

Simple
Object-
Dynamic
Oriented

Architecture
Secured
neutral
Features
Robust
of Java Platform
independent

High
Distributed
Performance
Multithreaded

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 5


Java Hello World

A "Hello, World!" is a simple program that outputs Hello, World! on the


screen.
The requirement for Java Hello World Example:

1. Install the JDK if you don't have installed it, download the JDK and install it.
2. Set path of the jdk/bin directory.
3. Install IntelliJ IDEA Community Edition, download and install it.
4. Create the java program
5. Compile and run the java program

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 6


Java Hello World

Java "Hello, World!" Program

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 7


Java Hello World

How Java "Hello, World!" Program Works?


// Your First Program

In Java, any line starting with // is a


comment.
Comments are intended for users reading
the code to better understand the intent
and functionality of the program.
It is completely ignored by the Java
compiler (an application that translates
Java program to Java bytecode that
computer can execute).
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 8
Java Hello World

How Java "Hello, World!" Program Works?


class HelloWorld { ... }

In Java, every application begins with a


class definition.
In the program, HelloWorld is the name of
the class, and the class definition is:

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 9


Java Hello World

How Java "Hello, World!" Program Works?


public static void main(String[] args) { ... }

This is the main method. Every application


in Java must contain the main method.
The Java compiler starts executing the
code from the main method.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 10


Java Hello World

How Java "Hello, World!" Program Works?


System.out.println("Hello, World!");

The following code prints the string inside


quotation marks Hello, World! to standard
output (your screen).

Notice, this statement is inside the main


function, which is inside the class
definition.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 11


Java Hello World

Notes
The compiler executes
the codes starting from
the main function

The main method must


be inside the class
definition

Every valid Java


Application must have a
class definition

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 12


Setting up the environment in Java

There are few things which must be clear before setting up the
environment
JVM: JVM
(Java
Virtual
Machine)

JRE(Java
Runtime
Environm
ent)

JDK(Java
Development
Kit)

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 13


Setting up the environment in Java

Step 1) Java JDK is available at Download Java.


Click second last link for Windows(32 bit) and last link for Windows(64 bit)
as highlighted below.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 14


Setting up the environment in Java

Step 2) After download, run the .exe file


and follow the instructions to install Java
on your machine. Once you installed Java
on your machine, you have to setup
environment variable.

Step 3) Go to Control Panel -> System


and Security -> System.
Under Advanced System Setting option
click on Environment Variables as
highlighted below.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 15


Setting up the environment in Java

Step 4) Now, you have to alter the “Path” variable under System variables
so that it also contains the path to the Java environment. Select the “Path”
variable and click on Edit button as highlighted below.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 16


Setting up the environment in Java

Step 5) You will see list of different


paths, click on New button and then add
path where java is installed.
By default, java is installed in
“C:\Program Files\Java\jdk\bin” folder
OR “C:\Program Files(x86)\Java\jdk\bin”.
In case, you have installed java at any
other location, then add that path.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 17


Setting up the environment in Java

Step 6) Click on OK, Save the settings and you are done !! Now to check
whether installation is done correctly, open command prompt and type javac
-version. You will see that java is running on your machine.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 18


Java JVM, JRE and JDK

JVM (Java Virtual Machine) is an abstract machine that enables your


computer to run a Java program.
When you run the Java program, Java compiler first compiles your Java code
to bytecode. Then, the JVM translates bytecode into native machine code
(set of instructions that a computer's CPU executes directly).
Java is a platform-independent language. It's because when you write Java
code, it's ultimately written for JVM but not your physical machine
(computer). Since JVM ​executes the Java bytecode which is platform-
independent, Java is platform-independent.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 19


Java JVM, JRE and JDK

JRE (Java Runtime Environment) is a software package that provides Java


class libraries, Java Virtual Machine (JVM), and other components that are
required to run Java applications.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 20


Java JVM, JRE and JDK

JDK (Java Development Kit) is a software development kit required to


develop applications in Java. When you download JDK, JRE is also
downloaded with it.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 21


Java Data Types

Java Variables
A variable is a location in memory (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name
(identifier).
How to declare variables in Java?

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 22


Java Data Types

Rules for Naming Variables in Java


Java programming language has its own set of rules and conventions for naming
variables. Here's what you need to know:
 Variables in Java are case-sensitive.
 A variable's name is a sequence of Unicode letters and digits. It can begin with a letter, $ or _.
However, it's a convention to begin a variable name with a letter. Also, variable names cannot
use whitespace in Java.
 When creating variables, choose a name that makes sense. For
example: score, number, level makes more sense than variable names such as s, n, and l.
 If you choose one-word variable names, use all lowercase letters. For example, it's better to
use year rather than YEAR, or yEAR.
 If you choose variable names having more than one word, use all lowercase letters for the first
word and capitalize the first letter of each subsequent word. For example, nextYear.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 23


Java Data Types

There are 4 types of variables in Java programming language:

Instance Variables
(Non-Static Fields)

Class Variables
Parameters
(Static Fields)

Local Variables

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 24


Java Data Types

Boolean

Char Byte

Java
Float
Primitive Short
Data
Types
Double Int

Long

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 25


Java Data Types

boolean
 The boolean data type has two possible values, either true or false.
 Default value: false.
 They are usually used for true/false conditions.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 26


Java Data Types

byte
 The byte data type can have values from -128 to 127 (8-bit signed two's
complement integer).
 It's used instead of int or other integer data types to save memory if it's
certain that the value of a variable will be within [-128, 127].
 Default value: 0

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 27


Java Data Types

short
 The short data type can have values from -32768 to 32767 (16-bit signed
two's complement integer).
 It's used instead of other integer data types to save memory if it's certain
that the value of the variable will be within [-32768, 32767].
 Default value: 0

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 28


Java Data Types

int
 The int data type can have values from -231 to 231-1 (32-bit signed two's
complement integer).
 If you are using Java 8 or later, you can use unsigned 32-bit integer with
a minimum value of 0 and a maximum value of 232-1.
 Default value: 0

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 29


Java Data Types

long
 The long data type can have values from -263 to 263-1 (64-bit signed
two's complement integer).
 If you are using Java 8 or later, you can use unsigned 64-bit integer with
a minimum value of 0 and a maximum value of 264-1.
 Default value: 0

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 30


Java Data Types

double
 The double data type is a double-precision 64-bit floating-point.
 It should never be used for precise values such as currency.
 Default value: 0.0 (0.0d)

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 31


Java Data Types

float
 The float data type is a single-precision 32-bit floating-point.
 It should never be used for precise values such as currency.
 Default value: 0.0 (0.0f)

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 32


Java Data Types

char
 It's a 16-bit Unicode character.
 The minimum value of the char data type is '\u0000' (0). The maximum
value of the char data type is '\uffff'.
 Default value: '\u0000'

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 33


Java Operators

Operators are special symbols (characters) that carry out operations on


operands (variables and values).

For example, + is an operator that performs addition.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 34


Java Operators

Assignment Operator
Assignment operators are used in Java to assign values to variables. For
example,

The assignment operator assigns the value on its right to the variable on its
left. Here, 5 is assigned to the variable age using = operator.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 35


Java Operators

Assignment Operator

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 36


Java Operators

Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like
addition, subtraction, multiplication, etc.

Operator Meaning
+ Addition (also used for string concatenation)
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Remainder Operator

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 37


Java Operators

Arithmetic Operators

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 38


Java Operators

Arithmetic Operators

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 39


Java Operators

Unary Operators
The unary operator performs operations on only one operand.

Operator Meaning
+ Unary plus (not necessary to use since numbers are
positive without using it)
- Unary minus: inverts the sign of an expression
++ Increment operator: increments value by 1
-- decrement operator: decrements value by 1
! Logical complement operator: inverts the value of a
boolean
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 40
Java Operators

Unary Operators

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 41


Java Operators

Unary Operators
You can also use ++ and -- operator as both prefix and postfix in Java.
The ++ operator increases value by 1 and -- operator decreases the value
by 1.

int myInt = 5;
++myInt // myInt becomes 6
myInt++ // myInt becomes 7
--myInt // myInt becomes 6
myInt-- // myInt becomes 5

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 42


Java Operators

Equality and Relational Operators


The equality and relational operators determine the relationship between the two
operands. It checks if an operand is greater than, less than, equal to, not equal to
and so on. Depending on the relationship, it is evaluated to either true or false.
Operator Description Example
== equal to 5 == 3 is evaluated to false
!= not equal to 5 != 3 is evaluated to true
> greater than 5 > 3 is evaluated to true
< less than 5 < 3 is evaluated to false
>= greater than or equal to 5 >= 5 is evaluated to true
<= less than or equal to 5 <= 5 is evaluated to true

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 43


Java Operators

Equality and Relational Operators

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 44


Java Operators

instanceof Operator
In addition to relational operators, there is also a type comparison operator
instanceof which compares an object to a specified type.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 45


Java Operators

Logical Operators
The logical operators || (conditional-OR) and && (conditional-AND) operate
on boolean expressions.

Operator Description Example


|| conditional-OR: true if either false || true is evaluated to
of the boolean expression is true
true
&& conditional-AND: true if all false && true is evaluated to
boolean expressions are true false

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 46


Java Operators

Logical Operators

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 47


Java Operators

Ternary Operator
The conditional operator or ternary operator ?: is shorthand for the if-then-else
statement. The syntax of the conditional operator is:

variable = Expression ? expression1 : expression2

Here's how it works.

 If the Expression is true, expression1 is assigned to the variable.


 If the Expression is false, expression2 is assigned to the variable.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 48


Java Operators

Ternary Operator

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 49


Java Input and Output

Java Output
System.out.println(); or

System.out.print(); or

System.out.printf();

to send output to standard output (screen).

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 50


Java Input and Output

print() - It prints string


println() inside the quotes.

println() - It prints string


print() printf() inside the quotes similar
like print() method. Then
the cursor moves to the
beginning of the next line.
Java
Output printf() - Tt provides string
formatting

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 51


Java Input and Output
Java Output

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 52


Java Input and Output

Java Input

// create an object of Scanner


Scanner input = new Scanner(System.in);

// take input from the user


int number = input.nextInt();

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 53


Java Input and Output

Java Input

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 54


Java Input and Output

Java Input

Similarly, we can use nextLong(),


nextFloat(), nextDouble(), and next()
methods to get long, float, double, and
string input respectively from the user.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 55


Java Expressions & Blocks

Java Expressions
A Java expression consists of variables, operators, literals, and method
calls.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 56


Java Expressions & Blocks

Java Statements
In Java, each statement is a complete unit of execution.

Here, we have a statement. The complete execution of this statement


involves multiplying integers 9 and 5 and then assigning the result to the
variable score.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 57


Java Expressions & Blocks

Java Statements
In Java, each statement is a complete unit of execution.

Here, we have a statement. The complete execution of this statement


involves multiplying integers 9 and 5 and then assigning the result to the
variable score.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 58


Java Expressions & Blocks

Expression statements
We can convert an expression into a statement by terminating the
expression with a ;
// expression
number = 10
// statement
number = 10;

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 59


Java Expressions & Blocks

Java Blocks
A block is a group of statements (zero or more) that is enclosed in curly
braces { }

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 60


Java Expressions & Blocks

Java Blocks
However, a block may not have any statements.

class Main {
public static void main(String[] args) {

if (10 > 5) { // start of block

} // end of block
}
}

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 61


Java Comment

In computer programming, comments are a portion of the program that are


completely ignored by Java compilers. They are mainly used to help
programmers to understand the code.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 62


Java Comment

single-
multi-line
line

A single-line comment starts and ends in the same line. To write a single-
line comment, we can use the // symbol.

When we want to write comments in multiple lines, we can use the multi-
line comment. To write multi-line comments, we can use the /*....*/ symbol.
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 63
Java Comment

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 64


Thank you

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 65

You might also like