Programming in Java

First Program in Java

class First
   {
      public static void main(String args[ ])
         {
            System.out.println("This is my Java Program");
  }
   }

In linux using text editor

Steps : 

  • open terminal 
  • vi First.java 
  • type the "first" program, Save and Exit. 
  • javac First.java 
  • set CLASSPATH=$CLASSPATH:. 
  • java First 

In Windows using text editor 

Steps : 

  • run->cmd
  • edit First.java 
  • type the "First" program, Save and Exit. 
  • javac First.java 
  • set CLASSPATH=%CLASSPATH%;. 
  • java First 


In eclipse IDE 

Steps : 

  • Locate Eclipse Installation 
  • Launch eclipse.exe 
  • Create new Java Project 
  • Add new class 
  • Class name 
  • Enter the remaining program 
  • Run as java program 

Coding Styles and Conventions

  • Class name is created with first letter capital. For example First
  • Class must be defined in the file name with same name. For example Class First is defined in First.java
  • Use of comments are advised. Block Comment use: /* This is commented text block */
  • Use of Punctuations. (see Punctuation on wikipedia for more details)
    • in Signature as input arguments : parentheses or round brackets:  ( )
    • in Array: box brackets or square brackets:  [ ]
    • in Scope of Class or method: curly brackets or braces:  { }
  • f
  • f

Program Template

Data Types 

Integers

Explanation

Java support four integer data types: byte, int, long and short

Use of integers

// This is Second Program handling Integers in Java 

class MyIntegerTest{
    public static void main(String args[ ]){
            int num1, num2, result;
            num1=16;
            System.out.println(" Number 1 = "+num1);
            num2=8;
            System.out.println(" Number 2 = "+num2);
            result=num1+num2;
            System.out.println(" Addition = "+result);
            result=num1-num2;
            System.out.println(" Subtraction = "+result);
            result=num1*num2;
            System.out.println(" Multiplication = "+result);
            result=num1/num2;
            System.out.println(" Division = "+result);
        }
}

Characters

Explanation

Use of integers

 

Boolean

Explanation

Use of integers

Floating-point Numbers

Explanation

Use of integers

Operators

Arithmetic Operators 

Use of Arithmetic operators

Relational 

Use of Arithmetic operators

Boolean 

Use of Arithmetic operators

Assignment 

Use of Arithmetic operators

Arrays 

One Dimensional Array

Syntax:     type var-name[ ];

                        array-var = new type[size];

Using one dimensional array

// Program showing One dimensional Arry
class MyArrayTest{
    public static void main(String args[]){
        String weekdays[];
        weekdays= new String[7];
        weekdays[0]="sun";
        weekdays[1]="mon";
        weekdays[2]="tue";
        weekdays[3]="wed";
        weekdays[4]="thu";
        weekdays[5]="fri";
        weekdays[6]="sat";
        System.out.println("week day 1 is " +weekdays[0]);
        System.out.println("week day 2 is " +weekdays[1]);
        System.out.println("week day 3 is " +weekdays[2]);
        System.out.println("week day 4 is " +weekdays[3]);
        System.out.println("week day 5 is " +weekdays[4]);
        System.out.println("week day 6 is " +weekdays[5]);
        System.out.println("week day 7 is " +weekdays[6]);
     }
}

 

Selection Statements 

If Statement

Signature : if(condition) statement;

Operator         Meaning

<                     Less than

>                     Greater than

= =                  Equal to

 

Using if Statement

// This is a program using If Statement
// ****** if(condition) statement;***** 
class MyIfConditionTest{
    public static void main(String args[ ]){
        int num1, num2;
        num1=16;
        System.out.println(" Number 1 is "+num1);
        num2=8;
        System.out.println(" Number 2 is "+num2);
        if(num1>num2)System.out.println(" Number 1 is Greater than Number 2");
        }
}

Switch

Iteration Statement 

for Loop

    Signature: for(initialization; condition; iteration) statement;

Using for Loop

// ***********This is a program using If Statement****************
// ****** for(initialization; condition; iteration) statement;***** 
class MyForLoopTest{
public static void main(String args[]){
int num1;
for (num1=0;num1<10;num1=num1+1)
System.out.println("Value of Number 1 is now:" + num1);
}
}

while Loop

    Signature: for(initialization; condition; iteration) statement;

Using For Loop

do-while Loop

    Signature: for(initialization; condition; iteration) statement;

Using For Loop

 

 

Last modified: Wednesday, 10 July 2013, 3:09 PM