Introduction to Java Programming

Only available on StudyMode
  • Download(s) : 143
  • Published : December 19, 2012
Open Document
Text Preview
2.3 Fundamental programming Structures in Java

A Simple Java Program
In Java, all program codes must be written inside a class. Classes in Java are declared using the keyword class followed by class name. //Simple Java Program
Class FirstProgram//declares a class, which is an object-oriented construct. {
public static void main(String args[])//defines a method named main
{
System.out.println(“My first program in Java”);/*similar to printf or cout in c& c++ */
}
}

//Simple Java Program
Class FirstProgram//declares a class, which is an object-oriented construct. {
public static void main(String args[])//defines a method named main
{
System.out.println(“My first program in Java”);/*similar to printf or cout in c& c++ */
}
}

Comments
Java permits both the single-line and multi-line comments.
// for single line
/* for multiple line comment used this */

Java Program Structure
A Java program may contain many classes of which only one class defines a main method. Classes contain data members and methods that operate on the data members of the class. Methods may contain data type declarations and executable statements. To write a Java program, we first define classes and then put them together. A Java program may contain one or more sections as shown in fig 2.2. Documentation|

Package Statements|
Import Statements|
Interface Statements|
Class Definition|
Main Method Class{Main Method Definition}|
Suggested
Optional
Optional
Optional
Optional
Essential
Suggested
Optional
Optional
Optional
Optional
Essential

Fig 2.2 General Structure of Java Program

Java Tokens
Java token is the smallest element of a program that is meaning to the computer. When we submit a Java Program to Java compiler, the compiler goes through the text and extracts individual tokens. Java language includes five types of tokens. They are:

* Reserved Keywords
* Identifiers
* Literals
* Operators
* Separators

Keywords
At the time of designing a programming language, some words are reserved to do specific tasks. Such words are known as keywords or reserved words. Java language has reserved 50 words as keywords.

abstract| Switch| Native| Finally| Default|
byte| Throws| Protected| If| Enum|
class| Volatile| Static| Int| Float|
do| Assert| Synchronized| New| Implement|
extends| Case| Transient| Public| Interface|
for| Const| While| Strictfp| Package|
import| Double| Boolean| This| Return|
long| Final| Catch| Try| Super|
Private| Goto| Continue| Break| Throw|
short| Instanceof| Else| char| void|

Identifiers
Identifiers are programmer-designed tokens. They are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program.

Literals
Literal is a constant value written into a program instruction. A literal does not change its value during the execution of a program. There are four types of literals. These are:
* Numeric literals
* Character literals
* String literals
* Boolean literals

Operators
It is symbol 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 basically divide the shape and function of our code.

Name| What it is used|
Parentheses()| Used to enclose parameters in method definition and invocation, also used for defining precedence in expressions, containing expressions for flow control and surrounding cast types.| Braces{}| Used to contain the values of automatically initialized arrays and to define a block of code for classes, methods and local scopes.| Brackets[]| Used to declare array types and for dereferencing array values.| Semicolon ;| Used to separate statements|

Comma ,| Used to separate consecutive identifiers in a variable declaration, also used to chain...
tracking img