Java Tutorial 1

Java Tutorial 1

Java Tutorial 1

(Java Environment Setup and Write First Java Program)

> Download Java (JDK) Software and Install

> set Environment Variable (Path Variable)
(* If want to execute Java programs from any directory in C Drive)

How to set Path Environment Variable:
OS: Windows 7

> Select MYComputer and Right click

> Properties

> Advanced System Settings

> Environment Variables

> Select “New” in System variables

> Enter Variable as “Path”

> Enter JDK bin directory path (“C:\Program Files\Java\jdk1.8.0_45\bin”) in value field

> OK > OK > Ok
—————————————-

Verify the Environment setup:

> Launch Command Prompt

> Type java (It provides details)
————————————-
Three steps to write and execute Java Programs

i) Write Java Program in Notepad (Editor)

ii) Compile the Program (It creates Class file)

Command prompt > Change to Program file Directory

> Type javac Programname.java (java is extension of Java program, javac command for compilation)

iii) Run /Execute the Java Program

> Type java programName
(It provides Output on the console)
—————————————————–
Use Eclipse IDE to write and execute Java programs.
Eclipse IDE (Integrated Development Environment)

It is a platform to write and execute Java programs, Perl, Python, PHP, Ruby etc…

It provides Editor, context help, help for syntax errors, Auto compilation and debugging.
—————————–
Download Eclipse software and extract.

Steps:
—————-
> Create Java Project
-> Create a Package
-> Create a Class / Interface
(We can write java code in the Class file)
————————————–

Ex:
public class Sample {
public static void main (String [] args){
System.out.println(“Hello Java and Selenium”);
}
}
————————————————-

Java Program structure or Java Syntax:

1) Package declaration statement
ex:

package abcd;
——————————–
2) Import statements
we can import Built in and User defined Libraries using import keyword.
(In order to use Existing Classes, Objects, and Methods)

Example:

import java.io.console;

(It Imports Console Class only from io package)

import java.io.*;

(It imports all classes from io package)
——————
java – project

io – package

Console – Class
———————————————-
3) Class declaration statement
ex:

public class Sample {
}

public – Access Modifier

class – Java keyword to declare a class

Sample – Name of the Class

Note: First letter of Class name should be upper case.
————————————————-
4) Comments section
// This is a Sample Java Program
———————————–
5) Main Method
(Java program execution starts from main method.)
(Main method is mandatory 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
—————————–
Normal statements

Conditional blocks

Loop blocks

Method declarations

Etc…
—————————–
Sample Java Program:
package abcd;

import java.io.Console;
import java.lang.*;

public class Sample2 {
// This is a Sample Java Program
public static void main (String [] args){
int a = 50, b, c; // Declaration of Variables
b= 20; // Initialization
System.out.println(a + b); // 70
System.out.println(“Addition of a, b is: “+ (a+b));
final int MONEY = 100; // Declaration of Constant

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

for (c = 1; c <= 10; c++){
System.out.println(c);
}
}
}

Follow me on social media: