Compiling and Running Java Compiling and Running Java Programs Programs
Compiling and Running Java Compiling and Running Java Programs Programs
Programs
Download Java compiler form http://java.sun.com
Also download English Java 2 SDK documentation from Sun Java page
To run Java program
Save file with .java extension using text editor
Use dos prompt, go to the directory where file is saved
Enter javac filename.java
.class version of the file is created
Errors could be as a result of:
You have not modified the path entry of your autoexec.bat file
correctly and the computer cannot execute the command javac
to compile your program.
You have made a mistake when typing the program, and the
syntax of at least one statement might be incorrect.
To execute the java object code, issue the following command in the
same window where you compiled the program:
java filename
If a program to fail during the execution phase, it must be stopped
from any further execution. If modifications to the program are
required, it is necessary to perform the amendments at phase 1,
and repeat phases 2 and 3.
SDK Tools
javac—the Java Language Compiler
that you use to compile programs
written in the Java programming language
into bytecodes.
java—the Java Interpreter that you
use to run programs written in the
Java programming language.
SDK tool documentation gives a full
explanation of the function of each tool.
Data
Data is the name given to characters and quantities operated upon by a
computer.
A computer program consists of a series of instructions for the
computer to execute and provides a method for processing data. The
data from bank transactions can be processed by a computer into
information for bank statements.
we can identify four data types: integer (a positive or negative whole
number), real (a positive or negative number with a decimal fraction),
character (a single character), and a string (a group of characters).
A variable is a named memory location that can hold a particular type
of data. For example, the following statement in a Java program will
reserve a memory location called count that can hold data of type
integer.
int count;
This statement is called a type declaration since it declares that the
variable count must hold data of type integer. We say that the data
type of count is int.
Data types: character
The type declaration for a character is
declared in Java as char.
E.g char myChar = 'A';
declares a variable called myChar, of type
char which can hold a character as a
value, and is initialized to the character 'A'
Integer numbers
An integer is stored as a binary number. In Java there are
several integer data types. The one we will be using most frequently
is declared as int and uses four bytes of computer memory.
Therefore, an int can represent any of 232 = 4,294,967,296
different integers. The range of int values is 2,147,483,648 to
+2,147,483,647.
If you want to store an integer number that lies outside of the
range for int types, then use the Java type long. These numbers are
represented with eight bytes (64 bits) and have a range of
9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807.
The use of a plus sign (+) is optional for positive integer literals. All
decimal integer literals must begin with a digit in the range from 1
to 9 after the sign (if a sign is present). Integer literals must not
begin with 0 (zero). A long integer literal, either decimal or
hexadecimal, has the character l or L appended immediately after
the number.
Data types: Real numbers
A real number is stored in the computer memory in two parts, a mantissa (the fractional
part) and an exponent (the power to which the base of the number must be raised in
order to give the correct value of the number when multiplied by the mantissa). For
example, 437.875 can be rewritten as 0.437875 x 103, where 0.437875 is the mantissa and
3 is the exponent. A four-byte representation of a real number will give a maximum value
of 3.40282347 X 1038 and the smallest value as 1.40239846 x1045. The majority of
decimal fractions
do not convert exactly into binary fractions; therefore, the representation of a real
number is not always accurate.
In Java, the type real is declared as float.
If the float range is too restrictive for the real numbers being stored, Java can store much
larger real numbers using the type double.The number of bytes used to store a double-
precision number is increased to eight.This increase in storage space will give a maximum
value of 1.79769313486231570 x 10308 and the smallest value as
4.94065645841246544 x 10324.
A real-number literal can be written in one of two ways. For example, the literal 123.456
can be written as depicted or using a scientific notation 1.23456E+2.The character E
represents the base 10, so the number can be interpreted as 1.23456 102, which, of
course, evaluates to 123.456 when you adjust the decimal point.
The data types char, int, long, float, and double are known as primitive data types. Only a
selection of the primitive data types that you are likely to use in this course have been
presented
Identifiers
To distinguish data in different areas of memory, we
could give the data a name or use the numeric
memory address of the first byte of the address in
which the data is stored.
An identifier may contain combinations of the letters
of the alphabet (both uppercase A-Z and lowercase a-
z), an underscore character _, a dollar sign $, and
decimal digits 0-9. The identifier may start with any of
these characters with the exception of a decimal
digit.
Java is a case-sensitive language, meaning that uppercase
letters and lowercase
letters of the alphabet are treated as different letters.
Identifiers can normally be of any practicable length.
Identifiers (contd.)
An identifier must not be the same as the Java keywords listed below
A programmer should always compose identifiers so they convey meaning
of the data that they represent
When an identifier is constructed from more than one word, each
successive word should begin with an uppercase letter; an identifier should
be easy to read, and its meaning should be clear.
Evaluation of (X * X + Y * Y) / (A + B)
With the exception of the % remainder operator, which must have integer operands, all
Evaluation of A + B * C - D / E other operators can have integer or real operands or a mixture of both types. In a
division, if both the operands are integer, then the result will also be an integer, i.e., the
fractional remainder in the result will be truncated.
Casting: When operands are of different types, one or more
of the operands must be converted to a type that can safely
accommodate all values before any arithmetic can be
performed
SYNTAX:
◦ Cast operation: (data-type)expression;
For example,
float money = 158.05;
int looseChange = 275;
money = (float) looseChange;