Purpose of Garbage collection & Identifier in Java

Identifiers 
Identifiers in Java are used for naming a variable, method, class or label. I will cover each of these topics in detail, in my later posts. For the time being just concentrate on the word identifier.

Identifiers are required by Java, so that you may give your variable a name. Though as seen above it is also used for naming method, class and label. There are some rules that need to be followed, while naming an identifier.

The rules for naming an identifier are simple and must be practiced thoroughly, if you don't want your JVM to shout that it cant compile the class. The rules for naming an identifier are:-

1. An identifier in Java can start with a letter.
2. An identifier in Java can start with a dollar sign($)
3. An identifier in Java can start with an underscore(_) 
4. Subsequent characters in the identifier after the first character can be letters, dollar sign, underscore, or a digit.

Any other rule for identifiers is not there, so just stick to these basic four rules and you won't face any problem in compilation of your code.

I will take a few examples so as to make the situation more clear to you.

companyName //legal
BigClass //legal: you may embed the keywords in between
$money //legal
8_node //illegal(Cannot start with a digit)
!node //illegal(special characters not allowed in starting except "$" and "_")

Now you would be able to clearly distinguish, which one is a valid identifier and which one is not. Having good understating of these basic concepts of Java help make the life of the developer easy, as he faces less errors. The life of a developer is already very hard, faced with coding requirements, meeting deadline, and debugging bugs.


Purpose of garbage collection in Java

The garbage collector is one of the best things in Java. In Java every Object lives on a heap. Heap is the play area of Objects. Whenever we create Objects, some area on the heap is allocated to this new Object. But what will happen, when this Object is of no use. 

I mean to say that, when the Object refers nothing, then it consumes unnecessary space on the heap, which must be freed. This space could be used by other Objects, and also helps the JVM in speeding up the operations of processing.

This is where the garbage collector comes into picture. The Garbage collector in Java, checks at regular intervals about the Objects that are no more referenced by any variable. These Objects are eligible for Garbage collection. Garbage Collector checks for the Objects that are unreachable for the program, and so are consuming unnecessary space, so Garbage Collector considers these Objects as Garbage, and so it collects them. There by freeing space on the heap.

For example, if I say that:-

Object a = new Object();

This line of code allots space on the heap for the new Object. But suppose that later in the code, we say that:-

a = null;

This statement will cause the reference of this Object to refer null, and so the Object is lost in the heap and is now unreachable. This Object is now eligible for Garbage collection, so that space could be freed on the heap. This space helps the JVM in performing better and faster.