Friday, August 27, 2010

Executing Java program without main() function

The title of post seems strange but it is fact. You can develop a Java program without main() function.
Try to execute following program.

class MainMethodNot
{
    static
    {
        System.out.println("This java program have run without the run method");
        System.exit(0);
        
    }
}

The reason that this program executes because static block is executed as soon as the class is loaded. However JVM throws an exception because it doesn't find main() method which can be avoided by using System.exit() function.

Immutable strings in Java

Strings are said to be immutable in Java.Immutability refers to ability toprevent change in value.
It means whenever you change the value of String, you create a new object and make that variable refer to new object.Appending a string is also a same kind of operation internally.

Each time you use "+" operator or ( String.concat() ) , a new string is created, old data is copied and new data is appended. Old string will be garbage collected.

When to use "+":

1). Multiline Strings

  
String text=
    "line a\n"+
    "line l\n"+
    "line b";
 
2) Short messages
System.out.println("x:"+x+" y:"+y); 


When you have multiple string blocks to concat use StringBuilder or 
StringBuffer( if multithreading is used ) class.
 

Saturday, August 21, 2010

Program to print 1 to 10 using while loop ( GTU Sem I Program No : 8)

/*Program to print 1 to 10 using while loop 
  Program No         :- 8 of GTU Practice Programs SEM I 
  Developed by       :- Malhar Vora 
  Developed on       :- 21/07/2010 
  Email              :- vbmade2000@gmail.com 
  Web Site           :- http://malhar2010.blogspot.com 
  Development Status :- Completed
**************************************************/  
void main()
{
     int n=1;

     while(n<=10){
 printf("%d\n",n);//Prints n
 n++; //Incrementing n
     }
}

Execute function after completion of main() function in C using #pragma directive

/*This Program demonstrates the use of #pragma directive.#pragma exit can be used to call a function after main.The function must not returning anything other than void and must not contain any arguments.
  Syntax : #pragma exit function-name [priority]
  Here function-name is the name of a function and priority is optional.
  priority can be from 64 to 255.Do not use priority from 0 to 63 because they are used by C libraries.
  100 is a Default priority
  255 is a lowest priority
  Developed on       :- 28/03/2009
  Developed by       :- Malhar Vora
  Email              :- vbmade2000@gmail.com         
  WebSite            :- http://malhar2010.blogspot.com
  Development Status :- Completed
  
*************************************************************/


 #include 

void PrintChar1()
{
   printf("Hello after main1\n");
   getch();
}



#pragma exit PrintChar1


void main()
{

   printf("Hello from Main");

}

Execute function before execution of main() function in C using #pragma directive

/*This Program demonstrates the use of #pragma directive.
  #pragma startup can be used to call a function before main.The function
  must not returning anything other than void and must not contain any
  arguments.
  
  Syntax : #pragma startup function-name [priority]
  Here function-name is the name of a function and priority is optional.
  priority can be from 64 to 255.Do not use priority from 0 to 63 because
  they are used by C libraries.
  100 is a Default priority
  255 is a lowest priority
  Developed on       :- 28/03/2009
  Developed by       :- Malhar Vora 
  Email              :- vbmade2000@gmail.com         
  WebSite            :- http://malhar2010.blogspot.com
  Development Status :- Completed
  
******************************************************************/


 #include 

void PrintChar1()
{
   printf("Hello before main1\n");
}

void PrintChar2()
{

   printf("Hello before main2\n");
}


#pragma startup PrintChar1 64
#pragma startup PrintChar2 65

void main()
{

   printf("Hello from Main");

}

Friday, August 20, 2010

Classsification of programming languages

Classification according to level

#. Low level languages
  • Machine Language
  • Assembly Language

#. High level languages
  • Imperative Language (FORTRAN,COBOL,ALGOL-60,JAVA)
  • Functional Language (F#,Haskel,Scala,Erlang)
  • Logic Language (Prolog)
Classification according to features
  • Sequential(LOGO)
  • Concurrent(Erlang,Eiffel)
  • Modular(Python,Runy)
  • Parallel(Haskel,Erlang)
  • Distributed
  • Object Oriented(JAVA,C#,C++)

Sunday, August 8, 2010

C Program to print square of first and last nos of inputted no

/*Program to print square of first and last no of inputted no
  Created by : Malhar Vora
  Created on : 8-8-2010
  Email          : vbmade2000@gmail.com
  WebSite    :  http://malhar2010.blogspot.com
******************************************************************************************************/
#include

void main()
{
      int i=358,a=0,first=0,last=0,flgfirst=0;

    while(i>0)
   {

       a = i%10;

       if(flgfirst==0)
       {
              last=a;
              flgfirst=1;
       }

       first=a;

       i=i/10;
   }

   printf("Square of First no : %d",(first*first));
   printf("\nSquare of Second no : %d",(last*last));

}