Unit 1 Introduction to JAVA
Unit 1 Introduction to JAVA
Why Oak? Oak is a symbol of strength and chosen as a national tree of many countries like the
U.S.A., France, Germany, Romania, etc.
In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.
Java is an island in Indonesia where the first coffee was produced (called Java coffee). It is a kind of
espresso bean. Java name was chosen by James Gosling while having a cup of coffee nearby his
office.
Why they chooses 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: revolutionary, dynamic, lively, cool, 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.
Java is an island of Indonesia where first coffee was produced (called java coffee).
Notice that Java is just a name not an acronym.
Originally developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle
Corporation) and released in 1995.
In 1995, Time magazine called Java one of the Ten Best Products of 1995.
JDK 1.0 released in (January 23, 1996).
What is Java?
Java is a programming language and a platform.
Platform -Any hardware or software environment in which a program runs, known as a platform.
Since Java has its own Runtime Environment (JRE) and API, it is called platform.
Where it is used?
According to Sun, 3 billion devices run java. There are many devices where java is currently used.
Some of them are as follows:
1. Desktop Applications such as acrobat reader, media player, antivirus etc.
2. Web Applications such as irctc.co.in, javatpoint.com etc.
3. Enterprise Applications such as banking applications.
4. Mobile
5. Embedded System
6. Smart Card
7. Robotics
8. Games etc.
Types of Java Applications
There are mainly 4 type of applications that can be created using java:
1) Standalone Application
It is also known as desktop application or window-based application. An application that we need to
install on every machine such as media player, antivirus etc. AWT and Swing are used in java for
creating standalone applications.
2) Web Application
An application that runs on the server side and creates dynamic page, is called web application.
Currently, servlet, jsp, struts, jsf etc. technologies are used for creating web applications in java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications etc. It has the advantage of
high level security, load balancing and clustering. In java, EJB is used for creating enterprise
applications.
4) Mobile Application
An application that is created for mobile devices. Currently Android and Java ME are used for creating
mobile applications.
Features of Java
1. Features of Java
1. Simple
2. Object-Oriented
3. Platform Independent
4. secured
5. Robust
6. Architecture Neutral
7. Portable
8. High Performance
9. Distributed
10. Multi-threaded
Features of java:
1. Platform Independent:
Compiler converts source code to bytecode and then the JVM executes the bytecode
generated by the compiler. This bytecode can run on any platform be it Windows, Linux, or macOS
which means if we compile a program on Windows, then we can run it on Linux and vice versa.
Each operating system has a different JVM, but the output produced by all the OS is the same
after the execution of bytecode. That is why we call java a platform-independent language.
2. Object-oriented
Java is an object-oriented programming language. Everything in Java is an object. Java is
purely an object oriented language due to the absence of global scope, Everything in java is an object,
all the program codes and data resides within classes and objects. It comes with an extensive set of
classes, arranged in packages, object model in java in sample and easy to extend.
Basic concepts of OOPs are:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
3. Secured
Java is best known for its security. With Java, we can develop virus-free systems. Java is secured
because:
o No explicit pointer
o Java Programs run inside a virtual machine sandbox
o 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.
o Bytecode Verifier: It checks the code fragments for illegal code that can violate access rights to
objects.
o 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.
4. Robust: Java language is robust which means reliable. It is developed in such a way that it puts a
lot of effort into checking errors as early as possible that is why the java compiler is able to detect
even those errors that are not easy to detect by another programming language. The main features
of java that make it robust are garbage collection, Exception Handling, and memory allocation.
5. Portable
The WORA (Write Once Run Anywhere) concept and platform independent feature make Java
portable. Now using the Java programming language, developers can yield the same result on any
machine, by writing code only once. The reason behind this is JVM and bytecode.
Suppose you wrote any code in Java, then that code is first converted to equivalent bytecode
which is only readable by JVM. We have different versions of JVM for different platforms.
Windows machines have their own version of JVM, linux has its own and macOS has its own
version of JVM. So if you distribute your bytecode to any machine, the JVM of that machine would
translate the bytecode into respective machine code.
6. Distributed:
In Java, we can split a program into many parts and store these parts on different computers.
A Java programmer sitting on a machine can access another program running on the other machine.
This feature in Java gives the advantage of distributed programming, which is very helpful when
we develop large projects. Java helps us to achieve this by providing the concept of RMI (Remote
Method Invocation) and EJB (Enterprise JavaBeans).
Java comes with an extensive library of classes for interacting, using TCP/IP protocols such as
HTTP and FTP, which makes creating network connections much easier than in C/C++.
7. Multithreaded:
A thread is an independent path of execution within a program, executing concurrently. Multithreaded
means handling multiple tasks simultaneously or executing multiple portions (functions) of the same
program in parallel.
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.
8. Simple:
Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to Sun
Microsystem, Java language is a simple programming language because:
o Java syntax is based on C++ (so easier for programmers to learn it after C++).
o Java has removed many complicated and rarely-used features, for example, explicit pointers,
operator overloading, etc.
o There is no need to remove unreferenced objects because there is an Automatic Garbage
Collection in Java.
9. 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.
JVM
Actually, JVM is an interpreter for Byte code.
The details of the JVM will differ from platform to platform, but all interpret the same Java
Byte code according to machine/platform.
The Byte code which are generated by the compiler will be tested by the JVM on the
execution of the program or we can say every Java Program is under the control of the JVM
which checks the code on the runtime many times for viruses and any malicious.
The Byte code generated by the compiler are also supported on any machine which has the
JVM which makes Java a platform independent language.
JRE
JRE is an acronym for Java Runtime Environment. It is used to provide runtime environment.
It is the implementation of JVM. It physically exists. It contains set of libraries + other files that JVM
uses at runtime.
Implementation of JVMs are also actively released by other companies besides Sun Micro Systems.
JDK
JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.
Variable and Data type in Java
There are three types of variables: local, instance and static.
There are two types of data types in java, primitive and non-primitive.
Variable
Variables in Java are fundamental elements that act as containers to store data values. They serve as
named storage locations in your program, where each variable is associated with a specific data
type and a value. The variable's name is a unique identifier used to refer to and manipulate its stored
value within the code.
int data=50; //Here data is variable
Types of Variable
There are three types of variables in java
• Local variable
• Instance variable
• Static variable
Local Variable
A variable that is declared inside the method is called local variable.
Instance Variable
A variable that is declared inside the class but outside the method is called instance variable. It is not
declared as static.
Static variable
A variable that is declared as static is called static variable. It cannot be local.
Example to understand the types of variables
class A{
int data=50; //instance variable
static int m=100; //static variable
void method(){
int n=90; //local variable
}
}//end of class
Data Types in Java
In java, there are two types of data types
primitive data types
non-primitive data types
6. Conditional Operator ( ? : ):
Conditional operator is also known as the ternary operator. This operator consists of three operands
and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should
be assigned to the variable. The operator is written as:
Value of b is : 30
Value of b is : 20
7. Unary Operator
Increment operator is used to increment a value by 1. There are two varieties of increment
operator:
Post-Increment: Value is first used for computing the result and then incremented.
Pre-Increment: Value is incremented first and then the result is computed.
Decrement operator is used for decrementing the value by 1. There are two varieties of
decrement operators.
Post-decrement: Value is first used for computing the result and then decremented.
Pre-decrement: Value is decremented first and then the result is computed.
First, let’s look at a code snippet using the pre-increment unary operator:
int operand = 1;
++operand; // operand = 2
int number = ++operand; // operand = 3, number = 3
Next, let’s have a look at the code snippet using the pre-decrement one:
int operand = 2;
--operand; // operand = 1
int number = --operand; // operand = 0, number = 0
Let’s look at a sample code snippet using the post-increment operator:
int operand = 1;
operand++; // operand = 2
int number = operand++; // operand = 3, number = 2
Also, let’s have a look at the post-decrement one:
int operand = 2;
operand--; //operand = 1
int number = operand--; // operand = 0, number 1
Conditional Branches, which we use for choosing between two or more paths. There are three
types in Java: if/else/else if, ternary operator and switch.
Loops that are used to iterate through multiple values/objects and repeatedly run specific
code blocks. 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. There are two
types in Java: break and continue.
/* Comments */ The compiler ignores comment block. Comment can be used anywhere in
the program to add info about the program or code block, which will be
helpful for developers to understand the existing code in the future easily.
import java.io.* This means all the classes of io package can be imported. Java io package
provides a set of input and output streams for reading and writing data to
files or other input or output sources.
public class This creates a class called Hello.
Hello All class names must start with a capital letter.
The public word means that it is accessible from any other classes.
Braces Two curly brackets {...} are used to group all the commands, so it is known
that the commands belong to that class or method.
public static When the main method is declared public, it means that it can also
void main be used by code outside of its class, due to which the main method
is declared public.
The word static used when we want to access a method without
creating its object, as we call the main method, before creating any
class objects.
The word void indicates that a method does not return a
value. main() is declared as void because it does not return a value.
main is a method; this is a starting point of a Java program.
String[] args It is an array where each element of it is a string, which has been named as
"args". If your Java program is run through the console, you can pass the
input parameter, and main() method takes it as input.
System.out.prin This statement is used to print text on the screen as output, where the
tln(); system is a predefined class, and out is an object of the PrintWriter class
defined in the system. The method println prints the text on the screen
with a new line. You can also use print() method instead of println()
method. All Java statement ends with a semicolon.
Program 2:
Input:
Hello Java
12
3.4
Output:
Java Arrays
An array is a collection of similar types of data.
Java array is an object which contains elements of a similar data type. Additionally, The elements
of an array are stored in a contiguous memory location. It is a data structure where we store
similar elements. We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element
is stored on 1st index and so on.
Unlike C/C++, we can get the length of the array using the length member. In C/C++, we need
to use the sizeof operator.
In Java, array is an object of a dynamically generated class. Java array inherits the Object class,
and implements the Serializable as well as Cloneable interfaces. We can store primitive values
or objects in an array in Java. Like C/C++, we can also create single dimensional or
multidimensional arrays in Java.
Moreover, Java provides the feature of anonymous arrays which is not available in C/C++.
//Java Program to illustrate how to declare, instantiate, initialize and traverse the Java 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<a.length;i++)//length is the property of array
System.out.println(a[i]);
}
}
Output:
10
20
70
40
50
============================== END==========================