Introduction to
Java Language
Saravana kumar
Java Language History
James Gosling and Sun Microsystems
Oak
Java, May 20, 1995, Sun World
HotJava
The first Java-enabled Web browser
JDK Evolutions
J2SE, J2ME, and J2EE (not mentioned in the book, but it
is good to discuss here).
Why Java?
The answer is that Java enables users to develop and
deploy applications on the Internet for servers, desktop
computers, and small hand-held devices. The future of
computing is being profoundly influenced by the
Internet, and Java promises to remain a big part of that
future. Java is the Internet programming language.
Java is a general purpose programming language.
Java is the Internet programming language.
JDK Versions
JDK Alpha and Beta (1995).
JDK 1.02 (January 23, 1996).
JDK 1.1 (February 19, 1997).
JDK 1.2, December 8, 1998).
JDK 1.3, May 8, 2000).
JDK 1.4(February 6, 2002).
JDK 1.5(September 30, 2004).
JDK 1.6(December 11, 2006).
JDK 1.7(July 28, 2011).
JDK 1.8(March 18 2014).
JDK Editions
Java Standard Edition (J2SE)
J2SE can be used to develop client-side standalone
applications or applets.
Java Enterprise Edition (J2EE)
J2EE can be used to develop server-side applications
such as Java servlets and JavaServerPages(JSP).
Java Micro Edition (J2ME).
J2ME can be used to develop applications for mobile
devices such as cell phones.
Java IDE Tools
Forte by Sun MicroSystems
Borland JBuilder
Microsoft Visual J++
IBM Visual Age for Java
Eclipse
Net beans
BlueJ
IntelliJ IDEA
Jcreator
Dr. Java …and many more
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
Characteristics of Java
Java Is Simple
Java Is Object-Oriented Java is partially modeled on C++,
Java Is Distributed but greatly simplified and
improved. Some people refer to
Java Is Interpreted
Java as "C++--" because it is like
Java Is Robust C++ but with more functionality
Java Is Secure and fewer negative aspects.
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple Java is inherently object-oriented.
Java Is Object-Oriented Although many object-oriented
languages began strictly as
Java Is Distributed procedural languages, Java was
Java Is Interpreted designed from the start to be
Java Is Robust object-oriented. Object-oriented
programming (OOP) is a popular
Java Is Secure programming approach that is
Java Is Architecture-Neutral replacing traditional procedural
Java Is Portable programming techniques.
Java's Performance One of the central issues in
software development is how to
Java Is Multithreaded
reuse code. Object-oriented
Java Is Dynamic programming provides great
flexibility, modularity, clarity, and
reusability through encapsulation,
inheritance, and polymorphism.
Characteristics of Java
Java Is Simple
Java Is Object-Oriented Distributed computing involves
Java Is Distributed several computers working
together on a network. Java is
Java Is Interpreted
designed to make distributed
Java Is Robust computing easy. Since networking
Java Is Secure capability is inherently integrated
into Java, writing network
Java Is Architecture-Neutral
programs is like sending and
Java Is Portable receiving data to and from a file.
Java's Performance
Java Is Multithreaded
Java Is Dynamic
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
Java Is Distributed Machine code called bytecode. The
Java Is Interpreted bytecode is machine-independent
Java Is Robust and can run on any machine that
has a Java interpreter, which is part
Java Is Secure of the Java Virtual Machine (JVM).
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented Java compilers can detect many
problems that would first show up at
Java Is Distributed
execution time in other languages.
Java Is Interpreted
Java has eliminated certain types of
Java Is Robust error-prone programming constructs
Java Is Secure found in other languages.
Java Is Architecture-Neutral
Java Is Portable Java has a runtime exception-handling
Java's Performance feature to provide programming
Java Is Multithreaded support for robustness.
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed Java implements several security
mechanisms to protect your system
Java Is Interpreted against harm caused by stray
Java Is Robust programs.
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Write once, run anywhere
Java Is Distributed
Java Is Interpreted With a Java Virtual Machine (JVM),
you can write one program that will run
Java Is Robust on any platform.
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Because Java is architecture neutral,
Java Is Interpreted Java programs are portable. They can
Java Is Robust be run on any platform without being
Java Is Secure recompiled.
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java’s performance Because Java is
Java Is Interpreted architecture neutral, Java programs are
Java Is Robust portable. They can be run on any
Java Is Secure platform without being recompiled.
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed Multithread programming is smoothly
Java Is Interpreted integrated in Java, whereas in other
languages you have to call procedures
Java Is Robust specific to the operating system to enable
Java Is Secure multithreading.
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed Java was designed to adapt to
Java Is Interpreted an evolving environment. New
code can be loaded on the fly
Java Is Robust without recompilation. There is
Java Is Secure no need for developers to
Java Is Architecture-Neutral create, and for users to install,
major new software versions.
Java Is Portable New features can be
Java's Performance incorporated transparently as
Java Is Multithreaded needed.
Java Is Dynamic
A Simple Application
public class Welcome
{
public static void main(String[] args)
{
System.out.println("Welcome to Java");
}
}
Creating and Compiling Programs
On command line
Create/Modify Source Code
javac file.java
Source Code
Compile Source Code
i.e. javac Welcome.java
If compilation errors
Bytecode
Run Byteode
i.e. java Welcome
Result
If runtime errors or incorrect result
Executing Applications
On command line
java classname
Bytecode
Java Java Java
Interpreter Interpreter ... Interpreter
on Windows on Linux on Sun Solaris
Output
javac Welcome.java
java Welcome
output:
Welcome to Java
22
Java Comments
In Java, comments are preceded by following
Single line Comments two slashes (//) in a line.
Multiline Comments /* and */
Java doc Comments /** documentation */
What is a Constant?
456—a literal numerical constant
System.out.println(456);
“A Literal String Constant”
System.out.println(“My First Java”);
What is a variable?
It is a named computer location in memory that
holds values that might vary
Must that location have an address?
YES
What has addresses? Bits, bytes, words, what?
Bytes
Can a variable be more than one byte long?
YES
Data type Declarations
Specify the type of data and the length of the data
item in bytes
int, short, long
float, double
boolean
char
Data Types – Integer, Floating
int –4 -byte integer
byte —1-byte integer
short —2-byte integer
long —8-byte integer
float 4-byte floating point number
double 8-byte floating point number
Primitives sizes and Ranges
PRIMITIVE SIZE IN BITS RANGE
int 32 -2 to the 31st to 2 to the 31st
int 4 bytes 2147483648
long 64 -- 8 bytes -2 to the 63rd to 2 to the 63rd
float 32 +- 1.5 x 10^45
double 64 +- 5.0 x 10^324
decimal (C# only) 128 28 significant figures
string 16 bits per Not applicable
char
char 16 One character
bool (boolean in Java) 8 True or false
Java Identifiers
An identifier is a sequence of one or more characters.
The first character must be a valid first character
(letter, $, _) in an identifier of the Java programming
language.
Each subsequent character in the sequence must be a
valid non first character (letter, digit, $, _) in a Java
identifier.
Java Keywords
In the Java programming language, a keyword is one
of 50 reserved words that have a predefined meaning
in the language; because of this, programmers cannot
use keywords as names or variables, methods, classes,
or as any other identifier.
The following are the keywords in java.
Java Keywords
abstract continue for new switch
assert*** default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum**** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while
Java Modifiers
Modifiers are keywords that you add to those
definitions to change their meanings. The Java
language has a wide variety of modifiers, including
the following:
Java Access Modifiers
Non Access Modifiers
Java Modifiers
Java Access Modifiers:
Java provides a number of access modifiers to set
access levels for classes, variables, methods and
constructors.
The four access levels are:
Visible to the package, the default. No modifiers are needed.
Visible to the class only (private).
Visible to the world (public).
Visible to the package and all subclasses (protected).
Java Modifiers
Java non Access Modifiers:
Java provides a number of non-access modifiers
to achieve many other functionality.
The following are the Java non Access modifiers
The static modifier for creating class methods and variables
The final modifier for finalizing the implementations of
classes, methods, and variables.
The abstract modifier for creating abstract classes and
methods.
The synchronized and volatile modifiers, which are used for
threads.
Console Input and Output
System.out.println for console output
System.out is an object that is part of the Java
language
println is a method invoked by the System.out object
that can be used for console output
The data to be output is given as an argument in
parentheses
A plus sign is used to connect more than one item
Every invocation of println ends a line of output
System.out.println("The answer is " + 42);
println Vs. print
Another method that can be invoked by the
System.out object is print
The print method is like println, except that it does
not end a line.
With println, the next output goes on a new line
With print, the next output goes on the same line
Formatting Output with printf
Starting with version 5.0, Java includes a method
named printf that can be used to produce output in a
specific format
The Java method printf is similar to the print method
Like print, printf does not advance the output to
the next line
System.out.printf can have any number of
arguments
The first argument is always a format string that
contains one or more format specifiers for the
remaining arguments.
All the arguments except the first are values to be
output to the screen
Importing Packages and Classes
Libraries in Java are called packages
A package is a collection of classes that is stored in
a manner that makes it easily accessible to any
program
In order to use a class that belongs to a package,
the class must be brought into a program using an
import statement
Classes found in the package java.lang are
imported automatically into every Java program
Importing Packages and Classes
import java.util.Scanner;
// import Scanner class only
import java.util.*;
//import all the classes in package java.util
Console Input Using the Scanner Class
Starting with version 5.0, Java includes a class for
doing simple keyboard input named the Scanner class
In order to use the Scanner class, a program must
include the following line near the start of the file:
import java.util.Scanner
This statement tells Java to
Make the Scanner class available to the program
Find the Scanner class in a library of classes
named java.util
Console Input Using the Scanner Class
The following line creates an object of the class
Scanner and names the object keyboard :
Scanner keyboard = new Scanner(System.in);
Although a name like keyboard is often used, a
Scanner object can be given any name
For example, in the following code the Scanner
object is named scannerObject
Scanner scannerObject = new Scanner(System.in);
Once a Scannerobject has been created, a program
can then use that object to perform keyboard input
using methods of the Scanner class.
Console Input Using the Scanner Class
The method nextInt() reads one int value typed in
at the keyboard and assigns it to a variable:
int numberOfPods = keyboard.nextInt();
The method nextDouble reads one double value
typed in at the keyboard and assigns it to a variable:
double d1 = keyboard.nextDouble();
The method next reads one String value typed in at
the keyboard and assigns it to a variable:
double d1 = keyboard.next();
Console Input Using the Scanner Class
Multiple inputs must be separated by whitespace and
read by multiple invocations of the appropriate
method
Whitespace is any string of characters, such as
blank spaces, tabs, and line breaks that print out
as white space
Console Input Using the Scanner Class
The method next reads one string of non-whitespace
characters delimited by whitespace characters such as
blanks or the beginning or end of a line
Given the code
String word1 = keyboard.next();
String word2 = keyboard.next();
and the input line
jelly beans
The value of word1 would be jelly, and the value of
word2 would be beans.
Console Input Using the Scanner Class
The method nextLine() reads an entire line of
keyboard input
The code,
String line = keyboard.nextLine();
Reads in an entire line and places the string that is
read into the variable line.
The end of an input line is indicated by the escape
sequence '\n'
This is the character input when the Enter key is
pressed
On the screen it is indicated by the ending of one
line and the beginning of the next line
Console Input Using the Scanner Class
When nextLine reads a line of text, it reads the '\n'
character, so the next reading of input begins on the
next line
However, the '\n' does not become part of the
string value returned .
e.g., the string named by the variable line above
does not end with the '\n' character.
Thank you