Escolar Documentos
Profissional Documentos
Cultura Documentos
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
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello JAVA");
}
}
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
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>;
EX:
Int Marks;
double avg=32.78;
Concatenation Operand
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
Assignment Operators
If a=25 then
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
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;
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;
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
Ex:
class Forloop
{
public static void main(String args[])
{
for(int a=1;a<=10;a++)
{
System.out.println(a);
}
}
}