Friday 29 July 2011

How to run Java program


A First Simple Program
/*
This is a simple program Call this file “Example.java”.
*/
Class Example
{
//Your program begins with a call to main ().
Public static void main (String args[])
{
System.out.println (“This is a simple Java program “);
}
}


Entering the program
-The name of a source file is very important
-In Java, all code must reside inside a class. By convention ,the name of that class should match the name of the file that holds the program
-A source file use the .java filename extension

Compilling the Program
C:>\javac Example .java
-The javac compiler creates a file called Example .class, that contain the Bytecode version of the program
-To actually run program, you must use the Java interpreter, called java
C:>\java Example
-When java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension
A closer look at the first program
-All Java application begin execution by calling main()
-A complex program will have dozens of classes, only one of which will need to have a main() method to get things started
-Java applets don’t have any main() method
-Java interpreter run only main() method
-Java interpreter run only the class that have a main method
-println() is a built in method
-System is a predefined class that provides access to the system,and out is the output stream that is connected to the console.

Features of java programming language


Compiled and interpreted
Platform – Independent
Object – Oriented
Robust and Secure
Distributed
Multithreaded
Dynamic and Extensible

Compiled and interpreted:-
                Usually a computer language is either compiled or interpreted. Java combines both these approaches thus making Java a two stage system.
First, Java compiler translates sources code into what is known as byte code instruction. Byte codes are not machine instructions and therefore, in the second stage. Java interpreter generates machine code that can be directly executed by the machine that is running the Java program.

Object- Oriented:
               Java is a true object – oriented language. Almost everything in Java is an object. All program code and data reside within object and classes.

Robust and Secure:
               Java is a robust language. It provides many safeguards to ensure reliable code. It has strict compile time and run time checking for data types. It is designed as a garbage – collected language relieving the programmers virtually all memory management problems. Java also incorporates the concept of execution handling which captures series errors and eliminates any risk of crashing the system.
Java system not only verifies all memory access but also ensure that no viruses are communicated with an applet. The absence of pointer in Java ensures that programs cannot gain access to memory locations without proper authorization.

Distributed:
               Java is designed as a distributed language for creating application on network. It has the ability to share both data and programs.

Multithreaded:
               Multithreaded means handling multiple tasks simultaneously. Java supports multithreaded programs. This means that we need to wait for the application to finish one task before beginning another. For example, we can listen to an audio clip while scrolling a page and at the same time down load and applet from a distant computer. This feature greatly improves the interactive performance of graphical applications.

Platform- Independent:
               The most significant contribution of Java over other languages is its portability. Java programs can be easily moved from one computer system to another anywhere and anytime. Changes and upgrades in operating systems, processors and system resources will not force any changes in Java programs. This is the reason why Java has become a popular language for programming on Internet which interconnects different kinds of systems worldwide. We can download a Java applet from a remote computer onto our local system via Internet and execute it locally. This makes the Internet an extension of the user’s basic system providing practically ultimate number of accessible applets and applications.

High Performance:         
               Java performance is impressive for an interpreted language, mainly due to the use of intermediate byte code. According to sun, Java speed is comparable to the native C/C++. Java architecture is also designed to reduce overheads during runtime. Further, the incorporation of multithreaded enhances the overall execution speed of Java programs.

Dynamic and Extensible:
Java is dynamic language; Java is capable of dynamically linking in new class libraries, method and object. Java can also determine the type of class through a query making it possible to either dynamically link or abort the program, depending on the response.
               Java programs support functions written in other language such as C and C++. These functions are known as native methods. This facility enables the programs to use the efficient functions available in these languages. Native methods are linked dynamically at runtime.
              



Thursday 28 July 2011

Program to enter a 5*5 matrix and display it


class matrix
{
Public static void main (String arg [])
{
int two [] [] = new int [5] [5];
{
int i,j,k =0;
for (i= 0; i<5 ; i++) {
for (j=0 ; j<5; j++)
{
Two [i] [j] = k;
k++;
} }
for (i=0; k=5 ;i++)
{
for (j=0; j<5 ; j++)
{
System.out.print( two [i] [i] + “  “ );
}
System.out.println( );
}
}
}

A simple program to arrange the 10 nos. in ascending order using array.


class ASC
{
Public static void main (String arg[])
{
int I,j,k;
int a[] ={10,12,18,16,3};
for (i=1;i<5;i++)
{
for(j=0;j<=3;j++)
{
if(a[j] > = a [ j+ 1])
{
k=a [j];
a[j]=a[j+1]
a[j+1]=k;
}
}
}
for(i=0;i<5;i++)
{
System.out.println (a [i]);
}
}
}

A simple program to find the average of 10 nos. using array class Arg



class Arg
{
Public static void main (string arg [])
{
int no []= { 10 ,12 , 14 ,16 ,18 ,20 ,22, 24 ,26 28);
int result = 0;
for (i=0; i<10;i++)
result =result + no [i];
System.out.println(“Average is “ +result/10);
}
}

Wednesday 27 July 2011

Structure of JAVA program

 A JAVA program may contains many classes of which only one class defines a main method. Classes contain data members and methods that operated on the data members of the class. Methods may contain data type declaration and executed statements. To write a Java program, we first define classes and then put them together. A Java program may contain one or more sections as shown in figure.



Documentation Section:
               The documentation section comprises a set of comment lines giving the name of the program, the author and other details, which the programmer would like to refer to at a later stage.

Package Statement:
               The first statement allowed in a Java file is a package statement. This statement declares a package name and informs the compiler that the classes defined here belong to this package.

Example:
            Package student;          
            The package statement is optional.

Import Statements:
               The next thin after a package statement (but before any class definitions) may be a number of import statement. This is similar to the # include statement in C.

Example:
Import student.test;

Interface Statements:
               An interface is like a class but includes group method declarations. This is also an optional section and is used only when we wish to implement the multiple inheritance features in the program.

Class Definition:
               A Java program may contain multiple class definitions. Classes are the primary and essential elements of a Java program. These classes are used to map the objects of real world problems. The number of classes used depends on the complexity of the problem.

Main Method Class:
               Since every Java stand alone program requires a main method as its starting point, this class is the essential part of a Java program. A simple program may contain only this part. The main method creates objects of various classes and establishes communications between them. On reaching the end of main, the program terminates and the control passes back to the operating system.




Simple program for printing the following chatuskon upto given value and print the patter for “*”



                        *
                                                                                                    *                  *                  *                                                                                                 
                                                                                *                  *                  *                  *                  *
                                                            *                  *                  *                  *                  *                  *                  *
                                        *                  *                  *                  *                  *                  *                  *                  *                  *




class a1

{

Public static void main (String args[])

{

int I,j,k,1;

for(i=1;i<=5;i++)

{

for(1=5;1>=I;1--)

System.out.print(“\t”)

for(j=1:j<=I;j++)

System.out.print(“*\t”);

for(k=i-1;k=1;k--)

System.out.print(“*\t”);

System.out.println();
}
}









simple program for printing the following pyramid using for loop



                                                            1                  2                 1
                                       1                  2                 3                 2                 1
       1                  2                 3                 4                 3                 2                 1
1                 2                 3                 4                 5                 4                 3                 2                 1




class a

{

Public static void main(String args[])

{

int I,j,k,l;
for(i=1; i<=5;i++)

{

for(i=5;1>=I;1--)

System.out.printl(“\t”)

for(j=1;j<=I;j++)

System.out.println(J+”\t”);

for(k=i-1;k=1;k--)

System.out.print(k+”\t”);

System.out.println( );

}

}

Program for Printing the following pyramid



1
2 2
3 3 3
4 4 4 4
n n n ………………n

class series
{

Public static void main (String args[])
{

int i,j,n;
n=50;

for(i=1;i<=50; i++ )

{

for(j=1;j<=1;j++)

{

system.out.print (i);

}

system.out.println (“ “);

}
}
}

Simple program for making Pyramid using only while loop


1
1 2
1 2 3
1 2 3 4
1 2 3 4…..N

class series
{

Public static void main (String args [])

{

int N=10; i=1; j=1;
while( i<N)

{

j=1;
while(J<=i)

{

System.out.println(j);
J++;

}

System.out.println(“”);
i++;

}
}
}








Program to find the greater number from the three nos. using nested if else statement


Classs max
{
             Public static void main (String args[]) 

             {

Int a,b,c;
A=25; b=30; c=10;
if(a>b)

             {

if(a<c)

System.out.println(“a is maximum”);

else

System.out.println(“c is maximum”);

}
else

if(b>c)

System.out.printl(“b is maximum”);

else

System.out.printl(“c is maximum”);
}
               }

}

A simple program for addition of two values:



Class addition
{
Public static void main (String args[])
   {
int a = 5, b=10;
int c= a+ b;

System.out.println(“the sum of two value is :” +c);

    }
}

The Java Runtime Environment



The Java Runtime Environment (JRE) consists of the JVM and the program on the other. The Java Runtime Environment runs code compiled for the JVM by:


Loading the .class files
Verifying byte code
Executing the code

Just in Time Compilers (JIT)



-The Java Virtual Machine includes an optional ‘just-in time’ (JIT) compiler.
-The JIT:

       -Dynamically compilers byte code into executable code
       -Takes byte codes and compilers them into native code for the machine
       -Compiles programs on a methods by method basis just before they are called
       -Is faster than the Java Virtual Machine

Process of compilation

Java virtual Machine



  • All language compilers translate source code into machine code for a specific computer.
  • Java compiler produces an intermediate code known as byte-code for a machine.
  • This machine is called Java Virtual Machine (JVM ) and it exists only inside the computer’s memory.
  • It is simulator of computer wihin the computer and does all major functions of a real computer.                                                                                                                                                                    

Tuesday 26 July 2011

Process of building and running java application programs


Difference Between applet and application


Application
Application can be directly executed by a Java.exe tool
Java Interpreters runs the application
We require JVM to run java application program.

Applet
Applet require web browser for their execution. Their execution is embedded within web pages .
Applet viewer can be used to run an applet.
An applet written by any developer in the world may be dynamically download from a web server and executed on a client PC.


Data Types



 Data types
-Integers
-long (64 bit)
-Int(16 bit)
-short(16,bit)
-byte(8 bit)
-Floating-pointer numbers
-double(64 bit)
-float(32 bit)
-Characters(char, 16 bit)
-Booleans (Boolean, true or false)

Arrays
-Arrays in Java work differently than they do in other languages
-First declare the array
                        Int month_days=new int[12];
-Arrays can be initialized during declaration
-Multidimensional arrays
                        Int twoDp[][]=new int[4][5];
-Alternative array declaration
                        Int [] a2=new int[3];

Java’s Magic: The Bytecode


The secrets behind the security and portability of Java is that the output of a Java computer is not executable code.Rather, it is Bytecode  
Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM)
JVM is an interpreter of Bytecode        
Java systems contain:
-Environment
-Language
-APIs
-Class libraries
Java programs normally undergo five phases
(1)    Edit
-Programmer writes program )and stores program on disk)
(2)    Complete
-Compiler creates bytecodes from program
(3) Load
-Class loader stores bytecodes in memory
(4)Verify
-Verifier ensures bytecodes do not violate security requirements
(5)Execute
-Interpreter translates bytecodes into machine language
Benefits of Bytecode
-Translating a Java program into Bytecode helps makes it much easier to run a program in a wide variety of environments
-Only JVM needs to be implemented for each platform
-Once the run time package exists for a given system, any java program can run on it

      

Twitter Delicious Facebook Digg Stumbleupon Favorites More