Brief History of Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

BRIEF HISTORY OF JAVA

Java is a programming language similar to C and C++


Java was conceived by James Gosling, in the early 1990s, as a simple and small language to be used
in programming consumer electronic devices, such as cellular phones and TV controllers.

FEATURES OF JAVA PROGRAMMING LANGUAGE

a. Portability: Java is a highly portable programming language because it is not designed for any
specific hardware or software platform. Java programs once written are translated into an
intermediate form called bytecode. The bytecode is then translated by the Java Virtual Machine (JVM)
into the native object code of the processor that the program is been executed on. JVMs exist for
several computer platforms; hence the term Write Once Run Anywhere (WORA).
b. Memory Management: Java is very conservative with memory; once a resource is no longer
referenced the garbage collector is called to reclaim the resource. This is one of the elegant features
that distinguishes Java from C/C++ where the programmer has to “manually” reclaim memory.
c. Extensibility: The basic unit of Java programs is the class. Every program written in Java is a class
that specifies the attributes and behaviors of objects of that class. Java APIs (Application Programmers
Interface) contains a rich set reusable classes that is made available to the programmers. These
classes are grouped together as packages from which the programmer can build new enhanced
classes. One of the key terms of object oriented programming is reuse.
d. Secure: Java is a very secure programming language. Java codes (applets) may not access the
memory on the local computer that they are downloaded upon. Thus it provides a secure means of
developing internet applications.
e. Simple: Java’s feature makes it a concise programming language that is easy to learn and
understand. It is a serious programming language that easily depicts the skill of the programmer.
f. Robustness: Java is a strongly typed programming language and encourages the development of
error free applications.

TYPES OF JAVA PROGRAMS


Java programs may be developed in three ways. They will be mentioned briefly here:
a. Java Applications: These are stand-alone applications such word processors, inventory control
systems etc.
b. Java Applets: These programs that are executed within a browser. They are executed on the
client computer.
c. Java Serverlets: These are server side programs that are executed within a browser.
In this course we will limit ourselves to only the first two mentioned types of Java programs –
applications and applets.

THE FOLLOWING ARE THE BASIC PRINCIPLES OF OOP.


a. Encapsulation: Encapsulation is a methodology that binds together data and the codes that it
manipulates thus keeping it safe from external interference and misuse. An object oriented program
contains codes that may have private members that are directly accessible to only the members of
that program. Also it may have program codes (methods) that will enable other programs to access
these data is a uniform and controlled fashion.
b. Polymorphism: Polymorphism is a concept whereby a particular “thing” may be employed in
many forms and the exact implementation is determined by the specific nature of the situation (or
problem). As an example, consider how a frog, lizard and a fish move (“the interface”) from one place
to another. A frog may leap ten centimeters, a lizard in a single movement moves two centimeters
and a shark may swim three meters in a single movement. All these animals exhibit a common ability
– movement – expressed differently.
c. Inheritance: Inheritance is the process of building new classes based on existing classes. The new
class inherits the properties and attributes of the existing class. Object oriented programs models real
world concepts of inheritance. For example children inherit attributes and behaviors from their
parents. The attributes such as color of eyes, complexion, facial features etc represent the fields in an
1
java. Behaviors such as being a good dancer, having a good sense of humor etc represent the
methods. The child may have other attributes and behaviors that differentiate them from the parents.

COMPONENTS OF A JAVA APPLICATION PROGRAM


Every Java application program comprises of a class declaration header, fields (instance variables –
which is optional), the main method and several other methods as required for solving the problem.
The methods and fields are members of the class.
OUR FIRST JAVA PROGRAM.
/*
* HelloWorld.java
* Displays Hello world!!! to the output window
*
*/
public class HelloWorld // class definition header
{
public static void main( String[] args )
{
System.out.println( “Hello World!!! “ ); // print text
} // end method main
} // end class HelloWorld

COMMENT

Comments can be created by using the // symbols at the beginning of a line:


// This is a comment. This type of comment is termed as an in-line comment.

CLASS DEFINITION HEADER


The class definition header class definition header starts with the access modifier public followed by
the keyword class then the name of the class HelloWorld. The access modifier tells the Java
compiler that the class can be accessed outside the program file that it is declared in. The keyword
class tells Java that we want to define a class using the name HelloWorld.

The next part of the program is the declaration of the main method. Methods are used for carrying
out the desired tasks in a Java program, they are akin to functions used in C/C++ programming
languages. The listing:
public static void main( String[] args )
{
}
is the main method definition header. It starts with the access modifier public, followed by the
keyword static which implies that the method main ( ) may be called before an object of the class
has been created. The keyword void implies that the method will not return any value on completion
of its task. These keywords public, static, and void should always be placed in the sequenced shown.

THE BASIC STEPS FOR COMPILING AND EXECUTING A JAVA PROGRAM ARE:
a) Enter the source code using a text editor. The file must be saved using the file extension .java.
b) Use the Java compiler to convert the source code to its bytecode equivalent. The byte code will
be saved in a file having the same name as the program file with an extension .class. To
compile our HelloWorld.java program, type the following instructions at the Windows command
prompt (c:\>): javac HelloWorld.java The bytecodes (.class file) will be created only if there are
no compilation errors.
c) Finally use the Java interpreter to execute the application, to do this at the Windows command
prompt (c:\>) type: java HelloWorld. (You need not type the .class extension)

2
Using Simple Graphical User Interface Apply Graphical Classes
import javax.swing.JOptionPane;
public class HelloWorldGUI {
public static void main(String[] args) {
String msg = “WELLCOME";
String ans ="";
JOptionPane.showMessageDialog(null, msg );
ans = JOptionPane.showInputDialog( null, "Enter your Name Please");
JOptionPane.showMessageDialog(null, "Hello " + ans );
} // end method main
} // end of class HelloWorldGUI

JAVA DATA TYPES


A data type defines a set of values and the operations that can be defined on those values. Data types
in Java can be divided into two groups:

There are eight primitive data types in Java:


• Four subsets of integers
• Two subsets of floating point numbers
• A character data type, and
• A boolean data type.
JAVA HAS TWO BASIC KINDS OF NUMERIC VALUES:
• Integers, which have no fractional part, and floating points, which do. There are four integer
data types (byte, short, int, and long) and two floating point data types (float and double). All
of the numeric types differ by the amount of memory space used to store a value of that type,
which determines the range of values that can be represented. The size of each data type is
the same for all hardware platforms. All numeric types are signed, meaning that both positive
and negative values can be stored in them.

 the following line was used to declare an object of type Vehicle:


 Vehicle minivan = new Vehicle();
 This declaration performs two functions. First, it declares a variable called minivan of the class
type Vehicle
 This variable does not define an object. Instead, it is simply a variable that can refer to an
object
 Second, the declaration creates a physical copy of the object and assigns to minivan a
reference to that object.
 This is done by using the new operator.
 The new operator dynamically allocates (that is, allocates at run time) memory for an object
and returns a reference to it.
 This reference is, more or less, the address in memory of the object allocated by new.
 This reference is then stored in a variable.
 Thus, in Java, all class objects must be dynamically allocated

PSEUDOCODE
Pseudocode is an informal language that helps programmers develop algorithms without having to
worry about the strict details of Java language syntax. The pseudocode we present is particularly
useful for developing algorithms that will be converted to structured portions of Java programs.
Pseudocode is similar to everyday Englishit is convenient and user friendly, but it is not an actual
computer programming language.

JAVA HAS THREE TYPES OF SELECTION STATEMENTS.


3
• The if statement either performs (selects) an action if a condition is true or skips the action, if
the condition is false.
• The if...else statement performs an action if a condition is true and performs a different
action if the condition is false.
The switch statement performs one of many different actions, depending on the value of an
expression.

TYPES OF ERROR
• Syntax errors (e.g., when one brace in a block is left out of the program) are caught by the
compiler.
• logic error (e.g., when both braces in a block are left out of the program) has its effect at
execution time.
• fatal logic error causes a program to fail and terminate prematurely.
• Nonfatal logic error allows a program to continue executing, but causes the program to
produce incorrect results.

PSEUDOCODE: GENERATE AND CALCULATE THE SUM OF THE FIRST TEN NUMBERS FROM
1 TO 10
FIRST PSEUDOCODE:
Step1: Initialize variables
Step2: Generate numbers from 1 to 10
Step3: Calculate sum of the numbers
Step4: Display numbers and sum
Step5: Stop.

What are Strings?


• A string is a sequence of characters treated as a single unit.
• A string may include letters, digits and various special characters, such as +, -, *, / and $. A
string is an object of class String.
• String literals (stored in memory as String objects) are written as a sequence of characters in
double quotation marks, as in:
Graphical user interface (GUI)
A graphical user interface (GUI) presents a user-friendly mechanism for interacting with an
application.
The order in which arithmetic operators are applied on data values (operand) is termed rules of
operator precedence. These rules are similar to that of algebra. They enable Java to evaluate
arithmetic expressions consistently and correctly.
The rules can be summarized thus:
a. Multiplication, division and modulus are applied first. Arithmetic expressions with
several of these operators are evaluated from the left to the right.
b. Addition and subtraction are applied next. In the situation that an expression contains
several of these operators they are evaluated from right to left.

The order in which the expressions are evaluated is referred to as their association. Now let us
consider some examples in the light of the rules of operator precedence; we will list both the
algebraic expression and the equivalent java expression.

You might also like