You are on page 1of 37

1

MS1104 – ADVANCED JAVA PROGRAMMING


- THEORY AND PRACTICAL

CHAPTER 1
INTRODUCTION TO JAVA PROGRAMMING

MASTER OF SCIENCE IN INFORMATION TECHNOLOGY


© ISBAT UNIVERSITY – 2019. 11/20/2021
Introduction

 Java programming language was originally developed by Sun Microsystems which


was initiated by James Gosling and released in 1995 as core component of Sun
Microsystems' Java platform (Java 1.0 [J2SE]).
 The latest release of the Java Standard Edition is Java SE 14. With the advancement
of Java and its widespread popularity, multiple configurations were built to suit
various types of platforms. For example: J2EE for Enterprise Applications, J2ME
for Mobile Applications.
 The new J2 versions were renamed as Java SE, Java EE, and Java ME respectively.
Java is guaranteed to be Write Once, Run Anywhere.

© ISBAT UNIVERSITY – 2019. 11/20/2021


 Java is −
 Object Oriented − In Java, everything is an Object. Java can be easily extended
since it is based on the Object model.
 Object Oriented programming is a programming style which is associated with the
concepts like class, object, Inheritance, Encapsulation, Abstraction, Polymorphism.
Most popular programming languages like Java, C++, C#, Ruby, etc. follow an
object oriented programming paradigm.
 Platform Independent − Unlike many other programming languages including C
and C++, when Java is compiled, it is not compiled into platform specific machine,
rather into platform independent byte code. This byte code is distributed over the
web and interpreted by the Virtual Machine (JVM) on whichever platform it is
being run on.
© ISBAT UNIVERSITY – 2019. 11/20/2021
History of Java
 James Gosling initiated Java language project in June 1991 for use in one of his

many set-top box projects. The language, initially called ‘Oak’ after an oak tree that
stood outside Gosling's office, also went by the name ‘Green’ and ended up later
being renamed as Java, from a list of random words.
 Sun released the first public implementation as Java 1.0 in 1995. It promised Write

Once, Run Anywhere (WORA), providing no-cost run-times on popular platforms.


 On 13 November, 2006, Sun released much of Java as free and open source

software under the terms of the GNU General Public License (GPL).
 On 8 May, 2007, Sun finished the process, making all of Java's core code free and
open-source, aside from a small portion of code to which Sun did not hold the
copyright.
© ISBAT UNIVERSITY – 2019. 11/20/2021
 Simple − Java is designed to be easy to learn. If you understand the basic concept
of OOP Java, it would be easy to master.
 Secure − With Java's secure feature it enables to develop virus-free, tamper-free
systems. Authentication techniques are based on public-key encryption.
 Architecture-neutral − Java compiler generates an architecture-neutral object file
format, which makes the compiled code executable on many processors, with the
presence of Java runtime system.
 Portable − Being architecture-neutral and having no implementation dependent
aspects of the specification makes Java portable. Compiler in Java is written in
ANSI C with a clean portability boundary, which is a POSIX subset.

© ISBAT UNIVERSITY – 2019. 11/20/2021


 Robust − Java makes an effort to eliminate error prone situations by emphasizing
mainly on compile time error checking and runtime checking.
 Multithreaded − With Java's multithreaded feature it is possible to write programs
that can perform many tasks simultaneously. This design feature allows the
developers to construct interactive applications that can run smoothly.
 Interpreted − Java byte code is translated on the fly to native machine instructions
and is not stored anywhere. The development process is more rapid and analytical
since the linking is an incremental and light-weight process.
 High Performance − With the use of Just-In-Time compilers, Java enables high
performance.

© ISBAT UNIVERSITY – 2019. 11/20/2021


 Distributed − Java is designed for the distributed environment of the internet.

 Dynamic − Java is considered to be more dynamic than C or C++ since it is


designed to adapt to an evolving environment. Java programs can carry extensive
amount of run-time information that can be used to verify and resolve accesses to
objects on run-time.

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java Virtual Machine
JVM is a engine that provides runtime environment to drive the Java Code or applications. It
converts Java bytecode into machines language. JVM is a part of JRE(Java Run Environment). It
stands for Java Virtual Machine.
When we compile a .java file, .class files(contains byte-code) with the same class names present
in .java file are generated by the Java compiler. This .class file goes into various steps when we
run it. These steps together describe the whole JVM.
The JVM performs following operation:
 Loads code
 Verifies code
 Executes code
 Provides runtime environment
© ISBAT UNIVERSITY – 2019. 11/20/2021
. JRE
 JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The
Java Runtime Environment is a set of software tools which are used for developing Java
applications. It is used to provide the runtime environment. It is the implementation of
JVM. It physically exists. It contains a set of libraries + other files that JVM uses at
runtime

© ISBAT UNIVERSITY – 2019. 11/20/2021


.
JDK(JAVA DEVELOPMENT KIT)
❑ The Java Development Kit is a software development environment
used for developing Java applications and applets.
❑ It includes the Java Runtime Environment (JRE), an
interpreter/loader (java), a compiler (javac), an archiver (jar), a
documentation generator (javadoc) and other tools needed in Java
development.

© ISBAT UNIVERSITY – 2019. 11/20/2021


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

© ISBAT UNIVERSITY – 2019. 11/20/2021


 Objects in Java

 An object in Java is any real-world things that have properties and actions. In other
words, an entity that has state and behavior is known as an object. Here, State
represents the properties and Behavior represents the actions or functionality.

© ISBAT UNIVERSITY – 2019. 11/20/2021


 Characteristics of objects in Java
 An object has three characteristics:

1. State: State represents the properties of an object. It is represented by the instance


variable/attribute of an object. The properties of an object are important because the outcome
of functions depend on the properties.

2. Behavior: Behavior represents functionality or actions. It is represented by methods in Java.

3. Identity: Identity represents the unique name of an object. It differentiates one object from
the other. The unique name of an object is used to identify the object

© ISBAT UNIVERSITY – 2019. 11/20/2021


 Realtime Examples of Objects in Java
 We are taking the example “person”. A person has three characteristics:
Identity (name), State (properties), and behavior (actions or
functionality). Consider the below figure.

© ISBAT UNIVERSITY – 2019. 11/20/2021


© ISBAT UNIVERSITY – 2019. 11/20/2021
 Let’s take one more interesting example ‘Phone Samsung Galaxy’. If I tell you to represent
this phone in java in the form of an object then How will you do like that?

 You can do that very easily. Ask two questions: What it has? (Properties) and What it does?
(Actions)

 Properties are width=6.2 or “6.2 inch”, height=13.6 or “13.6 cm”, color=”black”,


OS=”Android”, price=1000 or “1000$”, brand=”Samsung”, and weight=”130 gm”. All
these are nouns called attributes.

 Actions/Behavior are call(), sendSms(), runApp(), browserInternet(), sharing(). Thus, a phone


object is comprised of the properties that the phone has actions that phone does.

© ISBAT UNIVERSITY – 2019. 11/20/2021


 Classes in Java
 In Java programming, a class is basically user-defined data types which act as a
template for creating objects of the identical type. It represents the common
properties and actions (functions) of an object.

 In other words, a class can also be defined as “a class is a group of objects which
are common to all objects of one type”.
 A class is the type of an object.

© ISBAT UNIVERSITY – 2019. 11/20/2021


 Realtime Examples of Classes in Java
 Real-time example :
 Let us consider two objects Samsung Galaxy S4 and iPhone. Suppose Samsung Galaxy S4
have some properties like width=”6.98 cms”, height=”13.6 cm”, OS=”Android”,
brand=”Samsung”, price=”1000$” and actions are call(), sendMessage(), browser(), share().

 Now, suppose iPhone has some properties such as width=”5.86 cm”, height=”12.3 cms”,
OS=”iOS”, brand=”Apple”, price=”1200$” and actions are call(), sendMessage(), browse(),
share().
 Both objects have some different properties and actions but the type is the same “Phone”.
This is the class. i.e the name of the class is “Phone”.

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java Variables

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

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java Variables

Example:
int myNum = 15;
System.out.println(myNum);

String name = "John";


System.out.println(name);

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java Data Types
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99f; // Floating point number
char myLetter = 'D'; // Character
boolean myBool = true; // Boolean
String myText = "Hello"; // String

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java Operators
 Java divides the operators into the following groups:

 Arithmetic operators

 Assignment operators

 Comparison operators

 Logical operators

 Bitwise operators

© ISBAT UNIVERSITY – 2019. 11/20/2021


Arithmetic Operators
 Arithmetic operators are used to perform common mathematical operations.
Operator Name Description Example
+ Addition Adds together two values x+y

- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two values x*y

/ Division Divides one value from another x/y

% Modulus Returns the division remainder x%y

++ Increment Increases the value of a variable by 1 ++x

-- Decrement Decreases the value of a variable by 1 --x

© ISBAT UNIVERSITY – 2019. 11/20/2021


Assignment operators
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
© ISBAT UNIVERSITY – 2019. 11/20/2021
Java Comparison Operators

Operator Name Example


== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java Logical Operators

Operator Name Description Example

&& Logical and Returns true if both statements are x < 5 && x < 10
true

|| Logical or Returns true if one of the x < 5 || x < 4


statements is true

! Logical not Reverse the result, returns false if !(x < 5 && x <
the result is true 10)

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java Booleans
 Very often, in programming, you will need a data type that can only have one of

two values, like:


 YES / NO

 ON / OFF

 TRUE / FALSE

Example
int x = 10;
int y = 9;
System.out.println(x > y); // returns true, because 10 is higher than 9

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java If ... Else
 Java supports the usual logical conditions from mathematics:
 Less than: a < b
 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b
 Equal to a == b
 Not Equal to: a != b

if (20 > 18) {


System.out.println("20 is greater than 18");
}

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java While Loop
 Syntax

 while (condition) {
// code block to be executed
}
 Example

 int i = 0;

while (i < 5) {
System.out.println(i);
i++;
}

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java For Loop
 Syntax

 for (statement 1; statement 2; statement 3) {


// code block to be executed
}
 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.

Example
 for (int i = 0; i < 5; i++) {
System.out.println(i);
}
© ISBAT UNIVERSITY – 2019. 11/20/2021
Java Break
The break statement can also be used to jump out of a loop.

 Example
 for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java Switch Statements
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

© ISBAT UNIVERSITY – 2019. 11/20/2021


int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Sunday");
}
// Outputs "Thursday" (day 4)

© ISBAT UNIVERSITY – 2019. 11/20/2021


Java Arrays
Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
To declare an array, define the variable type with square brackets:
String[] cars;
We have now declared a variable that holds an array of strings. To insert
values to it, we can use an array literal - place the values in a comma-
separated list, inside curly braces:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

© ISBAT UNIVERSITY – 2019. 11/20/2021


Access the Elements of an Array
 You access an array element by referring to the index number.

 This statement accesses the value of the first element in cars:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

System.out.println(cars[0]);
// Outputs Volvo

© ISBAT UNIVERSITY – 2019. 11/20/2021


Thank You

© ISBAT UNIVERSITY – 2019. 11/20/2021

You might also like