Java Environment Setup, Java Syntax Rules and Java Program Structure

preview_player
Показать описание
Download and Install Java, Download and Extract Eclipse IDE, Set Environment Variable path, Write & execute a Java program using Command prompt.
Configure Eclipse IDE, Write and execute a Sample Java program using Eclipse.
Рекомендации по теме
Комментарии
Автор

Java Class Notes:
Java Tutorial 1: Java Environment Setup, Java Program Structure

i) Java Environment Setup and write first Java Program

ii) Java Syntax

iii) Java Program Structure

i) Java Environment Setup and write first Java Program

Steps:

1) Download Java (jdk) Software and Install
2) Set Environment variable path (Path Variable)
3) Download Eclipse IDE and Extract

Download Java from either java.com or oracle.com, and Install,
After Installation you can get Java folder in C:\program files\

Set Environment Variable Path

Navigation:
> Select MyComputer and right click
> Properties
> Advanced System Settings
> Environment Variables
> Select "Path" Variable from System Variables
> Edit
> Copy Java bin directory path and paste in variable value field
> OK > OK >OK > Close

1) Write and Execute a Sample Java program from Command prompt
2) Write and Execute a Sample Java program using Eclipse IDE

1) Write and Execute a Sample Java program from Command prompt

Step 1: Write a Program in any Editor (Ex: Notepad)

Step 2: Compile the Program

Step 3: Run / Execute the Program

Step 1: Write a Program in any Editor

public class Sample {
public static void main (String [] args) {
System.out.println("Hello Selenium World");
}
}

Note: Save with java extension

Step 2: Compile the Program

> Launch Command prompt
> Change to Java program file directory
> Type javac ProgramName.java

Note: It creates Java Class file

Step 3: Run / Execute the Program

Launch Command prompt
> Type java ProgramFileName

It displays Output on the Console

2) Write and Execute a Sample Java program using Eclipse IDE

About Eclipse IDE:

Eclipse Integrated Development Environment

> Eclipse IDE is an Open Source Software
> Eclipse IDE is platform to write and execute Computer programs like Java, Perl, Python, Ruby, PHP etc...
> It provides Editor for writing programs, syntax guidance, Context Help, Auto Compilation etc...

Download Eclipse IDE and Extract

Steps to write and execute Java programs

> Launch Eclipse IDE
> Create Java Project
> Create Java Package under Java Project
> Create Java Class / Java Interface under Java Package

Java Project
Java Package
Java Class / Java Interface

Sample Java Program:
package abcd;
public class Sample {
public static void main(String[] args) {
System.out.println("Hello Selenium World");
System.out.println("Hello Java World");
}
}

ii) Java Syntax

1) Java is a Case sensitive Language.

2) First letter of Java Class name should be in Upper Case.

Sample 'Correct
sample 'Incorrect
Sample2 'Correct
Javaexamples 'Correct
JavaExample 'Correct

3) Java Method names should start with Lower case value

4) Java Program File name should exactly match with Class name.

5) Java Program execution starts from main method, which is mandatory in every Java program

6) Every Statement / Step should end with Semi colon (;)

7) Code blocks (Ex: Conditions, Loops, Methods etc...) enclosed with {}

iii) Java Program Structure

1) Documentation Section
(Write about the Program....)

2) Package declaration statement

ex: package abcd;

3) Import Statements

We import Built in and User defined libraries using import keyword

ex:
import java.util.Scanner;
import java.util.*; - Imports all Classes from util package

import - It is a Java Keyword

java - Project
util - Package
Scanner - Class

4) Class Declaration statement

Ex:

public class Sample{
}

public - Access Modifier

class - Java Keyword / Reserved word to create Java Class

Sample - Class name

User defined Method Declarations

5) main Method

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

public - Access Modifier
static - Non Access Modifier (use main method without invoking Object)
void - Returns Nothing
main - Method Name
String [] args - is the Parameter to the main method

6) Declarations

We declare Variables and Constants

int a;
a=10;
.
.
.
a =20;
.

final int b =10;
.
.
b = 20; //Syntax Error

7) Normal Statements

d = a+b;
system.out.println(d);

Method Calls

8) Code Blocks

Condition Blocks
Method Calls
Loop Blocks
etc...

Note:
Every normal statement should end with semi colon (;)
Every code block should encolsed with {}

A Sample Java program

// It is a Sample Program to Understand Java Program Structure and Syntax

package abcd;

public class JavaSample {

public static void main(String[] args) {
int a, b;
a =100; b=20;
System.out.println("Addition of a, b is: "+ (a+b));

if (a > b) {
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}

for (int i=1; i<=5; i++){
System.out.println(i);
}

}
}

gcreddy
welcome to shbcf.ru