JAVA The Intro
JAVA The Intro
JAVA
Programs
2
Programming Languages
Ada Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada
language was developed for the Department of Defense and is used mainly in defense projects.
BASIC Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used easily
by beginners.
C Developed at Bell Laboratories. C combines the power of an assembly language with the ease of
use and portability of a high-level language.
C++ C++ is an object-oriented language, based on C.
C# Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft.
COBOL COmmon Business Oriented Language. Used for business applications.
FORTRAN FORmula TRANslation. Popular for scientific and mathematical applications.
Java Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform-
independent Internet applications.
Pascal Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a
simple, structured, general-purpose language primarily for teaching programming.
Python A simple general-purpose scripting language good for writing short programs.
Visual Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop
Basic graphical user interfaces.
4
Interpreting/Compiling Source Code
5
Garbage Collection
An interpreter reads one statement from the source code, translates it to the machine
code or virtual machine code, and
then executes it right away, as shown in the following figure. Note that a statement
from the source code may be translated into several machine instructions.
… Output
area = 5 * 5 * 3.1415; Interpreter
...
7
Compiling Source Code
A compiler translates the entire source code into a machine-code file, and the machine-code
file is then executed, as shown in the following figure.
… … Output
area = 5 * 5 * 3.1415; Compiler 0101100011011100 Executor
... 1111100011000100
…
...
8
Why Java?
9
Java, Web, and Beyond
10
History of Java
1) Open Source
Java since its birth, till today, is an open-source product and it has all the public access. programmers are free to post entire source
code for anyone to download, copy, redistribute them. High Performance Java is an Compiler & interpreted language, so it will
never be as fast as a compiled language like C or C++. But, Java enables high performance with the use of the just-in-time
compiler.
2) Multi-Threading
Java multithreading feature makes it possible to write a program that can do many tasks simultaneously. The benefit of
multithreading is that it utilizes the same memory and other resources to execute multiple threads at the same time, like While
typing, grammatical errors are checked along.
3) Secure
When it comes to security, Java is always the first choice. With java secure features it enables us to develop virus-free, temper free
system. Java program always runs in Java runtime environment with almost null interaction with system OS, hence it is more secure.
4) Platform independent Unlike other programming languages such as C, C++ etc which are compiled into platform-specific
machines. Java is guaranteed to be write-once, run-anywhere language. On compilation, Java program is compiled into bytecode.
This bytecode is platform-independent and can be run on any machine, plus this bytecode format also provides security. Any
machine with Java Runtime Environment can run Java Programs.
5) Portability
The cross-platform feature enables the Java code to be highly portable. Programmers can write code in windows and execute the
same code in Linux Operating System.
6) Object-Oriented
In java, everything is an object which has some data and behaviour. Java can be easily extended as it is based on Object Model.
7) Robust
Java makes an effort to eliminate error-prone codes by emphasizing mainly on compile-time error checking and runtime checking.
But the main areas which Java improved were Memory Management and mishandled Exceptions by introducing automatic Garbage
Collector and Exception Handling.
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
17
Characteristics of Java
Java Is Simple Java is partially modeled on C++, but greatly
Java Is Object-Oriented simplified and improved. Some people refer to
Java as "C++--" because it is like C++ but
Java Is Distributed with more functionality and fewer negative
Java Is Interpreted aspects.
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
18
Characteristics of Java
Java Is Simple Java is inherently object-oriented.
Java Is Object-Oriented Although many object-oriented languages
began strictly as procedural languages,
Java Is Distributed Java was designed from the start to be
Java Is Interpreted object-oriented. Object-oriented
programming (OOP) is a popular
Java Is Robust programming approach that is replacing
Java Is Secure traditional procedural programming
Java Is Architecture-Neutral techniques.
Java Is Portable One of the central issues in software
Java's Performance development is how to reuse code. Object-
oriented programming provides great
Java Is Multithreaded flexibility, modularity, clarity, and
Java Is Dynamic reusability through encapsulation,
inheritance, and polymorphism.
19
Characteristics of Java
Java Is Simple Distributed computing involves several
Java Is Object-Oriented computers working together on a network.
Java is designed to make distributed
Java Is Distributed computing easy. Since networking
Java Is Interpreted capability is inherently integrated into
Java, writing network programs is like
Java Is Robust sending and receiving data to and from a
Java Is Secure file.
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
20
Characteristics of Java
Java Is Simple You need an interpreter to run Java
Java Is Object-Oriented programs. The programs are compiled into
the Java Virtual Machine code called
Java Is Distributed bytecode. The bytecode is machine-
Java Is Interpreted independent and can run on any machine
that has a Java interpreter, which is part of
Java Is Robust the Java Virtual Machine (JVM).
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
21
Characteristics of Java
Java Is Simple Java compilers can detect many problems
Java Is Object-Oriented that would first show up at execution time
in other languages.
Java Is Distributed
Java Is Interpreted Java has eliminated certain types of error-
prone programming constructs found in
Java Is Robust other languages.
Java Is Secure
Java Is Architecture-Neutral Java has a runtime exception-handling
feature to provide programming support
Java Is Portable for robustness.
Java's Performance
Java Is Multithreaded
Java Is Dynamic
22
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust Java implements several security
mechanisms to protect your system against
Java Is Secure harm caused by stray programs.
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
23
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral Write once, run anywhere
Java Is Portable
With a Java Virtual Machine (JVM),
Java's Performance
you can write one program that will
Java Is Multithreaded run on any platform.
Java Is Dynamic
24
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Because Java is architecture neutral,
Java's Performance Java programs are portable. They can
Java Is Multithreaded be run on any platform without being
recompiled.
Java Is Dynamic
25
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java’s performance Because Java is
Java's Performance architecture neutral, Java programs
Java Is Multithreaded are portable. They can be run on any
platform without being recompiled.
Java Is Dynamic
26
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded Multithread programming is smoothly
integrated in Java, whereas in other
Java Is Dynamic languages you have to call procedures
specific to the operating system to enable
multithreading.
27
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance Java was designed to adapt to an evolving
Java Is Multithreaded environment. New code can be loaded on the
fly without recompilation. There is no need for
Java Is Dynamic developers to create, and for users to install,
major new software versions. New features can
be incorporated transparently as needed.
28
Versions of Java Languages
30
31
Compared with C/C++
NetBeans
Eclipse
Blue-J
Jcreater
FreeJava
32
Key Differences Between C++ Vs Java
C++ Java
C++ Java
C++ Java
C++ code is not portable. Java, however, translates the code into byte code.
It must be compiled for each platform. This byte code is portable and can be executed on any platform.
C++ Java
In C++ supports Constructor,methods and operators overloaded. This is static In Java, only method & Constructor overloading is allowed.
polymorphism. It does not allow operator overloading.
C++ Java
C++ is all about pointers. Java has limited support for pointers.
Initially, Java was completely without pointers but later versions started providing limited support
for pointers.
We cannot use pointers in Java.
C++ Java
C++ Java
C++ doesn’t have in-built thread support. Java supports threading concepts.
Application
According to Sun, 3 billion devices run Java. There are many devices
where Java is currently used. Some of them are as follows:----
1) Standalone Application
Standalone applications are also known as desktop applications or
window-based applications. Ex. Media player, antivirus
2) Web Application
An application that runs on the server side and creates a dynamic
page is called a web application.
3) Enterprise Application
An application that is distributed in nature, such as banking
applications, etc. is called enterprise application. It has advantages
of the high-level security, load balancing
4) Mobile Application
An application which is created for mobile devices is called a
mobile application.
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.
Access Modifier(Specifier)
Simple
Secure(Its Security is Very tight)
Pure Object Oriented Language
Supports Multithreading Concepts
Supports Error Handling Concepts
Supports Single & MultiLevel Inheritence
It’s a Compiler & Iterpreted Language
Advantages
JAVA
Java ByteCodes
Java Byte Codes are a instructions created by Java Compiler for the time of
Compilation and this Class file executed(run) by Interpreter.
Java Byte Codes are in the form of Class files.
Java Byte Codes are Platform Independent & Machine Independent.
Java Byte Codes are only run in different OS
Java Byte code is just like a Machine language(Binary language) equivalent.
44
Java JDK, JRE and JVM
What is JVM?(Interpreter)
JVM (Java Virtual Machine) is an abstract machine that enables your
computer to run a Java program.
When you run the Java program, Java compiler first compiles your Java
code to bytecode. Then, the JVM translates bytecode into native machine
code (set of instructions that a computer's CPU executes directly).
Java is a platform-independent language. It's because when you write Java
code, it's ultimately written for JVM but not your physical machine
(computer). Since, JVM executes the Java bytecode which is platform
independent, Java is platform-independent.
46
Java Virtual Machine(Interpreter)
J2SE
51
54
A Picture is Worth…
Write Once, Run Anywhere
57
Java Development Process
•void
This method does not return a value.
•main
This method is called main().
•(String [] args)
This is a parameter list that contains a 1D array of String objects. This is the command line list. We'll still have to explain
what a command line list is, but later on.
•Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println():----
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
public static void main(String... args)
static public void main(String[] args)
public static final void main(String[] args)
final public static void main(String[] args)
Class Name
Every Java program must have at least one class. Each class has a name. By convention,
class names start with an uppercase letter. In this example, the class name is Welcome.
74
Main Method
Line 2 defines the main method. In order to run a class, the class must contain a
method named main. The program is executed from the main method.
//
public class Welcome
{
public static void main(String[] args)
{
System.out.println("Welcome to Java!");
}
}
75
Statement
A statement represents an action or a sequence of actions. The statement
System.out.println("Welcome to Java!")
Program in Listing is a statement to display the greeting "Welcome to Java!“.
//
public class Welcome
{
public static void main(String[] args)
{
System.out.println("Welcome to Java!");
}
}
76
Statement Terminator
79
C style Format Printing in Java
public class format
{
public static void main(String[] args)
{
System.out.println("Integer Value: "+String.format("%d", 125));
System.out.println("String Value: "+String.format("%s", "Hello"));
System.out.println("Float Value: "+String.format("%.2f", 125.00));
System.out.println("Hexadecimal Value: "+String.format("%x", 125));
System.out.println("Char Value: "+String.format("%c", 'a'));
}
}
Static import
The normal import declaration imports classes from packages, so that they can be
used without package reference. Similarly the static import declaration imports
static members only from classes and allowing them to be used without class
reference.
A static import declaration also comes in two flavors: single-static import and
static-import-on-demand. A single-static import declaration imports one static
member from a type. A static-import-on-demand declaration imports all static
members of a type.
The general syntax of static import declaration is as follows:
//Single-static-import declaration:
import static <<package name>>.<<type name>>.<<static member name>>;
//Static-import-on-demand declaration:
import static <<package name>>.<<type name>>.*;
Static import example
class StaticImportExample
{
public static void main(String args[])
{
out.println("Hello"); //Now no need of System.out
out.println("Java");
}
}
Special Symbols
" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.
84
85
86
Java Keywords
volatile while
Software Requirements
Compilation
Javac
After Compilation
Class File
(Byte Code)
Application
Applet Programming
Programming
Application Programms
Required a Package
import java.awt.*;
import java.applet.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.util.*;