Saturday, July 26, 2014

JDBC Connectivity in Java

JDBC is used to establish connection with database through java program. Java Database connectivity is four step procedures. Let us see each step in detail for oracle database connectivity.

1. Loading database driver: first step is to load the driver class called “OracleDriver” into main memory.
Class.forName( "oracle.jdbc.driver.OracleDriver" );
Here “oracle.jdbc.driver” is package name and “OracleDriver” is a class name which contains driver methods.

2.Establish a Database Connection: second step is to create connection object by mentioning driver name, server address, user name and password.
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl”,”Username”,"Password” );
Here “jdbc” is database, “oracle” is sub database, “thin” is driver type, “127.0.0.1” is host name, “1521” is port number used for connection, “orcl” is name of service used (SID), “Username” is database username and “Password” is password for corresponding user.

3.Execute SQL Statements: Once connection with database is created, we need to execute SQL queries using above connection object.
Statement stmt = conn.createStatement();
stmt.executeUpdate(“QUERY”);
Here we have created statement stmt using connection object and “QUERY” is SQL statement that has to be executed using executeUpdate().

4.Close Database Connection: Once all the SQL transactions are done with database, we need to close the connection with database.

conn.close();

Wednesday, July 23, 2014

What is difference between String,StringBuffer and StringBuilder

The main difference is w.r.t. whether the objects are mutable or immutable.
Immutable means that once the object is created the value inside the object cannot be changed. Mutable object allows the value inside object to be changed whenever needed.
The main difference between String, StringBuffer and StringBuilder is that Sting objects are immutable whereas StringBuffer and StringBuilder objects are mutable.
Suppose we have String object as
String str="Hello";
str = "Good Morning";
str is assigned value “Hello” and further if str is assigned new value “Good Morning” . it will not replace “Hello” with “Good Morning” and it will have both the strings in string literal pool but str will start pointing to “Good Morning”. If we keep on changing value of str like this, string literal pool will have all those previous strings which increases size of string literal pool and every time new string object is created.
So if there are frequent changes in string contents then StringBuffer or StringBuilder should be used.
DIFFERENCE BETWEEN STRINGBUFFER AND STRINGBUILDER
StringBuffer and StringBuilder both have same methods with only difference of synchronization. All the methods of StringBuffer are synchronized while the methods of StringBuilder are not synchronized. The synchronized methods are thread safety implemented for thread environment. So if you are using threads in your program, StringBuffer should be used otherwise StringBuilder can be used.
Here is an example which will clear the exact difference between them.
String a = "Hi";
StringBuffer b = new StringBuffer("Hello");
StringBuilder c = new StringBuilder("Hello");
a=a.append("Java");
b=b.append("Java");
c=c.append("Java");
Here new object is created for String with value “Hi Java”, whereas in case of StringBuffer and StringBuilder, the same object is updated with the value “Hello Java”.

Finally when to use which string class
• String: if string operations are not used in your programs then you should use String class.
• StringBuffer: if you are using threads then you should use StringBuffer class as methods are synchronized
• StringBuilder: if you are not using threads then should use StringBuilder class.

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.