Java Syntax

Java Program Structure

Java Program Structure

Java program structure / Java Syntax

> Download Java Software (JDK) and Install

> Set Environment (Path) Variable to access Java Run -time Environment from any directory in C drive.

> Download Eclipse software

C:\Program Files\Java\jdk1.8.0_40\bin – Java path

C:\Users\G C Reddy\Desktop\eclipse -Eclipse

How Eclipse communicates with Java Run-time Environment

Using Path Environment variable

Java Programming

Java Project
-> Packages
-> Classes, Interfaces etc…

> Create Java Project in Eclipse

> Create a package

> Create a Class

Sample Java Program

1) Package Declaration statement
Ex:
package abcd;
———————-
2) Import Statements
We can import built in and User defined Libraries using import keyword.

Examples for Built in packages

io, jxl, sql etc…

import java.io.Console;

import java.lang.*;

import is java keyword to import Libraries

java – Project

io – package

Console – Class

lang.* – all classes from lang package
—————————–
3) Class declaration statement
Ex:
public class Sample {

public – Access Modifier

class – java keyword to declare a class

Sample – Name of the class
———————————
4) Comments section
// This is a Sample Java Program

5) main method statement – Program execution starts from main method
(* It is the mandatory statement in every Java program)

public static void main (String [] args){

public – Access Modifier

static – use main method without invoking any object

void – returns nothing

main method name

6) Declarations
We can declare variables and constants
———————
System.out.println (“Addition of a, b, c is: “+ (a+b+c));

System – Class

out – Object

println – method

“Addition of a, b, c is: ” – Message / Details

+ – Concatenation

a, b, c, d -Variables

Conditional statements
Loop statements
Built in and user defined methodsOperators etc….
——————————
Writing Normal statements – every statement should end with ;

Writing blocks – Statements of the block enclosed {}

Java Program Example:

package abcd;

import java.io.Console;
import java.lang.*;
public class Sample {
// This is a Sample Java Program
public static void main (String [] args){
int a = 10, b, c; // Declartion of Variables
b= 20; // Initialization
System.out.println (a+b);
a = 50;
final int MONEY = 100; // declaring constant
// MONEY = 200;
System.out.println (“Addition of a, b, is: “+ (a+b));

if (b > a){
System.out.println (“B is a Big Number”);
}
else
{
System.out.println (“A is a Big Number”);
}
for (c=1; c <= 10; c++){
System.out.println (c);
}
}
}
——————————————–
Java Comments
Comments are English word, can be used for code documentation.

Purpose of Comments:
a) To make the code Readable

b) To make the code disable from execution

Comments Syntax in Java:
Use // for Single line comment

Use /*….
……
…..
*/ for multiple lines comment
————–
Ex:

int a = 10, b, c; // Declaration of Variables

// public static void main (String [] args){

———-
/* if (b > a){
System.out.println (“B is a Big Number”);
}
else
{
System.out.println (“A is a Big Number”);
}
*/

Usage of Comments in Test Automation:
a) To write Test headers

b) To write method headers

c) To explain complex logic

d) To explain resource usage
Etc…

Java Data Types

What is Data Type?

Data type is a classification of the type of data that a variable or object can hold in computer programming.

Ex: character, integer, float etc….

> Java supports explicit declaration of Data types.
(we need to specify the data type before declaring the Variable)

Syntax:data_type variable_name;

data_type variable_name = value;

Example:int a;

int a = 100;
———————-
Two types of Data types in java
a) Primitive Data types

b) Non-primitive Data types / Reference Data types
———————————–
a) Primitive Data types8 primitive data types

Integer types
1) byte (8 bits)

Ex:

byte a =100;

2) short (16 bits)

short a = 10000;

3) int (32 bits)

int a = 100000;

4) long (64 bits)

long a = 100000L
——————
Rational types (Numbers with decimal places)
5) float (32 bits)

float a = 1.2;

6) double (64 bits)
ex:
double a = 19.3456;
———————
Characters
7) char (16 bits)

Ex:

char a =’Z’
————————
Conditional
8) boolean (undefined, depends on JVM)

Ex:
boolean a = true
———————————
b) Non-primitive / Reference data types
Non-primitive data types in Java are Objects and Arrays.

Note:

Reference types are not passed value, passed by reference

Ex:

Button a = new Button(“OK”)

Java Variables

1) What is Variable?
A named memory location to store or hold the data.

Two types of memory in Computer environment:

i) Primary memory -RAM

ii) Secondary memory -ROM
Ex: CD, DVD, HDD etc…
————————-
2) Declaration of Variables
Java Supports explicit declaration of variables

Syntax:

dataType VariableName;

Ex:

int a;
—————
dataType variable1, variable2, variable3;

Ex:

int a, b, c;
————————
dataType VariableName = value;

Ex:

int a =100;
————————–
3) Assigning values to variables
a) Initialization

int a;
a=10; // Initialization
————–
int a=10; // Initialization
—————-
b) Reading
using Input devices
from files(text/excel)
from databases
from Application objects
————————-
4) Types of Variables
a) Instance Variable
A variable that is declared inside the class but outside the method

b) Local variable
A variable that is declared inside the method

c) Static variables
A Variable that is declared as static, It cannot be local.
———————-
5) Naming Conventions
> Java Variables are case sensitive, monkey is not the as Monkey or MONKEY.

char monkey, Monkey, MONKEY

> Java variable name must start with a letter or $ or _

Ex:
myvar
MYVAR
$myvar
_myvar
myvar1
myvar_1
———-
1myvar – invalid
———————-
> Variable names cannot be equal to java reserved words

Ex:

int
for
import

> Must be unique in the scope of declaration.
—————————————-

(31)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *