History of java:
In
1991, Sun Microsystems was attempting to develop a new technology for
programming next generation smart appliances.
The
original plan was for the Star7 operating system to be developed in C++ but
rejected the idea for several reasons. As a result we have new language that
was better for the purposes of the Green project than C++ developed by Gosling.
He called the language Oak in honor of a tree that could be seen from his
office window.
James
Gosling was part of Green, an isolated research project at Sun that was
studying how to put computers into everyday household items like thoughtful
toasters, sagacious Salad Shooters and lucid lamps. The group also wanted these
devices to communicate with each other.
In
January 1995, The meeting, arranged by Kim Polese (marketing person) where
about a dozen people got together to brainstorm with James Gosling, a vice
president and fellow of Sun, and the author of Oak, the final suggest names
were Silk, DNA, Ruby, WRL and Java by the team in meeting But the other
names could not trademark. So finally JAVA was the name chosen because it
sounded the coolest and decided to go ahead with it and name was first
suggested by Chris Warth.
Features of Java:
Object-oriented
Java
support the all the features of object oriented programming language such as
Abstraction, Encapsulation, Inheritance, Polymorphism and Dynamic binding etc. So
with the help of these features user can reduce the complexity of the program
develops in JAVA. Java gave a clean, usable, realistic approach to objects so
we can say that the object model in Java is simple and easy to extend.
Platform Independent
/ Portable
Java
makes it possible to have the assurance that any result on one computer with
Java can be replicated on another. So the code is run in the different platform
has a same result.
Simple and Powerful
Java
inherits the C/C++ syntax and many of the object-oriented features of C++.so we
can say that Java was designed to be easy to learn and use. java provides a
small number of clear ways to achieve a given task. Unlike other programming
systems that they provide dozens of complicated ways to perform a simple task.
Secure
Java
Compatible Browser, anyone can safely download Java applets without the fear of
viral infection or malicious intent because of its key design principle. So
anyone can download applets with confidence that no harm will be done and no
security will be violated. Java achieves this protection by confining a Java
program to the Java execution environment and by making it inaccessible to
other parts of the computer.
Robust
Most programs in use today fail for one of the two reasons:
(i) MEMORY MANAGEMENT MISTAKES
For example, in C/C++, the programmer must manually allocate
and free all dynamic memory. This sometimes leads to problems, because
programmers will either forget to free memory that has been previously
allocated or, sometimes try to free some memory that another part of their code
is still using. Java virtually eliminates these problems by managing memory
allocation (with the help of new operator) and deallocation. (deallocation is
completely automatic, because Java provides garbage collection for unused
objects.)
(ii) MISHANDLED EXCEPTIONAL CONDITIONS
With the help of Exception Handling (try……….catch block),
the programmer can easily handle an error or exception so user can prevent the
program by automatically stop the execution when an exception found.
Thus, the ability to create robust programs was given a high
priority in the design of Java.
Multithreaded
Java
supports programming, which allows the user to write programs that perform many
functions simultaneously. The two or more part of the program can run
concurrently then each part of such a program is called a Thread and this type
of programming is called multithreaded programming. Each thread defines a
separate path of execution. Thus, multithreading is a specialized form of
multitasking.
Architecture-neutral
The
Java designers worked hard in achieving their goal “write once; run anywhere,
anytime, forever” and as a result the Java Virtual Machine was developed. Java
is Architecture-neutral it generates bytecode that resembles machine code, and
are not specific to any processor.
Interpreted and High performance
The source code is first compile and
generates the code into an intermediate representation called Java bytecode
which is a highly optimized set of instruction code. This code can be
interpreted on any system that has a Java Virtual Machine and generates the
machine code. Java bytecode was carefully designed by using a just-in-time
compiler so that it can be easily translated into native machine code for very
high performance. Most of the earlier cross-platform solutions are run at the
expense of performance.
Distributed
Java
allows the object can access the information across the network with the help
of RMI (Remote Method Invocation) means this allowed objects on two different
computers to execute procedures remotely. So this feature supports the client/server
programming.
Dynamic
Java
programs carry with them substantial amounts of run-time type information that
is used to verify and resolve accesses to objects at run time. This makes it
possible to dynamically link code in a safe and perfect manner.
Token:
A
token is the smallest element of a program that is meaningful to the
compiler. (Actually, this definition is true for all compilers, not just the
Java compiler.) These tokens define the structure of the Java language. When
you submit a Java program to the Java compiler, the compiler parses the text
and extracts individual tokens.
Java tokens can be broken into five categories: identifiers, keywords,
literals, operators, and separators. The Java compiler also recognizes and
subsequently removes comments and whitespaces.
Identifiers :
Identifiers
are tokens that represent names. These names can be assigned to variables,
methods, and classes to uniquely identify them to the compiler. “Identifiers
means a sequence of uppercase(A,B,C,……,Y,Z) and lowercase(a,b,c,…..,y,z)
letters, numbers(0,1 ,2,……,9), or the underscore(_) and dollar-sign($)
characters and must not begin with a number.”
Valid and invalid Java
identifiers.
Valid
|
Invalid
|
HelloWorld
|
Hello World (uses a space)
|
Hi_JAVA
|
Hi JAVA! (uses a space and
punctuation mark)
|
value3
|
3value(begins with a number)
|
Tall
|
short (this is a Java keyword)
|
$age
|
#age (does not begin with any
other symbol except _ $ )
|
NOTE : For
Java identifiers, you should follow a few stylistic rules to make Java
programming easier and more consistent. It is standard Java practice to name
multiple-word identifiers in lowercase except for the beginning letter of words
in the middle of the name. For example, the variable firstValue is in correct Java style; the
variables firstvalue, FirstValue, and FIRSTVALUE are all in violation of this
style rule. Another more critical naming issue regards the use of underscore
and dollar-sign characters at the beginning of identifier names. Using either
of these characters at the beginning of identifier names is a little risky
because many C libraries use the same naming convention for libraries, which
can be imported into your Java code. A good use of the underscore character is
to use it to separate words where you normally would use a space (Hi_JAVA).
Keywords :
“It is a special type of
reserved word for a specific purpose which cannot be use as a identifier means
cannot be used as names for a variable, class, or method.”
There are 49 reserved keywords
currently defined in the Java language (see the following table).
abstract
|
double
|
int
|
switch
|
assert
|
else
|
interface
|
synchronized
|
boolean
|
extends
|
long
|
this
|
break
|
false
|
native
|
throw
|
byte
|
final
|
new
|
transient
|
case
|
finally
|
package
|
true
|
catch
|
float
|
private
|
try
|
char
|
for
|
protected
|
void
|
class
|
goto
|
public
|
volatile
|
const
|
if
|
return
|
while
|
continue
|
implements
|
short
|
|
default
|
import
|
static
|
|
do
|
instanceof
|
super
|
The keywords const and
goto are reserved but not used. In the early days of
Java,several other keywords were reserved for possible future use. In
addition to the keywords, Java reserves the following: true, false,
and null. These are values defined by Java. You may not use
these words for the names of variables, classes, and so on.
Separators :
Separators are used to
inform the Java compiler of how things are grouped in the code. For example,
items in a list are separated by commas much like lists of items in a sentence.
The most commonly used separator in Java is the semicolon. As you have seen, it
is used to terminate statements.
Symbol
|
Name
|
Purpose
|
;
|
Semicolon
|
Terminates statements.
|
,
|
Comma
|
Separates consecutive
identifiers in a variable
declaration.
Also used to chain statements
together inside a for statement.
|
{ }
|
Braces
|
Used to contain the values of
automatically initialized arrays.
Also used to define a block of
code, for classes, methods, and local scopes.
|
( )
|
Parentheses
|
Used to contain lists of
parameters in method definition and invocation.
Also used for defining
precedence in expressions, containing expressions in control statements.
Also used for surrounding cast
types.
|
[ ]
|
Brackets
|
Used to declare array types.
Also used when dereferencing
array values.
|
.
|
Period
|
Used to separate package names
from subpackages and classes Also used to separate a variable or method from
a reference variable.
|
Comments and Whitespaces :
The comments
and whitespaces are removed by the Java compiler during the tokenization of the
source code. White space consists of spaces, tabs, and linefeeds. All
occurrences of spaces, tabs, or linefeeds are removed by the Java compiler, as
are comments. Comments can be defined in three different ways, as shown in
Table.
Types of comments
supported by Java.
Type
|
Syntax
|
Usage
|
Example
|
Single-line
|
// comment
|
All characters after the // up
to the end of the line are ignored.
|
//This is a Single-line style
comment.
|
Multiline
|
/* comment */
|
All characters between /* and */
are ignored.
|
/* This is a Multiline style
comment.
|
Documentation
|
/** comment */
|
Same as /* */, except that the
comment can be used with the javadoc tool to create automatic documentation.
|
/** This is a javadoc style
comment. */
|
Literals :
A constant value in Java is created by using a literal
representation of it.
For example, Here are some literals :
integer literal value : 100
floating-point literal value : 98.6
character literal value : ‘X’
string literal value : “This is a test”
A literal can be used anywhere a value of its type is
allowed.
Integer Literals :
Integer
literals are the primary literals used in Java programming. They come in a
few different formats: decimal, hexadecimal, and octal. These formats
correspond to the base of the number system used by the literal. Decimal (base
10) literals appear as ordinary numbers with no special notation. Hexadecimal
numbers (base 16) appear with a leading 0x or 0X. Octal (base 8) numbers appear with
a leading 0 in front of the digits.
For example, an integer literal for the decimal number 12 is
represented in Java as 12 in decimal, 0xC in hexadecimal, and 014
in octal. Integer literals default to being stored in the int
type, which is a signed 32-bit value. If you are working with very large
numbers, you can force an integer literal to be stored in the long
type by appending an l or L to the end of the number, as in 79L.
The long type is a signed 64-bit value.
Floating-Point Literals :
Floating-point
literals represent decimal numbers with fractional parts, such as 3.1415.
They can be expressed in either standard or scientific notation, meaning that
the number 143.85 also can be expressed as 1.4385e2. Unlike
integer literals, floating-point literals default to the double
type, which is a 64-bit value. You have the option of using the smaller 32-bit float
type if you know the full 64 bits are not required. You do this by appending an
f or F to the end of the number, as in 5.6384e2f.
Boolean Literals :
Boolean
literals are certainly welcome if you are coming from the world of C/C++. In C,
there is no boolean type, and therefore no boolean literals. The boolean values
true and false are represented by the integer values 1 and 0. Java fixes
this problem by providing a boolean type with two possible states: true
and false. Not surprisingly, these states are represented in the Java
language by the keywords true and false. Boolean literals are used in Java programming about as
often as integer literals because they are present in almost every type of
control structure. Any time you need to represent a condition or state with two
possible values, a boolean is what you need. The two boolean literal values: true
and false.
Character Literals :
Character
literals represent a single Unicode
character and appear within a pair of single quotation marks. Special
characters (control characters and characters that cannot be printed) are
represented by a backslash (\) followed by the character code.
Example of a special character is \n,
which forces the output to a new line when printed. Table shows the special
characters supported by Java.
Description
|
Representation
|
Backslash
|
\\
|
Continuation
|
\
|
Backspace
|
\b
|
Carriage return
|
\r
|
Form feed
|
\f
|
Horizontal tab
|
\t
|
Newline
|
\n
|
Single quote
|
\'
|
Double quote
|
\"
|
Unicode character
|
\udddd
|
Octal character
|
\ddd
|
An example of a Unicode character literal is \u0048,
which is a hexadecimal representation of the character H. This same character
is represented in octal as \110.
String Literals :
String
literals represent multiple characters and
appear within a pair of double quotation marks. String literals are implemented
in Java by the String class. This arrangement is very different from the C/C++
representation of strings as an array of characters. When Java encounters a
string literal, it creates an instance of the String class and sets its state to the
characters appearing within the double quotes.
Operators:
Operators means
specify an evaluation to be performed on a data (Operands).
First Java program :
Demonstrating program
that displays the text,” Hello Java World! First JAVA program.....” on the
console.
public
class FirstProg
{ //This is a first java
program.
public static void main(String[]
args)
{
System.out.println("Hello
Java World! First JAVA program.....");
}
}
Explanation :
public :
A
keyword of the Java language that indicates that the element that follows
should be made available to other Java elements. Public keyword indicates that
the FirstProg class is a public class, which means other
classes can use it.
class :
Java
keyword that indicates that the element being defined here is a class. All
Java programs are made up of one or more classes.
A
class definition contains code that defines the behavior of the objects
created and used by the program.
FirstProg :
An
identifier that provides the name for the class being defined here. While
keywords, such as public and class, are words that are defined by the Java
programming language, identifiers are words that you create to provide
names for various elements you use in your program. In this program, the
identifier FirstProg provides a name for the public class being defined
here.
{ :
The opening
brace marks the beginning of the body of the class. The end of the body is
marked by the closing brace. Everything that appears within these braces
belongs to the class. As you work with Java, you’ll find that it uses
these braces a lot.
//This is a first java program. :
This
is a comment. Like most other programming languages, Java lets you enter
a remark into a program’s source file. The contents of a comment are ignored by
the compiler. Instead, a comment describes or explains the operation of the
program to anyone who is reading its source code. In this case, the comment
describes the program and reminds you that the source file should be called
FirstProg.java. Of course, in real applications, comments generally explain how
some part of the program works or what a specific feature does.
public :
The public
keyword is used again, this time to indicate that a method being declared
here should have public access. That means classes other than the
FirstProg class can use it. All Java programs must have at least one class
that declares a public method named main. The main method contains the
statements that are executed when you run the program.
static :
Execute any
elements (properties) without an object means before the object creating then
it must declare a static so compiler directly execute those static
elements.Here the main method is executed before the any object is creating so
it must declare a static. The keyword static allows main( ) to be
called without having to instantiate a particular instance of the class. This
is necessary since main( ) is called by the Java interpreter before any
objects are made. Java language requires that you specify static when you
declare the main method.
void :
In Java, a
method is a unit of code that can calculate and return a value.
main :
Identifier
that provides the name for this method. Java requires that this method be named
main because main( ) is the method called when a Java application
begins. Besides the main method, you can also create additional methods with
whatever names you want to use.
(String[] args) :
It’s
called a parameter list, and it’s used to pass data to a method. Java
requires that the main method must receive a single parameter that’s an array
of String objects. By convention, this parameter is named args. If you don’t
know what a parameter, a String, or an array is, don’t worry about it
You have to write (String[] args) on the declaration for the main methods
in all your programs. In this case, args receives any command-line
arguments present when the program is executed. This program does not make
use of this information.
System.out.println(“Hello Java World! First JAVA
program..........”); :
Statement
in the entire program. It calls a method named println that belongs to the
System.out object. System is a predefined class, it is automatically
included in your programs that provides access to the system and out is
the output stream that is connected to the console. The println method displays
a line of text on the console. The text to be displayed is passed to the
println method as a parameter in parentheses following the word println. In
this case, the text is the string literal Hello Java World! First JAVA program................
enclosed in a set of double quotation marks. As a result, this statement
displays the text on the console.
Note: In
Java, statements end with a semicolon. Because this is the only
statement in the program, this line is the only one that requires a semicolon. Java
is case-sensitive. Thus, Main is different from main.
javac FirstProg.java
This command creates a class file named FirstProg.class
that contains the Java bytecodes compiled for the FirstProg class. Now run
the program by entering this command:
java FirstProg
Output :
Hello Java World! First JAVA program................
Comments
Post a Comment