Thursday, June 27, 2013

Demystifying the main() function

We all know about the main() function or main() method- the entry point of execution of all the programs of most of the programming languages we use now. Starting from C to C++ to Java and C# and much more main() is utilized in all the programs to make the program work. Even before we came to know about what functions/methods are, we have started using it. With the advent of new languages specially object oriented ones main() has come a long way from int main() (in C) to public static void main(String[] args) (in Java). The function of main() is still same, to serve as entry point for execution, then why a such giant leap in the syntax? Lets find out the answers from the following to demystify main()...


Q1. Can we overload main() function/method?

Ans. The answer is YES as well as NO depending upon which language you are using. More often than not if the main function of a particular language doesn't accept parameters or arguments then it can't be overloaded like in C or C++. If you try to overload main() than the compiler will show you some errors. While in cases where main() accepts parameters usually a String array, it can be overloaded like in pure object oriented languages ( C# and Java ). 

Q2. If main() can be overloaded then how the compiler understand which is the entry point for execution?

Ans. The compiler will consider the main() method with a single argument of string array as entry point for execution i.e static void main(String[] args) will be the entry point for compiler. If you are overloading main() and not using static void main(String[] args) than the compiler wont allow you to run the program in Java. The answer is in context with Java.

Q3. Why the main() has to be static?

Ans. Before answering the question lets revise what the major characteristic of any static method is? Well the obvious answer is that such methods don't need any instance of class when called. Plus they are loaded into the memory even before the class is loaded. Since the main() is entry point for execution, it has to be the first function or method in the memory and if it is not declared static, it will require an instance that will ultimately load the class in the memory before main() is loaded. Which is quite contradictory to the fact that main() has to be the first one utilizing memory since it is the entry point for execution.

Q4. Why main() accepts a String array (String[] args or String args[])?

Ans. The answer lies in the statement that in object oriented languages main() requires some command-line arguments when the program is made to run. These can be passed as an array of Strings. That is why it accepts a String array. The other reason behind this signature of main method  is that whenever main is overloaded, compiler must know which is the default entry point among other main methods and one with single argument of String array is considered as the default entry point for execution by the compiler.

Q5. Why main() method is public in Java?

Ans. Any method which need to be accessed outside the class must be public. main() method in Java has to be public so that it can be accessed by the JVM for the execution.

Q6. Why public constraint of Main() in C# is not necessary as in case of main() in Java?

Ans. Main() has to be public since it needs to be accessed outside the class by the machine which will run the program. In case of C#, even if the Main() is not declared public, .NET runtime is designed in such a way that whenever Main() is invoked, the compiler is able to bypass the access visibility of the Main function.


Note: In C# main() method is written with capital 'M' as Main(), while in most of the other languages, it is written with lowercase m as main().



Wednesday, June 26, 2013

Creating animated GIF images

Creating animated GIF images using Adobe Photoshop


Hello guys. This article will teach you how to create animated GIF images using Adobe Photoshop. Though you can change the format of any image using any Photo Editor to .gif but the resultant will be far away from an animated image. There are also certain tools available to create animated images online but creating your own animated image using your own skill set gives a tremendous satisfaction and a immense pleasure. What i am creating is animated text "Thank You". You can apply similar process to create image slide shows, moving objects and so on. So lets start the digging...

Step-1: Open new document in Photoshop

To start with creating your animated image, open Photoshop. I'm using CS6 though it doesn't matter whichever version you have. The process is  same anyways. Choose new from file in the menu bar. Set the size and other properties. i have kept the background contents as transparent. you can select whatever is appropriate to you.  Click OK.


Step-2: Create layers with text "THANK YOU"

The next step is to create Text using Horizontal type tool or just press 'T' key from the keyboard to start entering text which in my case is THANK YOU on the canvas. Select an appropriate color and font size.Now from the layers Window in the side bar, right click the layer and choose duplicate layer to add another similar layer. Name that layer appropriately. If you cant see Layers panel on your right side bar, simply click windows from the menu bar, check Layers option or just simply press F7 if you are a windows user.

 Now in the new layer double click on the T symbol in front of layer name to select the text. Now change the color from the swatches panel. Repeat the process a couple of times more selecting different color every time. to check individual layer you can turn off visibility of other layers by clicking at the little eye in front of layer name.

Step-3: Add Animation frames

Now you are done with adding different layers, lets get stated with actual animation. Open Animation window by selection Animation from Window residing in menu bar. It will appear something like below. Add duplicate frames equal to the number of layers you have. After that select a single frame from animation window and turn on visibility of just single layer from the layer panel. Similarly select next frame and turn on visibility on just single layer different from previous ones. Repeat for rest of the frames.

Step-4: Finalizing animation with TWEEN

In the next step, select all the frames from animation window then open Tween from pop up menu under animation. 


Now just click OK. the select timing for displaying each frame by clicking '0 sec' below each frame and selecting time for delay. Your animation is ready. Now all you need is to save it as mentioned in next step.


Step-5: Saving the Image

Saving just created file is a little bit different from traditional saving options. To save the image, go to File and select save for web & devices. Just click Save in the pop up window. Voila.... You have created your Animated gif Image. My end product is something like flashing than you below. :)

 

Hope you like and learn (if you didn't knew this). If you are facing any problem, feel free to leave comment or mail me. Keep visiting for new stuff. Enjoy