01 Introduction To Java
01 Introduction To Java
Algorithms
1
Lecture On: Introduction
to Edit
Java Master text styles
23/05/19 2
Course Roadmap
● Introduction to Java
● Operators and Conditionals
● Loops and Methods
● Strings and Arrays
1 Introduction to Java
2 Java Development Tools
3 Advantages of Java
4 Main Method
5 Variables in Java
6 Printing values
Computers also work in a similar manner, and you need to provide them with certain
instructions in order to use them to solve some important tasks.
A computer understands everything in its own language, which is called the machine
language, in which all the data is stored in the form of binary digits, known as bits in
short.
Since machine language is difficult for humans to understand, there are high-level
programming languages that are understandable by humans and, later, converted to
machine language using the process of compilation.
5
Introduction to Java
a. Python
b. Visual Basic
c. Delphi, Perl
d. PHP, ECMAScript
e. Ruby
f. C#
g. Java
What is Java?
6
Click to add Title
Poll 1
The process of conversion of a high-level
programming language to a machine language is
called:
1. Transpiling
4. Conversion
23/05/19 7 7 7
Click to add Title
Poll 1(Answer)
The process of conversion of a high-level
programming language to a machine language is
called:
1. Transpiling
4. Conversion
23/05/19 8 8 8
Java Development Tools
To develop Java applications, we use the Java Development Kit (JDK). It provides the environment to
develop and execute Java programs. It consists of the following two components:
● Development Tools – Provide an environment to develop Java programs
● JRE – Execute your Java program
JRE: Java Runtime Environment is an installation package, which provides the environment to run
Java programs on your machine. You cannot develop a program in JRE. Hence, it is used only by
someone who wants to run a Java programs, i.e., the end users of your program or application. It
consists of:
● JVM
● JIT
● Additional classes and packages.
JIT: The Just-In-Time (JIT) compiler is a an essential part of the JRE, which is responsible for
performance optimisation of Java-based applications at run time. The compiler is one of the key
aspects in deciding the performance of an application for both parties, i.e., the end user and the
application developer.
9
JVM
● The Java Virtual Machine (JVM) is the virtual machine that runs the Java bytecodes. The
JVM does not understand the Java source code, and this is why you need to compile the
*.java files to obtain the *.class, which contains the bytecodes, which are understood by the
JVM.
● JVM interprets the byte code into machine code depending upon the underlying operating
system and hardware combination.
● It is responsible for all the operations, such as garbage collection, array bound checking.
● JVM is platform-dependent.
● It is also the component that allows Java to be a ‘portable language’ (write once, run
anywhere).
10
JDK vs JRE vs JVM
11
Click to add Title
Poll 2
Is JVM platform-independent?
1. Yes
2. No
23/05/19 12 12 12
Click to add Title
Poll 2(Answer)
Is JVM platform-independent?
1. Yes
2. No
23/05/19 13 13 13
Click to add Title
Poll 3
Which of the following components helps Java achieve high
performance?
a. JVM
b. JDK
c. JIT
d. JRE • Practice in teams of 4 students
• Industry expert mentoring to learn better
• Get personalised feedback for improvements
23/05/19 14 14 14
Click to add Title
Poll 3(Answer)
Which of the following components helps Java achieve high
performance?
a. JVM
b. JDK
c. JIT
d. JRE • Practice in teams of 4 students
• Industry expert mentoring to learn better
• Get personalised feedback for improvements
23/05/19 15 15 15
main() Method
Every Java program needs an entry point to execute. The ‘main’ method acts as an entry point
in an application and subsequently invokes all other methods present in the program. It is
similar to the main method in C/C++.
public: This is the access modifier of the method, which defines the permission to access the
method. The main method has to be public for the program to access it.
static: A method is static when it does not need an object to be called. When the Java program
starts, the first component that is encountered is the main method, which does not have any
object created. Hence, the method must be static to be called.
16
main() Method
void: A method is void when it does not return anything. Every Java method must have a return
type. A Java program ends when the main method is executed and, thus, the method does not
return anything.
main: It is the name of the method and should be kept as ‘main’ only'; otherwise, the program
will not run.
String[] args: The main method accepts a single argument: An array of elements of type String.
Each string in an array is called a command-line argument. You can also write this as
String...args.
Note: You will learn about methods, return type and command-line arguments later in the
course.
17
Click to add Title
Poll 4
Can there be a Java Program without a main method?
a. Yes
b. No
23/05/19 18 18 18
Click to add Title
Poll 4(Answer)
Can there be a Java Program without a main method?
a. Yes
b. No
23/05/19 19 19 19
Variables in Java
While developing web applications, you need to store and manipulate a lot of information. For example,
Facebook lets you create your profile and stores all the information. When you sign in the next time, it
remembers your profile data and fetches your profile for you.
The basic storage unit in a computer program is called a variable. Variables are storage units in your
program and they facilitate a major chunk of the computations that you perform.
In simpler terms, variables are containers in the computer memory to store the information that you want.
In Java, variables have a type, name and the value they store.
However, to be able to access specific information in the memory, there should be a method to identify
these containers uniquely. For this purpose, we use a type of identifier known as variable names.
20
Naming Convention for Variables
Note:
21
Variable Declaration
● The Java programming language is statically typed, which means all the variables must first be
declared before they can be used.
● A variable declaration needs the name of the variable and its data type.
The code above tells the Java program that a field named grade of type char exists. So, you need to tell
the following two information about a variable to the computer:
1. The kind of information that you’d like to store in the variable, so that the computer can allocate
memory volume accordingly.
2. The name of the variable, so you can tell the computer to store information in that container or
retrieve information from it.
22
Variable Initialisation
Once a variable is declared, you need to assign a value to it. The process of assigning a value to a
variable is known as Variable Initialisation.
char grade;
grade = 'A';
23
Click to add Title
Poll 5
Which of the following is a valid variable name?
a. 123abc
b. var name
c. var_name
d. float
• Practice in teams of 4 students
• Industry expert mentoring to learn better
• Get personalised feedback for improvements
23/05/19 24 24 24
Click to add Title
Poll 5 (Answer)
Which of the following is a valid variable name?
a. 123abc
b. var name
c. var_name
d. float
• Practice in teams of 4 students
• Industry expert mentoring to learn better
• Get personalised feedback for improvements
23/05/19 25 25 25
Click to add Title
Poll 6
‘String’ is an example of:
1. Identifier
2. Class
• Practice in teams of 4 students
3. Keyword • Industry expert mentoring to learn better
• Get personalised feedback for improvements
4. Value
23/05/19 26 26 26
Click to add Title
Poll 6 (Answer)
‘String’ is an example of:
1. Identifier
2. Class
23/05/19 27 27 27
Data Types in Java
28
Data Types in Java
There are eight primitive data types in Java. Let’s take a look at each of them.
1. boolean: The boolean data type is used to track true or false conditions. It has only two possible
values: true or false. It represents one bit of information and its size is not defined precisely.
Example:
public class Main {
public static void main(String[] args) {
boolean boolVar1 = true;
boolean boolVar2 = false;
}
}
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html 29
Data Types in Java
2. byte: The byte data type is an 8-bit signed two’s complement integer. It has a minimum value of –128
and a maximum value of 127 (inclusive). The byte data type can be used for saving memory in large
arrays.
Example:
public class Main {
public static void main(String[] args) {
byte varName= 20;
}
}
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
30
Data Types in Java
3. short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of
–32,768 and a maximum value of 32,767 (inclusive). Similar to byte, you can use a short to save memory
in large arrays.
Example:
public class Main {
public static void main(String[] args) {
short varName= 20;
}
}
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
31
Data Types in Java
4. int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of –231 and a
maximum value of 231–1.
Syntax:
int varName;
Example:
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
32
Data Types in Java
5. long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value
of –263 and a maximum value of 263–1.
Syntax:
long varName;
Example:
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
33
Comparison of Integer Data Types
34
Data Types in Java
6. float: The float data type is a single-precision, 32-bit, IEEE 754 floating point.
Syntax:
float varName;
Example:
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
35
Data Types in Java
7. double: The double data type is a double-precision, 64-bit, IEEE 754 floating point.
Syntax:
double varName;
Example:
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
36
double vs float
37
Data Types in Java
8. char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0)
and a maximum value of '\uffff' (or 65,535 inclusive).
Syntax:
char varName;
Example:
public class Main {
public static void main(String[] args) {
char varName= ’A’;
}
}
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
38
Click to add Title
Poll 7
What is the difference between the size of double and
long?
a. 32 bits
b. 64 bits
c. 8 bits
d. No difference • Practice in teams of 4 students
• Industry expert mentoring to learn better
• Get personalised feedback for improvements
23/05/19 39 39 39
Click to add Title
Poll 7 (Answer)
What is the difference between the size of double and
long?
a. 32 bits
b. 64 bits
c. 8 bits
d. No difference • Practice in teams of 4 students
• Industry expert mentoring to learn better
• Get personalised feedback for improvements
23/05/19 40 40 40
Hands-On Exercise – 1
41
Default Values of Data Types
● It is not always necessary to assign a value when a field is declared.
● Fields that are declared but not initialised will be set to a default value set by the compiler.
● Variables are assigned a default value according to their data type.
● The table given below depicts the default values of the various data types.
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
42
Reading and Printing Values
In the previous slides, you saw variable declaration and value
assignment. In web applications, we often need to capture input
from the user and assign it to a variable.
import java.util.Scanner;
In Java, we use the Scanner class to prompt the user to input a
value. public class Main {
public static void main(String[] args) {
The Scanner class is used to take input from the user. It breaks Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
the input into tokens using a delimiter pattern, which, by default,
}
matches whitespace. The resulting tokens may then be }
converted to values of different types using the various methods
that appear next.
To read a value from a user, you need to follow the steps given
below:
1. Import the java.util.Scanner package first.
2. Create an object of the Scanner class
3. Read the input data into an variable
43
Reading and Printing Values
To print an output in Java, we use the System class.
44
Click to add Title
Poll 8
Which of the following is the correct way to import the
Scanner class?
a. import java.util.Scanner;
b. import java.Scanner;
c. import util.Scanner;
d. import java;
• Practice in teams of 4 students
• Industry expert mentoring to learn better
• Get personalised feedback for improvements
23/05/19 45 45 45
Click to add Title
Poll 8(Answer)
Which of the following is the correct way to import the
Scanner class?
a. import java.util.Scanner;
b. import java.Scanner;
c. import util.Scanner;
d. import java;
• Practice in teams of 4 students
• Industry expert mentoring to learn better
• Get personalised feedback for improvements
23/05/19 46 46 46
Hands-On Exercise – 2
Get an order detail from a customer, which contains the name of the product, price and
quantity. Print the values entered by the user.
47
Casting
Suppose you have stored the value of pi in a double variable.
You want to find the integer value of pi that is stored in the
same variable. How would you do this? public class Main {
public static void main(String[] args) {
double pi = 3.141592653589793;
In programming, you will often come across situations wherein
System.out.println(pi);
you may have to change the data type of a variable temporarily float floatPi = (float) pi;
in order to perform some calculations or operations. System.out.println(floatPi);
int intPi = (int)pi;
The process of converting one data type to another is known as System.out.println(intPi);
}
casting. It is quite useful in Java programs and lets you convert }
one data type to another.
48
Click to add Title
Poll 9
What would be the output of the following code?
a. 20.14
b. 20.14f • Practice in teams of 4 students
c. 20 • Industry expert mentoring to learn better
d. 20.139999389648438 • Get personalised feedback for improvements
23/05/19 49 49 49
Click to add Title
Poll 9(Answer)
What would be the output of the following code?
a. 20.14
b. 20.14f • Practice in teams of 4 students
c. 20 • Industry expert mentoring to learn better
d. 20.139999389648438 • Get personalised feedback for improvements
23/05/19 50 50 50
Hands-On Exercise – 3
51
Important Questions
1 What is the one basic difference between Java and all the other programming
languages?
2 Can you compile a Java program in Notepad? Illustrate with an example.
3 Tell us something about Java that no one in the campus told you.
52
Today’s Homework
You are designing an asset-tracking system that allows the user to enter the following details of an
asset:
Write a program that takes the inputs above from the user and prints them to the console.
Input Format:
a. The first line contains the asset tracking number, which is an integer.
b. The second line contains the asset name, which is a string.
c. The third line contains the asset value, which is a float number.
Output Format:
a. The first line contains the asset tracking number input by the user.
b. The second line contains the asset name input by the user.
c. The third line contains the asset value input by the user.
53
Tasks to Complete After the Session
Homework Questions
MCQs
Coding Questions
55
#RahoAmbitious
Thank You!
56