An Overview of JAVA

An Overview of JAVAAs in all other computer languages, the elements of JAVA do not exist in isolation.

Rather, they work together to form the language as a whole.

However, this interrelatedness can make it difficult to describe one aspect of Java without involving several others.

Often a discussion of one feature implies prior knowledge of another.

For this reason, I am writing this article for you all to get a quick overview of several key features of Java.

DammnnBlockedUnblockFollowFollowingFeb 13Object-oriented programming is at the core of Java.

In fact, all JAVA programs are to at least some extent object oriented.

OOP is so integral to JAVA that it is best to understand its basic principles before you begin writing even simple JAVA program.

All object-oriented programming languages provide a mechanism that helps you implement the object-oriented model.

They are encapsulation, inheritance, and polymorphism.

ENCAPSULATIONEncapsulation is the mechanism that binds together code and the data it manipulates and keeps both safes from outside interference and misuse.

One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper.

Access to the code and data inside the wrapper is tightly controlled through a well-defined interface.

The power of encapsulated code is that everyone knows how to access it and thus can use it regardless of the implementation details-and without fear of unexpected side effects.

In JAVA, the basis of encapsulation is the class.

A class defines the structure and behaviour that will be shared by a set of objects.

Each object of a given class contains the structure and behaviour defined by the class as if it were stamped out by a mould in the shape of the class.

For this reason, objects are sometimes referred to as an instance of the class.

Thus, a class is a logical construct, an object has physical reality.

When you create a class, you will specify the code and data that constitute that class.

Collectively, these elements are called members of the class.

Specifically, the data defined by the class are referred to as member variables or instance variables.

The code that operates on that data is referred to as a member method or just methods.

In a properly written Java program, the methods define how the member variables can be used.

This means that the behaviour and interface of a class are defined by the methods that operate on its instance data.

Since the purpose of a class is to encapsulate complexity, there is a mechanism for hiding the complexity of the implementation inside the class.

Each method or variable in a class may be marked private or public.

The public interface of a class represents everything the external users of the class need to know or may know.

The private methods and data can only be accessed by the code that is a member of the class.

Therefore, any other code that is not a member of the class cannot access a private method or a variable.

Since the private members of a class may only be accessed by other parts of your program through the class public methods, you can ensure that no improper actions take place.

INHERITANCEInheritance is the process by which one object acquires the properties of another object.

This is important because it supports the concept of hierarchical classification.

As mentioned earlier knowledge is made manageable by hierarchical classifications.

Without the use of hierarchies, each object would need to define all of its characteristics explicitly.

However, by the use of inheritance, an object need only define those qualities that make it unique within its class.

It can inherit its general attributes from its parent.

Thus, it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case.

Inheritance interacts with encapsulation as well.

If a given class encapsulate some attributes, then any subclass will have the same attributes plus any that it adds as a part of its specialization.

This is a key concept that lets object-oriented programs grow in complexity linearly rather than geometrically.

A new subclass inherits all of the attributes of all of its ancestors.

It does not have unpredictable interactions with the majority of the rest of the code in the system.

POLYMORPHISMPolymorphism is a feature that allows one interface to be used for a general class of actions.

The specific action is determined by the exact nature of the situation.

Because of polymorphism, in JAVA you can specify a general set of stack routines that all share at the same time.

More generally, the concept of polymorphism is often expressed by the phrase one interface, multiple methods.

This means that it is possible to design a generic interface to a group related activities.

This helps reduce complexity by allowing the same interface to be used to specify a general class of action.

It is the compiler’s job to select the specific action as it applies to each situation.

You, the programmer do not need to make this selection manually.

You need only remember and utilize the general interface.

When properly applied, polymorphism, encapsulation, and inheritance combine to produce a programming environment that supports the development of far more robust and scalable programs than does the process-oriented model.

A well-designed hierarchy of classes is the basis for reusing the code in which you have invested time and effort developing and testing.

Encapsulation allows you to migrate your implementations over time without breaking the code that depends on the public interface of your classes.

Polymorphism allows you to create clean, sensible, readable, and resilient code.

ENTERING THE PROGRAMFor most computer language, the name of the file that holds the source code to a program is immaterial.

However, this is not the case with JAVA.

The first thing that you must learn about java is that the name you give to a source file is very important.

For this example, the name of the source file should be Example.

java.

Let’s see why.

In Java, a source file is officially called a compilation unit.

It is a text file that contains one or more class definitions.

The Java compiler requires that a source file use the .

java filename extension.

As you can see by looking at the program, the name of the class defined by the program is also Example.

This is not a coincidence.

In Java, all code must reside inside a class.

By convention, the name of that class should match the name of the file that holds the program.

You should also make sure that the capitalization of the filename matches the class name.

The reason for this is that Java is case-sensitive.

At this point, the convention that filenames correspond to class names may seem arbitrary.

A FIRST SIMPLE PROGRAMNow that you have got some basic idea about what is object-oriented programming, let’s look at an example of Java program.

Let’s start by compiling and running a short Java program.

/*This is a simple Java program.

Call this file “Example.

java”.

*/class Example {// Your program begins with a call to main().

public static void main(String args[]) {System.

out.

println(“This is a simple Java program.

”);}}For most computer languages, the name of the file that holds the source code to a program is immaterial.

However, the way Java does things is a bit different than other programming languages.

The first thing that you must learn about Java programing is that the name you give to a source file is very important.

For this example, the name of the source file should be Example.

java.

The reason is for this is, that a source file is officially called a compilation unit.

It is a text file that contains one or more class definitions.

The Java compiler requires that a source file use the .

java filename extension.

As you can see by looking at the program, the name of the class defined by the program is also Example.

This is not a coincidence.

In Java, all code must reside inside a class.

By convention, the name of that class should match the name of the file that holds the program.

If your class name is not exactly same as the filename, then your java program will throw an error.

You should also make sure that the capitalization of the filename matches the class name.

This is because Java is case-sensitive.

At this point, the convention that filenames correspond to class names may seem arbitrary.

Although Example.

java is quite simple, it includes several key features that are common to all Java program.

The program begins with the following lines:/*This is a simple Java program.

Call this file "Example.

java".

*/This is a comment.

Like other programming languages, Java lets you enter a remark into a program’s source file.

The contents of a comment are ignored by the compiler.

Instead, a comment describes or explains the operation of the program to anyone who is reading its source code.

In this case, the comment describes the program and reminds you that the source file should be called Example.

java.

Of course, in real applications, comments generally explains how some part of the program works or what that part of the code does.

Java supports three styles of comments.

The one shown at the top of the program is called a multiline comment.

This type of comment must begin with /* and end with */.

Anything between these two comment symbols is ignored by the compiler.

As the name suggests, a multiline comment may be several lines long.

The next line of code in the program is shown here:Class Example {This line uses the keyword class to declare that a new class is being defined.

An example is an identifier that is the name of the class.

The entire class definition, including all of its members, will be between the opening curly brace ({) and the closing curly brace (}).

The next line of code is:public static void main(String args[]) {This line begins the main() method.

As the comment preceding it suggests, this is the line at which the program will begin executing.

All Java applications begin execution by calling main().

The full meaning of each part of this line cannot be given now since it involves a detailed understanding of Java’s approach to encapsulation.

Now let’s look at other things which we missed out.

The public keyword is an access specifier, which allows the programmer to control the visibility of class members.

When a class member is preceded by the public, then the member may be accessed by code outside the class in which it is declared.

The opposite of public is private, which prevents a member from being used by code defined outside of its class.

In this case, main() must be declared as public, since it must be called by code outside of its class when the program is started.

The keyword static allows main() must be declared as public, since it must be called by code outside of its class when the program is started.

The keyword static allows main() to be called without having to instantiate a particular instance of the class.

This is necessary since main() is called by the Java virtual machine before any objects are made.

The keyword void simply tells the compiler that main() does not return a value.

As you will see, methods may also return values.

If all this seems a bit confusing don’t worry.

As stated, main() is the method called when a Java application begins.

Keep in mind that Java is a case-sensitive.

Thus, Main is different from main.

It is important to understand that the Java compiler will compile classes that do not contain a main() method.

But Java has no way to run these classes.

So, If you had type Main instead of main, the compiler would still compile your program.

However, java would report an error because it would be unable to find main() method.

Any information that you need to a method is received by variables specified within the set of parentheses that follow the name of the method.

These variables are called parameters.

If there are no parameters required for a given method, you still need to include the empty parentheses.

In main(), there is only one parameter, albeit a complicated one.

String args[] declares a parameter named args, which is an array of instance of the class String.

Object of type string store character strings.

In this case, args receives any command line argument present when the program is executed.

This program does not make use of this information.

The last character on the line is the {.

This signals the start of main()’s body.

All of the code that comprises a method will occur between the method’s opening curly braces and its closing curly braces.

One other point: main() is simply a starting place for your program.

A complex program will have dozens of classes, only one of which will need to have a main() method to get things started.

The last line of code isSystem.

out.

println(“This is a simple Java program.

”);This line outputs the string “this is a simple Java program”.

Followed by a new line on the screen.

The output is actually accomplished by the built-in println() method.

In this case, println() displays the string which is passed to it.

as you will see, println() can be used to display other types of information, too.

The line begins with System.

out.

The system is a predefined class that provides access to the system, and out is the output stream that is connected to the console.

As you have probably guessed, console output is not used frequently in most real-world Java programs and applets.

Since the most modern computing environments are windowed and graphical in nature, console I/O is used mostly for simple utility programs and for demonstration programs.

Notice that the println() statement ends with a semicolon.

All statements in Java end with a semicolon.

The reason that the other lines in the program do not end in a semicolon is that they are not, technically, statements.

The first } in the program ends main(), and the last } ends the Example class definition.

Perhaps no other concept is more fundamental to a programming language than that of a variable.

As you probably know, a variable is a named memory location that may be assigned a value by your program.

The value may be changed during the execution of the program.

Perhaps no other concept is more fundamental to a programming language than that of a variable.

As you probably know, a variable is a named memory location that may be assigned a value by your program.

The value of a variable may be changed during the execution of the program.

The next program shows how a variable is declared and how it is assigned a value.

The program also illustrates some new aspects of console output.

As the comments at the top of the program state, you would call this file as Example2.

java.

class Example2 {public static void main(String args[]) {int num; // this declares a variable called numnum = 100; // this assigns num the value 100System.

out.

println("This is num: " + num);num = num * 2;System.

out.

print("The value of num * 2 is ");System.

out.

println(num);}}When you run this program, you will see the following output:This is num: 100The value of num * 2 is 200Let’s take a close look at why this output is generated.

The first new line in the program is shown here:int num; // this declares a variable called numThis line declares an integer variable called num.

java requires that variable is declared before they are used.

Following is the general form of a variable declaration.

type var-name;Here, type specifies the type of variable being declared, and var-name is the name of the variable.

If you want to declare more than one variable of the specified type, you may use a comma-separated list of variable names.

Java defines several data types, including integer, character, and floating point.

The keyword int specifies an integer type.

In the program, the linenum = 100; // this assigns num the value 100this line assigns to num the value of 100.

In Java, the assignment operator is a single equal sign.

The next line of code outputs the value of num preceded by the string “This is num”System.

out.

println("This is num: " + num);In this statement, the plus sign causes the value of num to be appended to the string that precedes it, and then the resulting string is output.

This approach can be generalized.

Using the + operator, you can join together as many items as you want within a single println() statement.

The next line of code assigns num the value of num time 2.

Like most other languages, Java uses the * operator to indicate multiplication.

After this line executes, num will contain the value 200.

Here are the next two lines in the program.

System.

out.

print("The value of num * 2 is ");System.

out.

println(num);Several new things are occurring here.

First, the built-in method println() is used to display the string “The value of num is * 2 is”.

This string is not followed by a newline.

This means that when the next output is generated, it will start on the same line.

The print() method is just like println(), except that it does not output a newline character after each call.

Now, look at the call to println().

Notice that num is used by itself.

Both print() and println() can be used to output values of any of Java’s built-in types.

THE IF STATEMENTThe Java if statement works much like IF statement in any other language.

If(condition) statement;Here, condition is a Boolean expression.

If condition is true, then the statement is executed.

If condition is false, then the statement is bypassed.

Here is an example:if(num < 100) System.

out.

println(“num is less than 100”);In this case, if num contains a value that is less than 100, the conditional expression is true, and println() will execute.

If num contains a value greater than or equal to 100, then the println() method is bypassed.

class IfSample {public static void main(String args[]) {int x, y;x = 10; y = 20;if(x < y) System.

out.

println("x is less than y");x = x * 2;if(x == y) System.

out.

println("x now equal to y");x = x * 2;if(x > y) System.

out.

println("x now greater than y");// this won't display anythingif(x == y) System.

out.

println("you won't see this");}}The output generated by this program is shown here:x is less than yx now equal to yx now greater than yNotice one other thing in this program.

The lineint x, y;declares two variables, x and y, by use of a comma-separated list.

THE FOR LOOPAs you may know from your previous programming experience, loop statements are an important part of nearly any programming language.

The simplest form of the for loop is:for(initialization; condition; iteration) statement;In its most common form, the initialization portion of the loop sets a loop control variable to an initial value.

The condition is a Boolean expression that tests the loop control variable.

If the outcome of that test is true, the for loop continues to iterate.

If it is false, the loop terminates.

The iteration expression determines how the loop control variable is changed each time the loop iterates.

Here is a short program that illustrates the for a loop.

class ForTest {public static void main(String args[]) {int x;for(x = 0; x<10; x = x+1)System.

out.

println("This is x: " + x);} }This program generates the following output:This is x: 0This is x: 1This is x: 2This is x: 3This is x: 4This is x: 5This is x: 6This is x: 7This is x: 8This is x: 9In this example, x is the loop control variable.

It is initialized to zero in the initialization portion of the for.

At the start of each iteration, the conditional test x < 10 is performed.

If the outcome of this test is true, the println() statement is executed, and then the iteration portion of the loop is executed.

This process continues until the condition test is false.

As a point of interest, in professionally written Java programs you will almost never see the iteration portion of the loop written as shown in the preceding program.

That is, you will seldom see a statement like this:X = x + 1;The reason is that Java includes a special increment operator which performs this operation more efficiently.

The increment operator is ++.

The increment operator increases its operands by one.

By use of the increment operator, the preceding statement can be written like this:X++;Thus, for in the preceding program will usually be written like this:for(x = 0; x<10; x++)Now that you have seen several short Java programs, it’s time to more formally describe, the atomic element of Java.

Java program is a collection of whitespace, identifiers, literals, comments, operators, separates and keywords.

WHITESPACEJava is a free form of language.

This means that you do not need to follow any special indentation rules.

IDENTIFIERSIdentifiers are used for the class name, method names, and variable names.

An identifier may be any descriptive sequence of uppercase and lowercase letters, numbers, or the underscore and dollar sign character.

They must not begin with a number, lest they are confused with a numeric literal.

Again, Java is case-sensitive, so VALUE is a different identifier than Value.

LITERALSA constant value in Java is created by using a literal representation.

COMMENTSAs mentioned, there is three types of comments defined by Java.

You have already seen two which are a single line and multiple lines.

The third type is called a documentation comment.

This type of comment is used to produce an HTML file that documents your program.

The documentation comment begins with a /** and ends with a */.

.

. More details

Leave a Reply