Pages

Monday, 15 February 2016

Overview of Java Language

Developing the Java Application
A Java application is a standalone Java program-- a program written in the Java language that runs independently of any browser.
Example:
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp
{
public static void main(String[] args)
{
System.out.println("Hello World!"); //Display the string.
}
}

Comments in Java Code
The bold characters in the following listing are comments.
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/

class HelloWorldApp
{
public static void main(String[] args)
{
System.out.println("Hello World!"); //Display the string.
}
}
The Java language supports three kinds of comments:
/* text */
The compiler ignores everything from /* to */.
/** documentation */
This indicates a documentation comment (doc comment, for short). The compilerignores this kind of comment, just like it ignores comments that use /* and */.
// text
The compiler ignores everything from // to the end of the line.


Defining a Class
The first bold line in the following listing begins a class definition block.
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp
{
public static void main(String[] args)
{
System.out.println("Hello World!"); //Display the string.
}
}
A class--the basic building block of an object-oriented language such as Java--is a template that describes the data and behavior associated with instances of that class.

The data associated with a class or object is stored in variables; the behavior associated with a class or object is implemented with methods. Methods are similar to the functions or procedures in procedural languages such as C.

In the Java language, the simplest form of a class definition is
class name {
. . .
}
The keyword class begins the class definition for a class named name.
The variables and methods of the class are embraced by the curly brackets that begin and end the class definition block. The "Hello World" application has no variables and has a single method named main.

The main Method
The first bold line in the following listing begins the definition of a main method.
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/

class HelloWorldApp {
public static void main(String[] args)
{
System.out.println("Hello World!"); //Display the string.
}
}

Every Java application must contain a main method whose signature looks like this:

public static void main(String[] args)
{
System.out.println("Hello World!"); //Display the string.
}
}


Every Java application must contain a main method whose signature looks like this:

public static void main(String[] args)

The method signature for the main method contains three modifiers:

• public indicates that the main method can be called by any object.
• static indicates that the main method is a class method.
• void indicates that the main method doesn't return any value.
How the main Method Gets Called
The main method in the Java language is similar to the main function in C and C++. When the Java interpreter executes an application (by being invoked upon the application's controlling class), it starts by calling the class's main method. The main method then calls all the other methods required to run your application. If you try to invoke the Java interpreter on a class that does not have a main method, the interpreter refuses to compile your program and displays an error message similar to this: In class NoMain: void main(String argv[]) is not defined
Arguments to the main Method
Here the main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information to your application. Each String in the array is called a command-line argument.


Using Classes and Objects
The "Hello World" application is about the simplest Java program one can write that actually does something. Because it is such a simple program, it doesn't need to define any classes except for HelloWorldApp.

The "Hello World" application does use another class--the System class--that is part of the API (application programming interface) provided with the Java environment. The System class provides system-independent access to system-dependent functionality.

The bold code in the following listing illustrates the use of a class variable of the System class, and of an instance method.
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/

class HelloWorldApp
{
public static void main(String[] args)
{
System.out.println("Hello World!"); //Display the string.
}
}
Using a Class Method or Variable
Let's take a look at the first segment of the statement:
System.out .println("Hello World!");  The construct System.out is the full name of the out variable in the System class.
Using an Instance Method or Variable
Methods and variables that are not class methods or class variables are known as instance methods and instance variables.

While System's out variable is a class variable, it refers to an instance of the PrintStream class (a class provided with the Java development environment) that implements the standard output stream.

When the System class is loaded into the application, it instantiates PrintStream and assigns the new PrintStream object to the out class variable.

System.out.println("Hello World!");


This line of code displays "Hello World!" to the application's standard output stream.

Java Program Structure
A Java program may contain one or more section which is as follow:
1. Documentation section (suggested)
The documentation section comprises a set of comment line of the program, the author
and other details, which the programmer would like to refer to at a later stage.
2. Package statement (Optional)
This statement declares a package name and inform the compiler that the class defined here belong to this package.
Ex: package student;
3. Import Statement (Optional)
This statement instructs the interpreter to load the specific class contained in the package.
Ex: import student. test;
4. Interface Statement (Optional)
An interface is like a class but include a group of method declarations.
5. Class Definitions (Optional)
Classes are the primary and essential elements of Java program. These classes are used
to map the object of real world problems.
6. Main Method Class (Essential)
As every Java stand-alone program requires a main method as its starting point, this class is the essential part of the program.

Java Tokens
The smallest individual units in a program are known as TOKEN.
A java program is a collection of tokens, comments and white spaces.
Java language includes five types of tokens
They are:
1) Reserved Keyword
2) Identifiders
3) Literals
4) Operator
5) Separators
Reserved Keyword
Keywords have specific meaning and implement specific features of the language.
Java language has reserved 60 words as keywords.
Identifiers
Identifiers are programmer-designed tokens. They are used for naming class,
methods, variables, objects, labels, packages, and interface in a program.
Literals
Literal is a programming language term that essentially means that what you type is what you get. Numbers, characters, and strings are all examples of literals.
Therefore literals
1) Are constants having no identifier?
2) Have their value specified within the program's source code?
3) Can only appear on the right side of an assignment operator (=) or within an      expression?
4) Have a data type associated with them?
Java program has five major types of literals:
1) Integer Literals
2) Floating type literals
3) Character literals
4) String literals
5) Boolean Literals
Integer literals
1. Represent an integer value
2. Can be expressed in decimal (the default), octal (base 8, or hexadecimal (base 16)
3. Are not enclosed in any special characters
4. Are automatically int (32 bits) unless the suffix 'L' is appended to make it long (64 bits)
Floating-point literals
1. Represent a real number (having a decimal point)
2. Can be expressed as a standard decimal value or in scientific notation
3. Are not enclosed in any special characters
4. Are automatically double (64 bits) unless the suffix 'F' is appended to make it float (32     bits)
char literals
1. Represent a single Unicode character (16 bits)
2. Must be enclosed within single quotes (apostrophes)
3. Are often associated with a single key stroke
4. Can represent special characters ("escape sequences") used for device control
String literals
1. Represent a string of characters, such as "Java is fun"
2. Must be enclosed in double quotes
3. Are automatically stored as String class objects by the compiler. They will be covered     later.
boolean literals
1. Can only have the value true or false
2. Can only be assigned to boolean variables
Constants
1. Are similar to variables but, once initialized, their contents may NOT be changed?
2. Are declared with the keyword final?
3. By convention, have all capital letters in their identifier. This makes them easier to see within the code.
Operators
An operator is a symbols that takes one or more arguments and operates on them to produce a result.
Separators
Separators are symbols used to indicate where groups of code are divided and arranged. They are basically define the shape and function of our code.
Ex: {}, [], ; , . , () etc.


   Previous Page                Scroll Up                        Next Page

No comments:

Post a Comment