Monday, 14 January 2013

Why Public Static Void Main ? - Java Code

We are so much habitual now to begin our code with public static void main(). Because we were taught to do so. Being taught that this is the entry point for our java program to execute, however are we aware what does it actually means??
Plenty of other questions which came into my mind:
1. Why public static void main()
2. Whats the meaning of each one of the individual keyword?
3. What if I don't use it?
4. Is there any other way to start the program execution?
5. How does JVM knows, "i need to start from here..."?
6. Can we shuffle the keywords?
  and so on....

Well answering to each one of the above, lets begin.

We structure our complete code into classes and objects(instance of those classes), that's what java says to do. Now we write a program say xyz, create a public class for it XYZ and define a public static void main(String args[]) method in the class.

The first thing to know is public static void main () aka psvm() is a method signature with an Array of String type as an argument. The method signature is
[access modifier] [return type] method_name (Argument_List)

Secondly, its a must to use this signature because JVM looks for this signature to begin execution. If it is not able to find the above signature an exception is thrown. Note this is the only way to begin the program execution and nothing else would work.

public is an access modifier, which tells compiler that any class or object can access this method.
or simply, Access is granted to anybody. Making the main method public is necessary so that anyone (object) can access the method.

static.. Please remember in java we need an object reference to call any class method. I mean instantiating the class. Now do we have any object before beginning the program. No we don't. So how we are going to begin with. Static keyword before a field or method describes that no instance or object reference is required to access that field/mehtod().

void.. Simply means no return by the method
Note : there's a difference between null returned and void.

main.. As we all know, method name defined by java.
main is different from Main as java is case sensitive. Further main() is pre-defined mehtod where as you can create your own method Main() / Main(x) or anything.

Friday, 4 January 2013

Thursday, 3 January 2013

JavaScript runtime calculator

Close Browser Window (Tab) : JavaScript

Closing the opened tab through javaScript

Javascript:
run this

window.opener='x';
window.close();

Above example would ask for user confirmation.
Do you want to close or not ??? type


Incase you dont want confirmation from user... and force closing
Try this:

window.open('', '_self', '');
alert('Window is closing');
window.close();

Thursday, 22 November 2012

String Memory, String Pool



String memory

If you use the new keyword, a new String object will be created. Note that objects are always on the heap - the string pool is not a separate memory area that is separate from the heap.

The string pool is like a cache. If you do this:

String s = "abc";
String p = "abc";
 
then the Java compiler is smart enough to make just one String object, and s and p will both be referring to that same String object. If you do this:

String s = new String("abc");
 
then there will be one String object in the pool, the one that represents the literal "abc", and there will be a separate String object, not in the pool, that contains a copy of the content of the pooled object. Since String is immutable in Java, you're not gaining anything by doing this; calling new String("literal") never makes sense in Java and is unnecessarily inefficient.

Note that you can call intern() on a String object. This will put the String object in the pool if it is not already there, and return the reference to the pooled string. (If it was already in the pool, it just returns a reference to the object that was already there). See the API documentation for that method for more info.



 in java for String there is a special consideration, if you are creating a String class object like
String s="jai"
than this will go to string pool and if are again want the same string like
String s2="jai"
than no other object of String class is created now reference variable s2 will refer to "jai"
this is done because String objects are immutable.
and you can check it by == test like
if(s==s2)
this will give the true value and execute if condition.


means when you do any modification on them than another object of String class is created and the reference of that class is gone.like
String s="jai"
s=s.concat(" hai");
now is referring to jai hai and reference to jai string is gone and this object is stored is sting pool. for further use.
like in future we need that object than the new object is not created and by JVM the reference of that object is given to that reference variable.

and if you are creating a object by new operator than always new object is created .
e.g.
String s2="jai".
String s=new String ("jai");
 and now you are doing the == test than this will give false and if condition will not execute.
if(s2==s)
{
//code
}
this will not execute.