Prayagasoft - web designer India, Ecommerce developer india, Ecommerce design

Java Language Essentials

Introduction

This tutorial will focus on the Java language essentials. Even if you have no programming experience, you will find this article very useful. The Java programming language is a modern, evolutionary computing language that combines an elegant language design with powerful features that were previously available in other languages.

Java is an Object Oriented programming language. This means that everything is an object in Java and thus you can design programs that represent any real world entities such as employees, cars, etc. Java provides a class construct for representing user-defined entities. The class construct supports the creation of user-defined data types, such as Employee, that represent both the data that describes a particular employee and the manipulation or use of that data.

The following sections illustrate Java syntax and program design in the context of several Java class definitions.

Data Types

To define a user defined data type in Java, you have to use the class construct. For example, to create a program that behaves like a Car, you have to define a class that represents a car.

class Car {

void changeGear() {

System.out.println("Gear Changed");

}

}

The user defined data type begins with the keyword class, followed by its name, Car.

Methods

A method is a program construct that provides the mechanism for performing some act, in this case, changeGear. Given an instance of some entity, you invoke behavior with a dot syntax that associates an instance with a method in the class definition:

<instance>.<method>(<arguments>..)

For example, to invoke the method of a Car named mercedes:

mercedes.changeGear()

The Java programming language is strongly typed, meaning that it expects variables, variable values, return types, and so on, to match properly, partly because data types are used to distinguish among multiple methods with the same name. Method return types and parameters are specified during definition:

<return-type> <method-name>(<arguments>..) { }

Applications

Java applications consist of one or more classes that define data and behavior. When Java applications are compiled, they are translated in a stream of data called bytecode. The operations in the bytecode stream implement an instruction set for Java virtual machine. Programs that implement the JVM simply process Java class files, sometimes specific to a particular environment. The Java compiler stores this bytecode stream in a class file with the filename extension .class. Any Java interpreter can read/process this stream--it interprets each operation and its accompanying data (operands).

Java class files are portable across platforms. Because Java compilers produce bytecode files that follow a prescribed format and are machine independent, and because any Java interpreter can read and further translate the bytecodes to machine instructions, a Java program runs anywhere--without recompilation.

A class definition such as Car is stored in a Java source file with a matching name, in this case, Car.java. A Java compiler processes the source file producing the bytecode class file, in this case, Car.class.

A Java program consists of one or more class files, one of which must define a program starting point. In Java, a program's starting point is defined by a method named main. Likewise, a program must have a well-defined stopping point. With the Java programming language, one way to stop a program is by invoking/executing the method exit.

public class MyFirstProgram {

public static void main(String[] args) {

System.out.println("This is my first program.");

System.exit(0);

}

}

The signature for main is invariable. Also, System (java.lang.System) is a standard Java class; it defines many utility-type operations. Note that the 0 in the call to exit indicates to the calling program, the Java interpreter, that nothing went wrong; that is, the program is terminating normally, not in an error state.

Comment Syntax

Java supports three types of comments:

1) int a; // single line comment

2) /* Multi line

Comment

*/

int a;

3) /**

javadoc comment

*/

int a;

The javadoc documentation tool is quite powerful. The standard development distribution from Sun includes documentation built with javadoc; hence, one avenue for learning this tool is to study the HTML documentation alongside the Java source code, which contains the comments that javadoc converts into HTML.

Creating Class Instances

In the Java programming language, as with other languages, a program allocates objects dynamically. Its storage allocation operator is new:

new <data-type>(<arguments>...)

<data-type> <variable> = new <data-type>(<arguments>...)

For example:

class Car {

void changeGear() {

System.out.println("Gear Changed");

}

public static void main() {

Car mercedes = new Car();

mercedes.changeGear();

System.exit(0);

}

}

Data Types

The Java type system supports a variety of primitive data types, such as int for representing integer data, float for representing floating-point values, and others, as well as class-defined data types that exist in supporting libraries (Java packages). All Java primitive data types have lowercase letters.

The Java programming language has the following primitive types: char, boolean (true/false), byte, short, int, long, float, double.

Conditional Execution

Like other languages, the Java programming language provides constructs for conditional execution, specifically, if, switch, and the conditional operator ?.

if (<boolean-expression>)

<statement>...

else

<statement>...

switch (<expression>) {

case <const-expression>:

<statements>...

break;

more-case-statement-break-groups...

default:

<statements>...

}

(<boolean-expression>)? <if-true-expression> : <if-false-expression>

Default Variable Initializations

In the Java programming language, variable initialization depends on context. In general, if there is no explicit initialization for an instance variable definition, the Java runtime automatically initializes the variable to a "zero-like" value, depending on the data type:

Data Type Default Initialization value
Char \u0000
Boolean false
Byte 0
Short 0
Int 0
Long 0
Float 0.0
Double 0.0
<user-defined-type> null

Arrays

An array is a linear collection of data elements, each element directly accessible via its index. The first element has index 0; the last element has index n - 1. The syntax for creating an array object is:

<data-type>[] <variable-name>;

This declaration defines the array object--it does not allocate memory for the array object, nor does it allocate the elements of the array. Also, you may not specify a size within the square brackets.

To allocate an array, use the new operator:

int[] x = new int[5]; // array of five elements

To create multidimensional arrays, simply create arrays of arrays, for example,

T[][] t = new T[10][5];

Garbage Collection

One of the really powerful features of the Java virtual machine is its memory-management strategy. Other languages put the burden on the programmer to free the objects when they're no longer needed with an operator such as delete or a library function such as free. The Java programming language does not provide this functionality for the programmer because the Java runtime environment automatically reclaims the memory for objects that are no longer associated with a reference variable. This memory reclamation process is called garbage collection.

The garbage collector automatically runs periodically. You can manually invoke the garbage collector at any time with System.gc. However, this is only a suggestion that system runs the garbage collector, not a forced execution.

Runtime Environments and Class Path Settings

The Java runtime environment dynamically loads classes upon the first reference to the class. It searches for classes based on the directories listed in the environment variable CLASSPATH. You may have to set a classpath before running the Java compiler and interpreter, javac and java, respectively. Also, note that in some situations the installation procedure will automatically update the PATH environment variable, but if you're unable to run javac or java, be aware that this setting could be unchanged.

PATH environment variable settings vary across operating systems and vendors. In a Windows environment, the following setting augments the old/existing PATH setting (%PATH%) with C:\java\bin, the default installation directory for the JDK:

set PATH=%PATH%;C:\java\bin

If you find it necessary to set the CLASSPATH environment variable, for example, if you're using an early JDK from Sun, it should include all directories on your computer system where you have Java class files that you want the Java compiler and interpreter to locate. As you add new class-file directories, you will typically augment this classpath setting. In a Windows environment, the following statement sets CLASSPATH to include three components/sites:

set CLASSPATH=C:\java\lib\classes.zip;C:\myjava\classes;.

Note that this setting includes a zipped class file archive classes.zip in the lib directory of a particular Java environment's distribution directory, represented generically here as C:\java\. That is, most Java environments can read class files stored in archive files of type .zip and .jar, as well as unarchived class files in any specified directory. During installation, many Java environments "remember" the location of their class files; thus, setting the Java environment's class file location is not necessary. For instance, the Java 2 platform works properly without setting the environment variable (and stores the runtime classes in rt.jar, not classes.zip).

 

 

PHP ecommerce web developer India flash website designer India seo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81