Professional Documents
Culture Documents
Introduction To Java: Grade VIII Computer Applications
Introduction To Java: Grade VIII Computer Applications
Computer Applications
Introduction to Java
Disadvantages
a. Coding in the machine language is an extremely cumbersome and complicated
task that is prone to errors.
b. The language itself is not very human readable. (i.e. user friendly).
c. The language is also machine dependent and requires knowledge of complete
computer circuitry.
ii) Assembly Language: (2nd Generation Language) The first language similar to
English was developed in 1950 which was known as Assembly Language or
Symbolic Programming Language. Assembly language is a low level language
that uses small symbolic words called mnemonics instead of zeroes and ones.
These mnemonics like ADD to add, MUL to multiply, LOC to locate an address
and such others express the operations to be performed. A translator called the
assembler converts these mnemonics to the machine language and performs the
necessary action.
Advantages: Assembly language is very powerful and efficient. It is fast, small in
program size and flexible. This language is popularly used at microprocessor level
and system coding.
Disadvantages: It is not very user friendly and easily understandable.
The assembler must be loaded to the computer memory for translation and it
occupies a sizeable memory of the computer, hence Assembly Language cannot
be used with small capacity computers.
iii) High Level Language: (3rd Generation Language) Procedural languages are used
to solve problems and are often described as problem oriented languages. They
focus on the method or HOW to solve the problem. In high level languages, the
programs are written in a language which is closer to the user i.e. English, just
following some rules specific for the language (syntax). Every instruction that
the programmer writes in this language is translated into the machine language
using a language processor /translator called the compiler or the interpreter.
Advantages: HLL is independent of the internal machine code of any particular
computer. High Level language is the most understandable and easy to code
language besides having the following advantages, easier documentation, easy to
learn and use and modifications can be done more easily.
Disadvantages: The syntax and the semantics of the language need to be learnt
and only then can it be used.
BASIC, COBOL, Pascal, FORTRAN, C, C++, Java are some examples of HLL.
Programming in HLL can be done in one of the following two approaches:
a) Procedural Programming Approach
b) Object Oriented Programming Approach.
iv) Non-Procedural Language: 4GLs are non procedural, that is only “WHAT is
required” is to be specified and the rest gets done on its own. The main aim of the
4GL is to cut down on development and maintenance time and make it easier for
users. The user interacts with the utility software which is an integral part of a
4GL and that in turn interacts with the system to get the work done. Word
processors, spreadsheets, DBMS, graphic software are some of the examples of
4GLs.
B) Interpreter: This language processor converts s a HLL program into machine language by
converting and executing it line by line. It there is any error in any line, it reports it at the
same time and program execution cannot be resumed till the error is rectified. The interpreter
must always be present in the memory every time the program is executed. For error
debugging, interpreter is very much useful as it reports the errors immediately on occurrence
but after the program is debugged, unnecessary usage of memory takes place as it has to be
present in the memory always.
Therefore, combination of interpreter and compiler is an ideal option of HLL
translation to the machine code. For the initial error removal, the interpreter can be used and
then the program can be compiled enabling the removal of the language translator from the
memory.
Programming Approach
Programming in HLL can be done in one of the following two approaches:
a) Procedural Programming Approach
b) Object Oriented Programming Approach.
Drawbacks:
i) The modules or parts of the whole program strongly depend on each other.
So a module cannot be reused by other programs.
ii) The procedural program code is not flexible to change. The program
written in a procedural language completely needs to be changed as all the
parts of a program are linked and dependent on each other.
iii) Solutions to real world problems cannot be directly depicted using this
language.
b) Object Oriented Programming: OOP simulates the real world in software. Real
world consists of identifiable entities or objects. These objects have their peculiar
characteristics and behavior. Examples of real world objects are books, pens, students,
chair, and car and so on. This approach concentrates on “WHAT”- “What is the object
that will get affected by this problem?” and then the approach works on the
attributes/state and behavior/methods of that object. Examples: Smalltalk, C++, Java.
Advantages:
i) This approach models the real world quite accurately as the programs
written in the language are focused on the objects rather than the
procedure.
ii) It is extremely reusable as these objects can be invoked in different
programs.
iii) Programs written in OOP are simple to change when the requirements for
the application changes.
iv) The principles of OOP prevent the objects from being mishandled and are
kept safe from unauthorized access.
JAVA
(An Object Oriented Programming Language)
History of Java
Sun Micro systems began for software to run on TV and VCR sets i.e. Interactive TV
and VCR sets. A team of software engineers led by James Gosling, Patric Naughton, Chris
Warth, Ed Frank and Mike Sheridan laid specifications for these projects in 1991. By
September 1992, the basic system was drafted. These interactive sets were called set-top
boxes. The hardware was called 7(Seven), OS was called Green and the programming
language was called OAK.
Between the initial implementation of OAK in the fall of 1992 and the public
announcement of Java in 1995, many more people contributed to the design and the evolution
of the language. Bill Joy, Arther van Hoff, Jonathan Payne, Frank Yellin and Tim Lindholm
were key contributors to the maturing of the older prototype.
Hot Java was the first commercial product to be developed completely in Java. On
May 23, 1995, Java was commercially released. The advent of Internet gave a tremendous
boost to this language. Java Programs can be embedded in HTML pages and downloaded by
Web browser to bring live animations and interaction to web clients. The power of Java is not
limited to web applications. Java is a general purpose programming language. It has full
programming features and can be used to develop standalone applications.
Characteristics of Java
JAVA APPLICATIONS
Java programs can be one of the two types: stand alone applications and applets.
Applications are stand-alone programs like any program written using high-level language.
Applications can be executed from any computer with a Java interpreter and are ideal for
developing software. Applets are special kind of Java programs that can run directly from
Java compatible Web browser. Applets are suitable for deploying (installing) Web projects.
Java Program Structure (For stand alone programs only)
Probably, the best way to start learning a programming language is to write a simple
program. [* Please consider the program structure as a mandatory syntax requirement, the
details of what is it will be learnt by you in due course of time]
static- static methods are class methods that do not need the creation of any object to
work with the method.
void – Functions or methods can return a value. void means nothing. The main
method does not return any value, hence it is declared void.
iii) { } The pair of curly braces called the block of code marks the scope of the main
method and the scope of the class. They indicate the boundaries.
Java Keywords: Keywords are the words that convey a special meaning to the language
compiler. These are reserved for special purpose and cannot be used as normal identifier
names.
abstract boolean break Byte case catch
char class const Continue default do
double else extends Final finally float
for goto if implements import instanceof
int interface long native new package
private protected public Return short static
strictfp super switch synchronized this throw
throws transient try Void volatile while
*true and false and null are reserved words but not keywords, they are literals.
Identifiers:
Identifiers are fundamental building blocks of a program. They are programmer designed
tokens. They are used for naming all the components of a program like the classes, methods,
variables, objects, labels, packages, and interfaces in a program. The identifier follows the
following rules:
1. They can have alphabets, digits, underscore, and dollar sign characters.
2. They must not begin with a digit.
3. Uppercase and lowercase letters are distinct. Java is case sensitive.
4. They can be of any length.
5. They must not be a keyword or a boolean or null literal.
Java developers follow some naming conventions for the identifiers. Conventions are good
programming practice to improve the readability of the program.
• Names of all public methods and instance variables start with a leading lowercase letter.
If the name has more than one word, the second and subsequent words are marked with a
leading uppercase letters.
E.g. average
totalMarks
• All private and local variables use only lowercase letters combined with underscores.
E.g. length
batch_strength
• All classes & interfaces start with a leading uppercase letter (and subsequent words are
marked with a leading uppercase letter).
E.g. Student
MotorCycle
• Variables that represent literal values use all uppercase letters & underscores.
Eg TOTAL
MAX_MARKS
Literals
They refer to fixed values that do not change during the execution of a program.
Integer literals
It refers to sequence of digits. There are 3 types of integer literals, namely, decimal integer,
octal integer and hexadecimal integer.
Decimal integer consists of a set of digits, 0 through 9, preceded by an optional minus sign.
An octal integer literal consists of digits 0 through 7 with a leading zero. A sequence of digits
preceded by 0x or 0X is considered as hexadecimal integer. They may also include alphabets
A through F.
Real literals
Integers containing fractional parts are called real (or floating point) literals. Eg. –0.75. It can
also be expressed in exponential notation. Eg. 2.345e2
Character literals
It contains a single character enclosed in single quotes. Eg. ‘5’, ‘s’ as well as certain escape
sequence characters.
String literals
It is a sequence of characters enclosed between double quotes. Eg. “ Good Day! “
Comments:
Comments or remarks are non executable statements in the program that improve the
readability of the program. They help in internal program documentation to understand the
code better. It is a convention to use comments regularly in the source code.
There are three types of comments in Java:
i. Single line comment //this is a comment
ii. Multi line comment /* all the enclosed
lines are comments */
iii. Documentation Comment
/** Program Name : Test
* Author : ABC
* Code Date: Feb 11, 2011
*/
Data Type:
Java like any other language provides ways to facilitate the handling of different types
of data. Data types are means to identify the type of data and the associated operations of
handling it.
Integer types
Holds whole numbers. Java supports four types of integers - byte, short, int and long. Java
values are signed meaning either positive or negative.
Data Types Size in Bytes
byte 1
short 2
int 4
long 8
Floating Point Types
They are used to hold numbers containing fractional parts. There are 2 types of floating point
storage- float and double. Float type values are single-precision while double represent
double-precision numbers.
Data Types Size in Bytes
float 4
double 8
Character Type
These are used to store character literals in memory. They are always enclosed in single
quotes e.g. ‘ a‘. The data type is called char. It occupies 2 bytes in memory.
Boolean Type
It is used to test a condition. A boolean datatype can only have one of these two values: true
or false. It occupies 1 byte in memory.
Variables
It is a named memory location storing varying values. It is an identifier that denotes a storage
location used to store a data value. It may take different values during program execution.
Relational operators
The comparison of 2 data items is done with the help of relational operator. The result of a
relational expression is either true or false. The relational operators are <, <=, >, >=, ==, !=.
Logical operators
It also yields true or false. The operators are &&(and), | |(or), !(not).