0% found this document useful (0 votes)
167 views32 pages

Java PPT-1 by Adi

The document discusses an introduction to Java programming. It covers the history and development of Java, its key features like being platform independent and object-oriented, and basic Java concepts like classes, objects, inheritance and encapsulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views32 pages

Java PPT-1 by Adi

The document discusses an introduction to Java programming. It covers the history and development of Java, its key features like being platform independent and object-oriented, and basic Java concepts like classes, objects, inheritance and encapsulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Java Programming

Unit-1

Dr. K ADISESHA
Java Tutorial 2

Introduction

Java Platforms

Features of Java

Java program

Java Control Structure

Prof. K. Adisesha
3
Introduction
Introduction:
Java is a general-purpose programming language intended to let programmers write
once, run anywhere (WORA). This means that compiled Java code can run on all
platforms that support Java without the need to recompile.
➢ Java is popular high-level, class-based object oriented programming language
originally developed by Sun Microsystems and released in 1995.
❖ Java is Open Source which means its available free of cost.
❖ Java is simple and so easy to learn
❖ Java is much in demand and ensures high salary
❖ Java has a large vibrant community
❖ Java has powerful development tools
❖ Java is platform independent
Prof. K. Adisesha
4
Introduction
History of Java:
James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language
project in June 1991. The small team of sun engineers called Green Team.
➢ Initially it was designed for small, embedded systems in electronic appliances like set-top
boxes.
➢ Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
➢ After that, it was called Oak and was developed as a part of the Green project.
➢ In 1995, Oak was renamed as "Java" because it was already a trademark by Oak
Technologies.
➢ Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of
Oracle Corporation) and released in 1995.
Prof. K. Adisesha
5
Introduction
History of Java:
James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language
project in June 1991. The small team of sun engineers called Green Team.
➢ Many java versions have been released till now: JDK Alpha and Beta (1995), JDK 1.0( 1996) to
Java SE 18 (to be released by March 2022)
➢ Popular Java Editors :To write your Java programs, you will need a text editor. There
are even more sophisticated IDEs available in the market.
❖ Notepad − On Windows machine, you can use any simple text editor like Notepad
(Recommended for this tutorial).
❖ 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
Prof. K. Adisesha
downloaded from https://www.eclipse.org/.
6
Introduction
Application:
According to Sun, 3 billion devices run Java. There are many devices where Java is
currently used.
➢ Some of them are as follows:
❖ Desktop Applications such as acrobat reader, media player, antivirus, etc.
❖ Web Applications such as irctc.co.in, javatpoint.com, etc.
❖ Enterprise Applications such as banking applications.
❖ Mobile
❖ Embedded System
❖ Smart Card
❖ Robotics
❖ Games, etc.
Prof. K. Adisesha
7
Introduction

Java Platforms / Editions:


There are many types of applications that can be created using Java programming:
➢ There are 4 platforms or editions of Java:
➢ Java SE (Java Standard Edition):
➢ It is a Java programming platform. It includes Java programming APIs such as java.lang,
java.io, java.net, java.util, java.sql, java.math etc.
➢ Java EE (Java Enterprise Edition): It is an enterprise platform that is mainly used to develop
web and enterprise applications. It includes topics like Servlet, JSP, Web Services, EJB, JPA,
etc.
➢ Java ME (Java Micro Edition): It is a micro platform that is dedicated to mobile applications.
➢ JavaFX: It is used to develop rich internet applications. It uses a lightweight user interface
API.
Prof. K. Adisesha
8
Introduction

Features of Java:
The primary objective of Java programming language creation was to make it portable,
simple and secure programming language.
➢ Simple
➢ Object-Oriented
➢ Portable
➢ Platform independent
➢ Secured
➢ Robust
➢ Architecture neutral
➢ Interpreted
➢ Multithreaded
➢Prof.Distributed
K. Adisesha
9
Introduction
Features of Java:
The primary objective of Java programming language creation was to make it portable,
simple and secure programming language.
➢ Object Oriented − In Java, everything is an Object. Java can be easily extended since it is based on
the Object model.
➢ Platform Independent − When Java is compiled, it is not compiled into platform specific machine,
rather into platform independent byte code.
➢ 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.
Prof. K. Adisesha
10
Introduction

Features of Java:
The primary objective of Java programming language creation was to make it portable,
simple and secure programming language.
➢ Portable − Being architecture-neutral and having no implementation dependent aspects of the
specification makes Java portable.
➢ 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.
➢ High Performance − With the use of Just-In-Time compilers, Java enables high performance.
➢ Distributed − Java is designed for the distributed environment of the internet.
➢ Dynamic − Java programs can carry extensive amount of run-time information that can be used
to verify and resolve accesses to objects on run-time.
Prof. K. Adisesha
11
Introduction

OOPs Concepts in Java:


The basic concept of OOPs is to create objects, re-use them throughout the program, and
manipulate these objects to get results.
➢ Class − The class is one of the Basic concepts of OOPs which is a group of similar entities. It is
only a logical component and not the physical entity.
➢ Object - An object can be defined as an instance of a class, and there can be multiple instances of
a class in a program.
➢ Inheritance - Inheritance is one of the Basic Concepts of OOPs in which one object acquires the
properties and behaviors of the parent object.
➢ Polymorphism - Polymorphism refers to one of the OOPs concepts in Java which is the ability of
a variable, object or function to take on multiple forms.
➢ Abstraction - Abstraction is one of the OOP Concepts in Java which is an act of representing
essential features without including background details.
Prof. K. Adisesha
12
Introduction

OOPs Concepts in Java:


The basic concept of OOPs is to create objects, re-use them throughout the program, and
manipulate these objects to get results.
➢ Encapsulation − Encapsulation is one of the best Java OOPs concepts of wrapping the data and
code. In this OOPs concept, the variables of a class are always hidden from other classes.
➢ Association - Association is a relationship between two objects. It is one of the OOP Concepts in
Java which defines the diversity between objects. In this OOP concept, all objects have their
separate lifecycle, and there is no owner.
➢ Aggregation - In this technique, all objects have their separate lifecycle. However, there is
ownership such that child object can’t belong to another parent object.
➢ Composition - Composition is a specialized form of Aggregation. It is also called “death”
relationship. Child objects do not have their lifecycle so when parent object deletes all child
object will also delete automatically.
Prof. K. Adisesha
13
Introduction

C++ vs Java:
C++ Java
• C++ is platform-dependent. • Java is platform-independent.
• C++ is mainly used for system programming. • It is widely used in Windows-based, web-based,
enterprise, and mobile applications.
• C++ supports multiple inheritance. • It can be achieved by using interfaces in java.
• C++ supports goto statement, Pointers, operator • Java doesn't supports goto statement, Pointers,
overloading. operator overloading.
• C++ supports both call by value and call by • Java supports call by value only. There is no call
reference. by reference in java.

Dr. K ADISESHA
14
Introduction

Java Architecture:
Java Architecture combines the process of compilation and interpretation.
➢ In Java, there is a process of compilation and interpretation.
➢ The code written in Java, is converted into byte codes which is done by the Java Compiler.
➢ The byte codes, then are converted into machine code by the JVM.
➢ The Machine code is executed directly by the machine.

Prof. K. Adisesha
15
Java program

Java Program:
To create a simple Java program, you need to create a class that contains the main
method.
➢ For executing any Java program, the following software or application must be
properly installed.
➢ Install the JDK if you don't have installed it, download the JDK and install it.
➢ Set path of the jdk/bin directory.
class Simple{
➢ Create the Java program public static void main(String args[])
➢ Compile and run the Java program {
❖ To compile: javac Simple.java System.out.println("Hello Java");
}
❖ To execute: java Simple }
Prof. K. Adisesha
16
Java program
Basic Syntax:
Java program, can be defined as a collection of objects that communicate via invoking
each other's methods.
➢ 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
❖ 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.
Prof. K. Adisesha
17
Java program
Basic Syntax:
Java program, can be defined as a collection of objects that communicate via invoking
each other's methods.

Prof. K. Adisesha
18
Java program
Java I/O (Input and Output) :
Java uses the concept of a stream to make I/O operation fast. The java.io package
contains all the classes required for input and output operations.
➢ Java uses the concept of a stream to make I/O operation fast. The java.io package
contains all the classes required for input and output operations.
➢ A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a
stream because it is like a stream of water that continues to flow.
➢ In Java, 3 streams are created for us automatically. All these streams are attached
with the console.
❖ System.out: standard output stream
❖ System.in: standard input stream
❖ System.err: standard error stream
Prof. K. Adisesha
19
Java program

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.
Prof. K. Adisesha
20
Java program

Java Variables:
A variable is a container which holds the value while the Java program is executed. A
variable is assigned with a data type.
➢ Variable is a name of memory location. There are three types of variables in java:
public class A
❖ local variable : A variable declared inside the body of the { static int m=100; //static variable
method is called local variable. void method()
❖ instance variable: A variable declared inside the class but {
int n=90; //local variable
outside the body of the method, is called an instance variable. }
❖ static variable : A variable that is declared as static is called public static void main(String args[])
a static variable. It cannot be local. You can create a single {
copy of the static variable and share it among all the int data=50; //instance variable
instances of the class. }
Prof. K. Adisesha } //end of class
21
Java program

Data Types in Java:


Data types specify the different sizes and values that can be stored in the variable.
➢ There are two types of data types in Java:
➢ Primitive data types: The primitive data
types include boolean, char, byte, short, int,
long, float and double.
➢ Non-primitive data types: The non-primitive
data types include Classes, Interfaces, and
Arrays.

Prof. K. Adisesha
22
Java program

Primitive data types: :


Data Type Characteristics Range
byte 8 bit signed integer -128 to 127
short 16 bit signed integer -32768 to 32767
int 32 bit signed integer -2,147,483,648 to 2,147,483,647
long 64 bit signed integer -9,223,372,036,854,775,808 to- 9,223,372,036,854,775,807
float 32 bit floating point number + 1.4E-45 to
+ 3.4028235E+38
double 64 bit floating point number + 4.9E-324 to
+ 1.7976931348623157E+308
boolean true or false NA, note Java booleans cannot be converted to or from other types

char 16 bit, Unicode Unicode character, \u0000 to \uFFFF Can mix with integer types
Prof. K. Adisesha
23
Java program
Operators in Java:
Operator Type Category Precedence
Operator in Java is a symbol that isUnary
used to perform
postfix
operations. expr++ expr--
➢ Types of operators in Java: prefix ++expr --expr +expr -expr ~ !
❖ Unary Operator, Arithmetic multiplicative */%

❖ Arithmetic Operator, additive +-


Shift shift << >> >>>
❖ Shift Operator, Relational comparison < > <= >= instanceof
❖ Relational Operator, equality == !=
❖ Bitwise Operator, Bitwise bitwise AND &

❖ Logical Operator, bitwise exclusive OR


bitwise inclusive OR
^
|
❖ Ternary Operator and Logical logical AND &&
❖ Assignment Operator. logical OR ||
Ternary ternary ?:
Prof. K. Adisesha Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
24
Java program
Java Keywords:
Java keywords are also known as reserved words. These are predefined words by Java
so they cannot be used as a variable or object name or class name.
abstract assert boolean break byte case
catch char class const continue default
do double else enum extends final
finally float for goto if implements
import instanceof int interface long native
new package private protected public return
short static strictfp super switch synchronized
this throw throws transient try void
Prof. K. Adisesha volatile while
25
Java program

Java Control Statements:


Java provides three types of control flow statements:
➢ Decision Making statements if(condition) {
❖ if statements statement 1; //executes when condition is true
❖ switch statement }
else{
➢ Loop statements statement 2; //executes when condition is false
❖ do while loop }
❖ while loop
❖ for loop
❖ for-each loop for(initialization; condition; increment/decrement){
➢ Jump statements //statement or code to be executed
❖ break statement }
❖ continue statement
Prof. K. Adisesha
26
Java program

Decision Making statements:


Decision-making statements decide which statement to execute and when. Decision-
making statements evaluate the Boolean expression and control the program flow
depending upon the result of the condition provided:
➢ If Statement: public class Student
{
❖ Simple if statement
public static void main(String[] args)
❖ if-else statement {
❖ if-else-if ladder int x = 10;
int y = 12;
❖ Nested if-statement if(x+y < 10) { System.out.println("x + y is less than 10"); }
else { System.out.println("x + y is greater than 20"); }
}
}
Prof. K. Adisesha
27
Java program

Decision Making statements:


Decision-making statements decide which statement to execute and when. Decision-
making statements evaluate the Boolean expression and control the program flow
depending upon the result of the condition provided: switch(expression) {
case value :
➢ Switch Statement: In Java, Switch statements are // Statements
break; // optional
similar to if-else-if statements. The switch case value :
statement contains multiple blocks of code called // Statements
cases and a single case is executed based on the break; // optional
variable which is being switched // You can have any number of case statements.
default : // Optional
// Statements
Prof. K. Adisesha }
28
Java program
Loop Statements:
loop statements are used to execute the set of instructions in a repeated order. The
execution of the set of instructions depends upon a particular condition.
➢ for loop : In Java, for loop is similar to C and C++. It enables us to initialize the loop variable,
check the condition, and increment/decrement in a single line of code.
for(initialization, condition, increment/decrement)
{ //block of statements }
➢ while loop : It is also known as the entry-controlled loop since the condition is checked at the
start of the loop. If the condition is true, then the loop body will be executed.
➢ do-while loop : It is also known as the exit-controlled loop since the condition is not checked in
advance. The syntax of the do-while loop is given below. do {
while(condition){
//statements
//looping statements
Prof. K. Adisesha
} while (condition);
}
29
Java program

Java - Arrays:
Java provides a data structure, the array, which is used to store a collection of
sequential collection of elements of the same type.
➢ Declaring Array Variables: ➢ Example:
dataType[] arrayRefVar; // preferred way. double[] myList; // preferred way.
or or
dataType arrayRefVar[]; // works but not preferred way. double myList[]; // works but not preferred way.
➢ Creating Arrays ➢ Example:
arrayRefVar = new dataType[arraySize]; double[] myList = new double[10];
dataType[] arrayRefVar = new dataType[arraySize]; or
dataType[] arrayRefVar = {value0, value1, ..., valuek}; double[] myList = {1.9, 2.9, 3.4, 3.5};
dataType[][] arrayRefVar = new dataType[Size][Size]; or
Prof. K. Adisesha
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
30
Java program

Java Arrays Loop:


When processing array elements, we often use either for loop or foreach loop because
all of the elements in an array are of the same type and the size of the array is known.
➢ Passing Arrays to Methods: Just as you can pass primitive public class TestArray
{
type values to methods, you can also pass arrays to methods. public static void main(String[] args)
For example. { double[] myList = {1.9, 2.9, 3.4, 3.5};
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) { // Print all the array elements
for (double element: myList) {
System.out.print(array[i] + " ");
System.out.println(element);
} }
}. }
}
Prof. K. Adisesha
31
Java program

Program to Reverse a Number in Java:


public class ReverseNumber
{
public static void main(String[] args)
{
int number = 987654, reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
System.out.println("The reverse of the given number is: " + reverse);
}
Prof. K. Adisesha
}
32
Discussion

Queries ?
Prof. K. Adisesha
9449081542

Prof. K. Adisesha (Ph. D)

You might also like