Java Language

Introduction to Java Programs

There are two types of Java programs - applications and applets. An application is a stand alone program whereas an applet is a program embedded in a web page that runs on the client machine.

The Java compiler compiles the source code of an application to the machine code of the Java Virtual Machine (JVM). The machine code of the JVM is known as byte-code. The byte code can be interpreted or it can be compiled by the JIT (just-in-time) compiler to machine code, or it can be run on computer chips whose machine language is byte-code.

A compiler translates the source code of a program into machine language of the computer. The compiler produces a file containing the optimized executable code. But this executable code is not platform independent, since different processors have different instruction set. Interpreters on the other hand directly parse and execute line by line the source code. The interpreter does not optimize the execution nor does it produce an executable file. Interpretation is platform independent but slow.

A program in Java consists of one or more class definitions, each of which has been compiled into its own .class file of the JVM byte code. One of these classes must define a method main() which is where the program starts running.

If a file contains a public class called ClassName then it must be saved in a text file called ClassName.java. JAVA IS CASE SENSITIVE.

A language's syntax is the set of rules that determine whether a particular statement is correctly formulated. Semantics is the meaning of each statement.

Comments

Character Set

Other languages, like C or C++, use the ASCII character set. The ASCII character set represents characters by a byte. Actually 7 bits of the byte are used. So at most 128 characters can be represented. This is sufficient to represent the letters of the English alphabet as well as the decimal digits from 0 to 9, and all the punctuation marks. However, if you have to represent letters of the alphabet of other languages, a byte does not offer sufficient storage. Java from the beginning has adopted the 16-bit Unicode system. The ASCII representation is a subset of the Unicode system. To represent a character that from another language one has to use the acutal Unicode. The convention used is \uxxxx where xxxx are 4 hexadecimal numbers.

Identifiers

Variables

A variable marks a place in memory to store data. Every variable has a type which tells the compiler how to interpret the string of bits in that memory location. A variable has to be declared before using.

Primitive Types

In Java there are 8 primitive types. The sizes of these types are fixed for the Java Virtual Machine. Octal integer literal begins with 0 and hexadecimal integer literal begins with 0x or 0X.

Declaring a Variable

The generic way of declaring a variable is
dataType variableName;

int i;
char ch;
boolean found;

Assignment

variable = expression;

int i = 0;
short x = 0xBF;
float area = length * width;

Constants


final datatype CONSTANTNAME = VALUE;

final double PI = 3.14159;

final int RATE = 6;

Constants have declared and initialized. You cannot change the value of a constant.

Numeric Operators

A literal is a numeric value that is assigned to a variable like 2 or 5.8. Floating point numbers can be represented in the scientific notation 5.43e+2 which is 543. The numeric operators +, -, *, / can be applied to all integers and floating point numbers. Integer division truncates the result. There are two symbols used to handle overflow in case of floating point division:

Shortcut Operators

x += 4 x = x + 4
x -= 4 x = x - 4
x *= 4 x = x * 4
x /= 4 x = x / 4
x %= 4 x = x % 4

Increment and Decrement Operators

Increment and decrement operators can be applied to all integers and floating point types. They can be used either prefix (--x, ++x) or postfix (x--, x++) mode.

Prefix Increment Operation

int x = 2;
int y = ++x;	// x == 3, y == 3

Postfix Increment Operation

int x = 2;
int y = x++;	// x == 3, y == 2