You are on page 1of 12

JAVA Programming

Java Programming can be writing as followings

1. Console Applications Run Through MSDOS


2. Applets Runs Through Web Component (HTML)

JAVA Code Syntax

class <ClassName>
{
public static void main(String args[])
{
Statements;
}
}

JAVA EDITORS

Text Editors
TextPad
Notepad

Scientific Editors
Eclipse
JDeveloper
Visual Studio

Open Notepad

1. Start
2. All Programs
3. Notepad++
4. Notepad++

Or

1. Start
2. Run
3. Type Notepad++ and press Enter

Setting JAVA Path


1. Install JAVA jdk (JAVA Development KIT) file
2. Open JAVA JDK Bin folder
3. Copy the path available in the Address bar
4. Right Click on My Computer and Select Properties
5. Select Advanced Tab
6. Click on Environment Variables button
7. Click on New Button Under User Variable Section
8. Type PATH as variable Name and paste the copied path as
Variable Value
9. Click ok three times
10. Open Command Prompt
11. Type javac/java and press ENTER for verification

First JAVA Program

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

Save JAVA File

1. File Menu
2. Select Save / Save As
3. Select the Location to place the JAVA file
4. Type the ClassName with the extension of .java
5. Click Save

Compile JAVA Program

1. Open MSDOS Window


2. Move to the location where the JAVA Files are saved using DOS
Commands
3. Type JAVAC <ClassName>.java
Ex: D:\Java>javac mySample.java and press Enter

Execute JAVA Compiled File


1. Type java <ClassName>
Ex: D:\java>java HelloWorld and press Enter
Comments

Comments means avoid executing the statements.

There are two types of comments

1. Single line Comment (//)


2. Multiline Comment (/*.*/)

Ex:
/*
This is the first java sample
the code is execute "Hello JAVA"
*/
//Define the class name
class HelloJava
{
//define the main method of the class
public static void main(String args[])
{
//print the statement on the dos prompt
System.out.println("Hello JAVA");
}
}

Println Vs Print

Println means type the text on the Dos Prompt and move the cursor to
the next line.
Print means type the text on the Dos Prompt and keep the cursor on
the same line.

Ex:

class Lineact
{
public static void main(String args[])
{
System.out.println("Hello JAVA");
System.out.print("Hello JAVA");
System.out.println("Hello JAVA");
}
}

Output

Hello JAVA
Hello JAVAHello JAVA

Variables

Variable
Variable is a memory allocation that can be change during program
execution.

Variable Declaration

Syntax
<data Type> <Varibalename> = <Value>;

Primitive Data types

There are 4 types of data type groups.


1. Text Type
a. String
b. Char 1 characters
2. Integer Type
a. Byte - 255
b. Short - 65536
c. Int -
d. Long
3. Real type (Decimal Numbers)
a. Float
b. Double
4. Boolean Type

Rules for Naming a Variable

1. Starting with a Character


2. It can be content Alphanumeric and underscore Mark ( _ ) only
3. No special punctuations available in Any variable name
(@.,:,;,!....)
4. No keywords can apply
5. Maximum length is 40 characters

EX:

Int Marks;
double avg=32.78;

Concatenation Operand

Mixing different types of values by using a Symbol called plus ( + )


symbol.

Ex:
My Name is + <Variabel Name >
Ex:
class ExName
{
public static void main(String args[])
{
String Fname = "Samantha";
String Lname ="Weickrama";
String Full;

Full=Fname+" "+Lname;

System.out.println(Full);
}
}

Operators
There are 4 types of operators
1. Arithmetic
2. Assignment
3. Logical
4. Comparison

Arithmetic Operators

If a=25 b=10 then

Operator Symbol Example Output


Addition + a+b 35
Subtraction - a-b 15
Multiplication * a*b 250
Division / a/b 2
Modulus % a%b 5

Assignment Operators

If a=25 then

Operator Symbol Example Output


= Assign to a=25 25
+= Add and Equal to a+=10 35
-= Subtract and Equal to a-=10 15
*= Multiply and Equal to a*=10 250
/= divide and Equal to a/=10 2
%= modulus and Equal to a%=10 5
++ Increment by 1 b=a++ b=25
a=26
b=++a b=26
a=26
-- Decrement by 1 b=a-- b=25
a=24
b=--a b=24
a=24

Comparison Operators

If a=25 then
Operator Symbol Example Output
== Equal to a==25 true
< Less than a<10 false
> Grater Than a>10 true
<= Less than or Equal to a<=10 false
>= Grater than or Equal to a>=10 true
!= Not Equal to a!=10 true

Logical Operators

A=10, B=15

Operator Symbol Example Output


&& AND A>8 && B<30 true
|| OR A>8 || B>20 true
! NOT !a>10 true

Selection statements

Control Construction

If Statement

Single if Statement

Syntax
If(condition)
{
Stetment if true;
}
else
{
Statement if False;
}

Ex:
class IfStat
{
public static void main(String args[])
{
double avg=56.78;
String grade;

if (avg >= 50)


{
grade = "Pass";
}
else
{
grade = "Fail";
}
System.out.println("Student Graded as "+ + grade);
}
}

Nested if Statement

Syntax

If (condition)
{
Statement if True;
}
Else if(condition)
{
Statement if True;
}
.
.
.
.
Else if(condition)
{
Statement if True;
}
Else
{
Statement if False;
}

Ex:
class NestedIf
{
public static void main(String args[])
{
double avg=56.78;
String grade;

if (avg >= 75)


{
grade = "A";
}
else if (avg >= 60)
{
grade = "B";
}
else if (avg > 45)
{
grade = "C";
}
else if (avg > 30)
{
grade = "S";
}
else
{
grade = "W";
}

System.out.println("Student Graded as "+grade);


}
}

Switch Statement

Syntax

switch(Variable)
{
case <value>:Stetement;break;
case <value>:Stetement;break;
case <value>:Stetement;break;
.
.
Default:statement;break;
}

Ex:
class SwitchStat
{
public static void main(String args[])
{
char grade='D';
String stat;

switch(grade)
{
case 'A':stat="Very Good Pass";break;
case 'B':stat="Good Pass";break;
case 'C':stat="Credit Pass";break;
case 'S':stat="Simple Pass";break;
case 'W':stat="Weak Pass";break;
default:stat="Invalid Grade Character";break;
}
System.out.println(stat);
}
}

Repetition Statement

Repetition is the action that can perform till the given Condition is true.
There are mainly three repetitions activities in JAVA
1. For Loop
2. While Loop
3. Do While loop

While Loop

Syntax

Initialization;
while(condition)
{
Statement;
Update (Increment / Decrement);
}

Ex:

class Whileloop
{
public static void main(String args[])
{
int a=1;

while(a<=10)
{
System.out.println(a);
a=a+1; //a++
}
}
}

DoWhile

Syntax

Initialization;
do
{
Statement;
Update (Increment / Decrement);
}
while(condition);

Ex:

class Dowhile
{
public static void main(String args[])
{
int a=1;

do
{
System.out.println(a);
a=a+1; //a++
}
while(a<=10);
}
}

For Loop

Syntax

For(initialization; Condition; update)


{
Statement;
}

Ex:

class Forloop
{
public static void main(String args[])
{
for(int a=1;a<=10;a++)
{
System.out.println(a);
}
}
}