Showing posts with label COREJAVA. Show all posts
Showing posts with label COREJAVA. Show all posts

Wednesday, 18 May 2016

control statements in java

difference between while loop and dowhile loop example

 do while loop example program:
public class DoTest {

   public static void main(String args[]){
      int x =20;
   do
      {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }

while( x < 20 );
}
}

output :


while loop example program:

public class WhileTest {

   public static void main(String args[]){
      int x =20;
   while( x < 20 )
      {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }


}
}

output:
 


Saturday, 14 May 2016

Exception handling in java


file handling in java

how to create a backup file 
import java.io.*;
class BakFileCreator
{
 
   public static void main(String args[]) throws FileNotFoundException,IOException
    {
    FileInputStream fis=new FileInputStream(args[0]);
  
    FileOutputStream fos=new FileOutputStream(args[0]+".bak");
  
  
   
   int data;
  while((data=fis.read())!=-1)
{
   fos.write(data);
}
System.out.println("backup file is success fully created");

 
}//main
}//class

output

 

file handling in java

Program to copy file content into another file
for example
create two text files
file1.txt content is copied into file2.txt


import java.io.*;
class Filedemo
{
  public static void main(String args[]) throws FileNotFoundException,IOException
  {
   FileInputStream fis=new FileInputStream("file1.txt");
  
    FileOutputStream fos=new FileOutputStream("file2.txt");
  
  
   
   int data;
  while((data=fis.read())!=-1)
{
   fos.write(data);

 System.out.println("successfully file1 is copied into file2");
 

}

 
}//main
}//class

output
 

Saturday, 7 May 2016

how to install and compile ,run java program using EditPlus


how to install and compile ,run java program using eclipse


how to compile and run Java Program using command Prompt


how to set class Path for java using command prompt


how to set class Path for java using environment variables


how to install JDK in your system


Java Basic Concepts


JDK(Java Development Kit):

Java Developer Kit contains tools needed to develop the Java programs, and JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), Appletviewer, etc…
Compiler converts java code into byte code. Java application launcher opens a JRE, loads the class, and invokes its main method.
You need JDK, if at all you want to write your own programs, and to compile them. For running java programs, JRE is sufficient.
JRE is targeted for execution of Java files
i.e. JRE = JVM + Java Packages Classes(like util, math, lang, awt,swing etc)+runtime libraries.
JDK is mainly targeted for java development. I.e. You can create a Java file (with the help of Java packages), compile a Java file and run a java file.

JRE(Java Runtime Environment):
Java Runtime Environment contains JVM, class libraries, and other supporting files. It does not contain any development tools such as compiler, debugger, etc. Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system
The Java Virtual Machine provides a platform-independent way of executing code; That mean compile once in any machine and run it any where(any machine).

JVM(Java Virtual Machine):


The JVM interprets the byte code into the machine code depending upon the underlying operating system and hardware combination. It is responsible for all the things like garbage collection, array bounds checking, etc… Java Virtual Machine provides a platform-independent way of executing code.

What is JIT Compiler:

In Java you have to write and compile a program only once. The Java on any platform will interpret the compiled bytecode into instructions understandable by the particular processor. However java virtual machine handles only one bytecode instruction at a time that makes execution slow. But Using the Java just-in-time compiler at the particular system platform compiles the bytecode into the particular system code. After the code has been recompiled by the JIT compiler, it will usually run more quickly on the computer.

what is Class Loader:

how many memory areas allocated by JVM:

what is platform independent and Platform dependent:

Exception Handling:

what is an Exception
An Exception can be anything which interrupts the normal flow of the program. When an exception occurs program processing gets terminated and doesn’t continue further. In such cases we get a system generated error message. The good thing about exceptions is that they can be handled


How to handling Exception:

To handle Eceptions java suports five keywords
1)throw
2)throws
3)try
4)catch
5)finally

                              Example Program to generate Excepton 
 This Example generate following posible 3 exceptions
1)ArrayIndexOutOfBoundsException------------------------
2)NumberFormatException ---------------------------------
3)ArithmeticException --------------------------------------
class Edivision
{
public static void main(String args[])
{

int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=a/b;
System.out.println("'result"+c);

}

}
output: 

                         Example Program to handling the Exception

To handle Exception by using try and catch block

 class Edivision
{
public static void main(String args[])
{
try
{

int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);

int c=a/b;
System.out.println("'result"+c);

}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("please give two values");
}

catch(NumberFormatException e)
{
System.out.println("please enter only numbers");
}
catch(ArithmeticException e)
{
System.out.println("please do not enter second value is zero");
}


}
}

 output: