Wednesday, July 23, 2014

What is difference between Typecasting and Coercion in Java

In computer science, typecasting, and coercion are different ways of changing an entity of one data type into another either implicitly or explicitly. This is done to take advantage of certain features of type hierarchies or type representations.
The process by which a compiler automatically converts a value of one type into a value of another type is called as 
Coercion.
Let us see, how coercion and casting are different in following example.
Coercion (implicit):
double d = 3.0 ;
int i =1 ;
if (d > i) d = i;
Cast (explicit):
double da = 3.3;
double db = 3.3;
double dc = 3.4;
int result = (int)da + (int)db + (int)dc;
 //result == 9

integer i is getting converted into type double implicitly. Implicit conversion from int to double is possible since size of int(4 Byte) is less than the size of double(8 Byte). Whereas conversion of double to int is not an implicit conversion rather its explicit conversion since size of double is larger than size of int.
compiler does implicit conversion only when there is no precision loss, for example
int j = da // double (da=3.3) can not be converted into int implicitly since after conversion there is loss of precision by 0.3.

Saturday, July 19, 2014

How to Update Packages in Ubuntu

The apt-get is an important command which works with Advavced Packaging Tool(APT) for updating package list,upgrading package list,installing new software etc.

The packaging system maintains one database which contains information of which packages are installed,which are not installed and which are available to install.The apt-get uses this database to install the various packages.

Update the list of available packages       :  sudo apt-get update
Install any particular package                  :  sudo apt-get install packagename
Reinstall any particular package              :  sudo apt-get --reinstall install packagename
Remove any particular package              :  sudo apt-get remove packagename

Upgrade the packages                              :  sudo apt-get -u upgrade
Upgrade any particular package              :  sudo apt-get -u install packagename

Search any particular package                 :  sudo apt-cache search packagename

SU Authentication Failure

The fresh installation does not assign root/super user password, so when you run su command first time it will show the message authentication failure. so you need to set super user password by running the following command.
$ sudo passwd
then it will ask for new password and then confirm password.

after this u can run su command.

Friday, July 18, 2014

What is Garbage Collection in Java ?

The Garbage collection is automatic memory management mechanism provided by java in order to reclaim memory occupied by ideal objects or objects which are no longer used in our program. Garbage collector frees programmer from manually dealing with memory de-allocation. The Garbage Collector is a demon thread which starts running automatically as soon as we execute program. So java program in execution, consists of two threads main thread and garbage collector thread. Garbage collection internally uses finalize() method of object which gives a chance to reclaim any type of resources hold by the object. We can manually call garbage collection using System.gc() but since we do not know when the object needs to be reclaimed and when the object is not in use. Manually calling System.gc() is just a hint to JVM that garbage collection is needed.

Generally Garbage collection internally runs mark and sweep algorithm which is two phase procedure.

Phase 1: Mark
The first phase in garbage collection is marking. GC will mark all the objects as alive, ideal and not referenced. It will internally create the graph of alive and ideal objects.
All the alive objects are directly reachable from the root objects. As long as object is referenced, it is marked as alive. The objects those are referenced but are not used for long time are marked as ideal and the remaining objects are marked as not referenced.

Phase 2: Sweep/Compact
In this phase, GC actually starts reclaiming the objects memory. First it will reclaim the memory of all the objects which are not referenced. Now if any class wants to create new object and if sufficient memory is not available in that case GC will look for ideal objects and will reclaim their memory. If the objects are ideal and sufficient memory is available for creating more objects then garbage collector will not reclaim ideal objects memory.

Here GC moves all the alive objects to the bottom of heap and leaves free space at the top of heap. As objects are moved, the pointers to these objects become invalid, so GC corrects these pointers and helps to solve memory fragmentation problem.

Why file name is same as class name containing main method in java ?

Whenever any member is declared as static, it can be used before any of its object is created, just by using class name. Hence static members are used just by using class name. When JVM calls main(), it does not have any instance of class having main() method. So this main() method is called as classname.main().

JVM needs classname to call main() method. How does JVM know what is your classname ?. When java program is compiled as javac filename.java, (filename is same as class name containing main() method) JVM knows your classname using which it calls main().

So file name is same as class name containing main method in java.

Thursday, July 17, 2014

Why Java Is Platform Independent

Getting output from java program is two step procedure, compilation and interpretation. Output of java compiler is not executable code but it is compiled code. JVM is an interpreter for byte code. Byte code is set of instructions which are to be executed by java run time system called as Java Virtual Machine (JVM). Each JVM has its own executable format so this byte code (.class file) becomes platform independent as this byte code runs on JVM and not on actual OS.
So once we have compiled java program, it can be executed on any platform. The details of the JVM differ from platform to platform and each OS has its own JVM format.  So in java it is “write once; run anywhere, anytime”.

So java is platform independent and JVM is highly platform dependent.

Why Java Is Preferred?

The developers preferred java over traditional programming languages like C and C++.Java is preferred due to its unique feature platform independent, robustness and more secure.
·         Simple: Java inherits most of the syntax of C and many of the OOPs features of C++. In addition to this is has its own add in features which are easy for professional programmers to learn.
·         Object Oriented: Java uses OOPs concepts for faster programming. It has most of the packages and interfaces built-in which are easily reused and extended. The class and object structure provides data encapsulation, abstraction, inheritance and polymorphism.
·         Robust:  Java is robust because of two reasons memory management and exception handling. In traditional programming C,C++ dynamic memory management is very difficult as memory allocation and de-allocation must be done by programmer but java does this dynamic memory allocation and de-allocation without any extra efforts. Exceptional conditions may arise in traditional programming which may leads to abnormal termination of programs but java provides exception handling which helps to handle and eliminate these run time errors.
·         Secure: All java programs run over JVM platform and not on Operating system. So any OS related updates or modification does not affect execution of java program. Java also provides packages and access modifiers which in turn provides security. Java applets are executed inside sandbox which creates an environment which provides restricted access to OS resources and allows user to run untrusted code from unknown source safely. Java uses byte code verifier module which checks java code automatically before executing it. Java provides run time array boundary check which is not provided in traditional programming. In traditional programming memory references can be manipulated but in java you cannot manipulate references which interns provides secure memory access.
·         Platform Independent: Traditional languages C, C++ were designed to compile for specific platform (processors, OS). In java executable code runs over JVM platform which is independent of OS. So once we have byte code (.class file) we can execute it on any OS. So java code needs to be compile only once and we can execute it any number of times across different platforms (processors, OS). So it is “write once, run anywhere, anytime”. 
·      Multithreaded: Java provides multithreaded programming which allows doing many things simultaneously. Each thread performs separate task. For example using text editor with spell check feature enabled which allows editing and spelling check simultaneously.