Java Unit - 1
Java Unit - 1
Introduction To Java
1) Java Evolution
2) Overview Of Java Language
3) Constants, Variables, and Data types
4) Operators and Expressions
5) Decision Making and Branching
6) Decision Making and Looping
1) Java Evolution
Java History
Java is purely Object Oriented Language.
10) There are no header files in Java. 10) C++ has header files
Java & Internet
Java is strongly associated with Internet.
Internet Users can use Java to create applet programs and run them locally
using Java-enabled browser such as HotJava.
Internet users can develop websites containing Java applets that could be
used by other remote users of Internet.
5. The user may have further interaction with the applet but with no further
downloading from the provider's Web server. This is because the bytecode
contains all the information necessary to interpret the applet.
Web Browsers
Web browsers are used to navigate through the
information found on the net.
When the Java language was first developed and ported to the
Internet, no browsers were available that could run Java applets.
Although we can view a Web page that includes Java applets with a
regular browser, we will not gain any of Java's benefits.
Itwas the first Web browser to provide support for the Java
language, thus making the Web more dynamic and interactive.
2. Netscape Navigator
Itis a general-purpose browser that can run Java applets.
With versions available for Windows 95, NT, Solaris
and Apple Macintosh etc
It
also supports JavaScript, a scripting language used in
HTML documents.
3. Internet Explorer
It
is another popular browser developed by
Microsoft for Windows 95, NT and XP
Workstations.
Development tools:
1) appletviewer for viewing Java applets
2) javac Java Compiler
3) java Java Interpreter
4) javap Java disassembler
5) javah for C header files
6) javadoc for creating HTML documents
7) jdb Java debugger
1) appletviewer Enables to run Java applets without java
compatible browser
4) Deployment technologies:
Java plug-in: Enables the execution of a Java applet on the
browser.
Java Web Start: Enables remote-deployment of an application.
With Web Start, users can launch an application directly from the
Web browser without going through the installation procedure.
2) Overview of Java Language
Java is a general-purpose, object-
oriented programming language.
java3.webp
Simple Java Program consists
class Sample
{
public static void main(String args[])
{
System.out.println(“Java is better than
C++”);
}
}
1)Class Declaration :
class is an object-oriented construct.
1) public
The keyword public is access specifier that declares the an
main method as unprotected and therefore making it accessible
to all other classes. This is similar to the C++ public modifier.
2) static
keyword static declares main method as one that belongs to
the entire class and not a part of any objects of the class.
The main must always be declared as static since the
interpreter uses this method before any objects are created.
3) void
void states that the main method does not return any value
(but simply prints some text to the screen.)
4) All parameters to a method are
declared inside a pair of parentheses.
String args[] declares a parameter
named args, which contains an array of
objects of the class type String.
4) The Output Line
System.out.println ("Java is better than C++.");
Since Java is a true object oriented language, every method must be part
of an object.
The println method is a member of the out object, which is a static data
member of System class.
The method println always appends a newline character to the end of the
string. This means that any subsequent output will start on a new line.
Comments
Java permits both the single-line comments and multi-line
comments .
The single-line comments begin with //
For longer comments, we can create long multi-line comments
by starting with a /* and ending with a */
Java Program Structure
2) When more than one words are used in a name, the second
and subsequent words are marked with a leading uppercase
letters.
Examples: dayTemperature firstDayOfMonth
totalMarks
3) All private and local variables use only
lowercase letters combined with
underscores.
Examples: length
batch_strength
Java Separators
Name What it is Used for
Parentheses ( ) Used to enclose parameters in method definition and invocation, also used
for defining precedence in expressions, containing expressions for flow
control, and surrounding cast types.
Braces { } Used to contain the values of automatically initialized arrays and to define
a block of code forclasses, methods and local scopes
Brackets [ ] Used to declare array types and for dereferencing array values
Semicolon ; Used to separate statements
Comma , Used to separate consecutive identifiers in a variable declaration, also used
to chain statements together inside a 'for
Period . Used to separate package names from sub-packages and classes; also used
to separate a variable method from a reference variable.
Java Statements
The statements in Java are like sentences in
natural languages.
2) Labelled Statement
Any Statement may begin with a label.
Such labels must not be keywords, already
declared local Labels variables or previously
used labels in this module.
In Java are used as the arguments of Jump
statements, which are described later in this
list.
3) Expression Statement
Most statements are expression statements.
Java has seven types of Expression statements:
• Assignment
• Pre-Increment
• Pre-Decrement
• Post-Increment
• Post-Decrement
• Method Call and
• Allocation Expression.
4) Control statements
Control statements are used to control the flow of execution
of a program based on certain conditions.
5) Selection Statement
These select one of several control flows. There
are 3 types of selection statements in Java:
• if
• if-else, and
• switch.
6) Iteration Statement
These specify how and when looping will take
place. There are three types of iteration statements
• while
• do
• for
7) Jump Statements
They pass control to the beginning or end of the current block,
or to a labeled statement.
Such labels must be in the same block, and continue labels must
be on an iteration statement.
The four types of Jump statement are
• break,
• continue,
• return and
• throw
8) Synchronization Statements
These are used for handling issues with multithreading.
9) Guarding Statements
Guarding statements are used for safe handling of code that may
cause exceptions (such as division by zero).
These statements use the keywords try, catch, and finally.
Implementing a java program
Implementation of a Java application program involves a following
steps.
1) Creating the program
2) Compiling the program
3) Running the program
The interpreter looks for the main method in the program and begins
execution from there.
Machine Neutral
The compiler converts the source code files into bytecode files.
These codes are machine-independent and therefore can be run on any
machine.
That is, a program compiled on an IBM machine will run on a Macintosh
machine.
Java interpreter reads the bytecode files and translates them into machine
code for the specific machine on which the Java program is running.
The interpreter is therefore specially written for each type of machine.
(refer fig in next slide)
Fig: Implementing Of java programs
Java Virtual Machine
How does Java achieve architecture neutrality?
• The Java compiler produces an intermedia code known as
bytecode for a machine that does not exist. This machine is
called the Java Virtual Machine and it exists only inside the
computer memory.
• It is a simulated computer within the computer and does all
major functions of a real computer.