Introduction to Java
Prof. Salunkhe S.S.
Dept. of Computer Science
Greenfingers College of Computer and Technology,
Akluj
What is Java??
•Java is a popular programming language, created in 1995.
•Java provide platform.
•Java is a high level, robust, object-oriented and secure programming
language.
•It is used for:
Mobile applications (specially Android apps)
Desktop applications
Web applications
Web servers and application servers
Games
Database connection
And much, much more!
History of Java
Java was developed by Sun Microsystems
(which is now the takeover by Oracle) in the year 1995.
James Gosling is known as the father of Java.
Java team members (also known as Green Team)
They initiated this project to develop a language for digital
devices such as set-top boxes, televisions, etc.
But it was too advanced technology for the digital cable
television industry at the time.
However, it was suited for internet programming.
Later, Java technology was incorporated by Netscape.
Before Java, its name was Oak.
Why had they chosen java name for
Java language?
The team gathered to choose a new name.
The suggested words were "dynamic", "revolutionary", "Silk", "jolt", "DNA", etc.
They wanted something that reflected the essence of the technology: unique,
and easy to spell and fun to say.
According to James Gosling, "Java was one of the top choices along with Silk".
Since Java was so unique, most of the team members preferred Java than other
names.
Java is an island of Indonesia where the first coffee was produced (called java
coffee)
Java name was chosen by James Gosling while having coffee near his office.
Notice that Java is just a name, not an acronym.
Java Version History
Many java versions have been released till now.
The current stable release of Java is Java SE 10.
• JDK Alpha and Beta (1995)
• JDK 1.0 (23rd Jan 1996)
• JDK 1.1 (19th Feb 1997)
• J2SE 1.2 (8th Dec 1998)
• J2SE 1.3 (8th May 2000)
• J2SE 1.4 (6th Feb 2002)
• J2SE 5.0 (30th Sep 2004)
• Java SE 6 (11th Dec 2006)
• Java SE 7 (28th July 2011)
• Java SE 8 (18th Mar 2014)
• Java SE 9 (21st Sep 2017)
• Java SE 10 (20th Mar 2018)
Feature of Java
• The features of Java are also known as
java buzzwords.
• A list of most important features of Java language is
given below.
1. Object-Oriented
2. Simple
3. Secured
4. Platform independent
5. Robust
6. Portable
7. Architecture neutral
8. Dynamic
9. Interpreted
[Link] Performance
[Link]
[Link]
Object-oriented
Java is an object-oriented programming language.
Everything in Java is an object.
Object-oriented means we organize our software as a
combination of different types of objects that incorporates both
data and behavior.
Object-oriented programming (OOPs) is a methodology that
simplifies software development and maintenance by providing
some rules.
Basic concepts of OOPs are:
Object
Class
Inheritance
Polymorphism
Abstraction
Simple
Java syntax is based on C++ (so easier for programmers to learn
it after C++).
Java has removed many complicated and rarely-used features,
for example, explicit pointers, operator overloading, etc.
There is no need to remove unreferenced objects because there
is an Automatic Garbage Collection in Java
Secured
Java is best known for its security.
With Java, we can develop virus-free systems.
Java is secured because:
No explicit pointer
Java Programs run inside a virtual machine sandbox
Classloader: Classloader in Java is a part of the Java Runtime
Environment(JRE) which is used to load Java classes into the Java Virtual
Machine dynamically. It adds security by separating the package for the
classes of the local file system from those that are imported from network
sources.
Bytecode Verifier: It checks the code fragments for illegal code that can
violate access right to objects.
Security Manager: It determines what resources a class can access such
as reading and writing to the local disk.
Java language provides these securities by default. Some security can also
be provided by an application developer explicitly through SSL, JAAS,
Cryptography, etc.
Platform Independent
Java is platform independent because it is different from other
languages like C, C++, etc. which are compiled into platform
specific machines while Java is a write once, run anywhere
language.
A platform is the hardware or software environment in which a
program runs.
There are two types of platforms software-based and hardware-
based.
Java provides a software-based platform.
Java code can be run on multiple platforms, for example,
Windows, Linux, Sun Solaris, Mac/OS, etc. Java code is compiled
by the compiler and converted into bytecode. This bytecode is a
platform-independent code because it can be run on multiple
platforms, i.e., Write Once and Run Anywhere(WORA).
Robust
Robust simply means strong.
Java is robust because:
Strong memory management.
A lack of pointers that avoids security problems.
Automatic garbage collection in java.
Exception handling and the type checking mechanism in Java.
All these points make Java robust.
Portable
Java is portable because it facilitates you to carry the Java bytecode
to any platform.
It doesn't require any implementation.
Architecture Neutral
Java is architecture neutral because there are no implementation
dependent features, for example, the size of primitive types is
fixed.
In C programming, int data type occupies 2 bytes of memory for
32-bit architecture and 4 bytes of memory for 64-bit architecture.
However, it occupies 4 bytes of memory for both 32 and 64-bit
architectures in Java.
Dynamic
Java is a dynamic language.
Supports dynamic loading of classes.
Classes are loaded on demand.
Supports functions from its native languages, i.e., C and
C++.
Supports dynamic compilation and automatic memory
management (garbage collection).
High Performance
Java is faster than other traditional interpreted
programming languages because Java bytecode is
"close" to native code.
Java is an interpreted language.
Multi-threaded
A thread is like a separate program, executing
concurrently.
Can write Java programs that deal with many tasks at
once by defining multiple threads.
The main advantage of multi-threading is that it doesn't
occupy memory for each thread.
It shares a common memory area.
Threads are important for multi-media, Web
applications, etc.
Distributed
Java is distributed because it facilitates users to create
distributed applications in Java.
RMI and EJB are used for creating distributed applications.
This feature of Java makes us able to access files by calling the
methods from any machine on the internet.
Summary
Java works on different platforms (Windows, Mac, Linux,
Raspberry Pi, etc.)
It is one of the most popular programming language in the world
It is easy to learn and simple to use
It is open-source and free
It is secure, fast and powerful
It has a huge community support (tens of millions of developers)
Java is an object oriented language which gives a clear structure
to programs and allows code to be reused, lowering development
costs
As Java is close to C++ and C#, it makes it easy for
programmers to switch to Java or vice versa
What is Java Virtual Machine(JVM)??
A Java virtual machine is a virtual (an abstract) an abstract
machine.
JVMs are available for many hardware and software platforms
(i.e. JVM is platform dependent).
This means a programmer can develop Java code on one system
and can expect it to run on any other Java enabled system
without any adjustment.
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.
Variables in Java
Variable is name of reserved area allocated in memory. [A
variable is a container which holds the value while the
Java program is executed.]
In other words, it is a name of memory location.
It is a combination of "vary + able" that means its value can be
changed.
There are three types of variables in Java:
local variable
instance variable
static variable
A variable is assigned with a data type.
Types of variables
1. Local Variable-
• A variable declared inside the body of the method is called local variable.
• Use this variable only within that method and the other methods in the class aren't even aware
that the variable exists.
• A local variable cannot be defined with "static" keyword.
2. Instance Variable-
• A variable declared inside the class but outside the body of the method, is called instance variable.
• It is not declared as static.
• It is called instance variable because its value is instance specific and is not shared among instances.
3. Static variable-
• A variable which is declared as static is called static variable.
• It cannot be local.
• Create a single copy of static variable and share among all the instances of the class.
• Memory allocation for static variable happens only once when the class is loaded in the memory.
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
double.
Non-primitive data types: The non-primitive data types include Classes
, Interfaces, and Arrays
Primitive Data Types
There are 8 types of primitive data
Datatypes:
Type Default Value Default size
boolean data type boolean false 1 bit
byte data type char '\u0000‘click 2 byte
char data type byte 0 1 byte
short data type short 0 2 byte
int data type int 0 4 byte
long data type long 0L 8 byte
float data type float 0.0f 4 byte
double data type double 0.0d 8 byte
Operators in Java
Operator in Java is a symbol which is used to perform operations.
For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
Unary Operator,
Arithmetic Operator,
Shift Operator,
Relational Operator,
Bitwise Operator,
Logical Operator,
Ternary Operator and
Assignment Operator.
Control Structure
There are three kinds of control structures:
Conditional Branches, which we use for choosing between two or more
paths.
o There are three types in Java:
1. if/else/else if
2. ternary operator
3. switch.
Loops that are used to iterate through multiple values/objects and
repeatedly run specific code blocks.
o The basic loop types in Java are for- while and do while.
Branching Statements, which are used to alter the flow of control in
loops.
o There are two types in Java: break and continue.
Simple Program-”Hello World”
class Simple
{
public static void main(String args[])
{
[Link]("Hello Java");
}
}
To compile: javac [Link]
To execute: java Simple
Simple Program-”Hello World”
Parameter Description
class keyword is used to declare a class in java
public keyword is an access modifier which represents visibility. It means it is
visible to all.
static is a keyword. If we declare any method as static, it is known as the
static method.
The core advantage of the static method is that there is no need to
create an object to invoke the static method.
The main method is executed by the JVM, so it doesn't require to create
an object to invoke the main method. So it saves memory.
void is the return type of the method. It means it doesn't return any value.
main represents the starting point of the program
String[] args is used for command line argument.
[Link] is used to print statement.
ntln()
Input/Output in Java
Java I/O (Input and Output) is used to process the input and produce the
output.
Java uses the concept of a stream to make I/O operation fast. The [Link]
package contains all the classes required for input and output operations.
We can perform file handling in Java by Java I/O API.
Stream
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.
1) [Link]: standard output stream
2) [Link]: standard input stream
3) [Link]: standard error stream
How to get input from user in Java
• Java Scanner class allows the user to take input from the
console.
• It belongs to [Link] package.
• It is used to read the input of primitive types like int, double,
long, short, float, and byte. It is the easiest way to read input in
Java program.
• Scanner sc=new Scanner([Link]);
Example of String Input from
user
import [Link].*;
class DemoUserInputString
{
public static void main(String[] args)
{
Scanner in= new Scanner([Link]); //
[Link] is a standard input stream
[Link]("Enter a message: ");
String msg= [Link](); //reads string
[Link]("You have entered: "+msg);
}
}
Example of integer input from
user
import [Link].*;
class DemoUserInputInt
{
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]); //
[Link] is a standard input stream
[Link]("Enter first number- ");
int a= [Link]();
[Link]("Enter second number- ");
int b= [Link]();
[Link]("Enter third number- ");
int c= [Link]();
int d=a+b+c;
[Link]("Total= " +d);
}
}
More Example
• Example 1: -
• Example 2: -
• Example 3: -
• Example 4: -
• Example 5:-
• Example 6:-
• Example 7:-
• Example 8:-
• Example 9:-
• Example 10:-
• Example 11:-
Array in Java
• Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value.
• An array is a collection of similar types of data
• To declare an array, define the variable type with square
brackets:
• For example, you can create an array that can hold 100 values
of int type.
Array in Java
• Types of Array in java
• There are two types of array.
1. Single Dimensional Array
2. Multidimensional Array
Single Dimensional Array in Java
dataType[] array_nam int[] rollno;
e; or
or int []rollno;
dataType []array_nam
or
e;
int rollno[];
or
dataType array_name[
int intArray[]; //declaring array
];
intArray = new int[20]; // allocating memory to array
Single Dimensional Array in Java
The simple example of java array, where we are going to declare, instantiate, initialize and
traverse an array.
class Testarray
{
public static void main(String args[])
{
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<[Link];i++)//length is the property of array
[Link](a[i]);
}
}
Arrays Example
• Click here
Example of Multidimensional Java
Array
class Testarray3
{
public static void main(String args[])
{
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
[Link](arr[i][j]+" ");
}
[Link]();
}
}
}
Arrays in Java
Advantages
Code Optimization: It makes the code optimized, we can retrieve or sort
the data efficiently.
Random access: We can get any data located at an index position.
Disadvantages
Size Limit: We can store only the fixed size of elements in the array. It
doesn't grow its size at runtime. To solve this problem, collection
framework is used in Java which grows automatically.
Questions??
Thank you!