Thursday, August 19, 2010

String comparison in Java

String class is an important API in Java but this is the most confusing also when asked in Interviews
regarding its comparison with another String. Here I am discussing the same.

The difference between == and equals(): Both operators can be used in Java, but the second one is a method.

"==" This is called the "Equal to" operator and is used to compare "primitive" types or check if 
you have equal object references(or do they refer to the same object in the heap). There are 8 primitive 
types in Java and you can usually identify them: 
byte, short, int, long, float, double, boolean, char

When used with Strings, one might assume that you are making a comparison with the String values, but no. 
We mentioned above that "Equal to" operator checks for object references when used with objects.

So let's assume the following code:
String s1 = "STRING ME";
String s2 = "STRING ME";
System.out.println("s1 == s2 is " + (s1 == s2));

This would output:
s1 == s2 is true

Why? The JVM does some optimization step with Strings(i.e the Strings get "pooled"). Basically, the JVM 
makes you point to the same Object reference that is pooled in the heap.

Let's make some modification with the Strings
s1 += "2";
s2 += "2";
System.out.println("s1: " + s1);
System.out.println("s2: " + s2);
System.out.println("s1 == s2 is " + (s1 == s2));

The output is now:
s1: STRING ME2
s2: STRING ME2
s1 == s2 is false

Why? Because Strings are "immutable", new Object references are actually created. The String was not actually 
modified, it is a new String object. They are now different object references.

Now, what most of us usually want is "OBJECT EQUALITY", and that's what the next thing is for "equals()"
All objects can use and override the equals() method. Any instance of a class you use or created automatically 
inherits this method. Without overriding, you use the default implementation of the Object class which is not 
very helpful. This works differently when used with the java.lang.String class as it does some character 
comparisons. You cannot override this method in String because String is a final class and cannot be extended.

Using a similar set of code, let's try to make modifications with the Equal To operator, for the String references 
to use the equals() method.

String s1 = "STRING ME";
String s2 = "STRING ME";
System.out.println("s1 == s2 is " + (s1.equals(s2) ) ); //true

s1 += "2";
s2 += "2";
System.out.println("s1 == s2 is " + (s1.equals(s2) ) ); //true
//Both will display "true".

So a final complete example with some added test cases:
----------------------------
public class StringAnother {
    public static void main(String[] args) {
        String s1="Hello";
        String s2=new String("Hello");
        String s3="Hello";
        String s4=new StringBuffer("Hello").toString();
        String s5="Hel"+"lo";
        
        System.out.println("s1==s2: "+(s1==s2));
        System.out.println("s1==s3: "+(s1==s3));
        System.out.println("s1==s4: "+(s1==s4));
        System.out.println("s1==s5: "+(s1==s5));
                
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s4));
        System.out.println(s1.equals(s5));
    }
}
/////Output:
s1==s2: false
s1==s3: true
s1==s4: false
s1==s5: true
true
true
true
true
----------------------------END--------------------------

Wednesday, August 11, 2010

SVN plugin in myeclipse 6.5

SVN plugin in myeclipse 6.5:

MyEclipse-> Help ->Software Updates -> Find and Install -> Search for new features to install-> Next -> New Remote Site->

Now please enter values in text fields of Dialog box:

Name- SVN
Url- http://subclipse.tigris.org/update_1.6.x

Click on Ok/Next/Finish. You need only SVN/Subeclipse, Avoid any other libraries shown like JNA..

Ration card rules in bangalore

Taken from: http://timesofindia.indiatimes.com/city/bangalore-times/Ration-card-rules/articleshow/16684790.cms

Life is so much easier if you have a ration card. Whether you buy the rations doled out or not, it is wiser to possess one, because it has its uses - whether it's at the passport office or the RTO.
For getting a ration card afresh, here's what you have to do:
•You have to fill in an application form available at any of the five range offices of the Food and Civil Supplies department.
•You will also need your voter identity card, and a court fee stamp of Rs 2.
•Attach proof of residence. For this, you can offer any one of the following - rent agreement document, tax paid receipt, telephone bill, gas bill, electricity bill, bank passbook; even a letter mailed to the house will do.
Submit these at the jurisdictional range office. There are five of them: East - near RBANMS college, West - Bhashyam Circle, Rajajinagar, North - Vyalikaval, South - 5th main road, Chamarajpet, Central - opposite Vishveshwaraiah museum.
Submit the documents, get an acknowledgement. An inspector from the department will visit your residence to check if you really reside there, after which he sends his report to the office. And the ration card is ready for you to collect in less than a month.
On the day you go to receive your card, pay Rs 45, and get a computerised card, complete with your photo in it.
Surrender certificate
In case you are applying for a ration card and you have held one earlier at another place, you have to get a surrender certificate from the previous place. A ration card will be issued afresh on the same day.
Deletion certificate
In case there has been a bifurcation in the family, if a member has married and moved away, the name has to be deleted. For this you have to get a deletion certificate and apply with the other documents. The card will be issued on the same day.
Addition certificate
If a child's name is to be included, produce the birth certificate. If you don't have one, get a certificate issued by the school authorities. The card will be issued on the same day.
According to officials at the Food and Civil Supplies department, Chamarajpet, there are three kinds of ration cards issued - yellow (earlier green) for those below the poverty line; blue card or the photo card for those above the poverty line; and the white card or what is called the honorary card, for those who want the card only for address proof purposes and not for rations. This is issued within 10 days of your applying for it.

Friday, August 6, 2010

List of JDBC drivers for Java

A List of JDBC Drivers
If you need to access a database with Java, you need a driver. This is a list of the drivers available, what database they can access, who makes it, and how to contact them.

IBM DB2
jdbc:db2://HOST:PORT/DB
COM.ibm.db2.jdbc.app.DB2Driver

JDBC-ODBC Bridge
jdbc:odbc:DB
sun.jdbc.odbc.JdbcOdbcDriver

Microsoft SQL Server
jdbc:weblogic:mssqlserver4:DB@HOST:PORT
weblogic.jdbc.mssqlserver4.Driver

Oracle Thin
jdbc:oracle:thin:@HOST:PORT:SID
oracle.jdbc.driver.OracleDriver

PointBase Embedded Server
jdbc:pointbase://embedded:PORT/DB
com.pointbase.jdbc.jdbcUniversalDriver

Cloudscape
jdbc:cloudscape:DB
COM.cloudscape.core.JDBCDriver

Cloudscape RMI
jdbc:rmi://HOST:PORT/jdbc:cloudscape:DB
RmiJdbc.RJDriver

Firebird (JCA/JDBC Driver)
jdbc:firebirdsql://HOST:PORT/DB
org.firebirdsql.jdbc.FBDriver

Informix Dynamic Server
jdbc:informix-sqli://HOST:PORT/DB:INFORMIXSERVER=SERVER_NAME
com.informix.jdbc.IfxDriver

Hypersonic SQL (v1.2 and earlier)
jdbc:HypersonicSQL:DB
hSql.hDriver

Hypersonic SQL (v1.3 and later)
jdbc:HypersonicSQL:DB
org.hsql.jdbcDriver

Microsoft SQL Server (JTurbo Driver)
jdbc:JTurbo://HOST:PORT/DB
com.ashna.jturbo.driver.Driver

Microsoft SQL Server (Sprinta Driver)
jdbc:inetdae:HOST:PORT?database=DB
com.inet.tds.TdsDriver

Microsoft SQL Server 2000 (Microsoft Driver)
jdbc:microsoft:sqlserver://HOST:PORT;DatabaseName=DB
com.microsoft.sqlserver.jdbc.SQLServerDriver

MySQL (MM.MySQL Driver)
jdbc:mysql://HOST:PORT/DB
org.gjt.mm.mysql.Driver

Oracle OCI 8i
jdbc:oracle:oci8:@SID
oracle.jdbc.driver.OracleDriver

Oracle OCI 9i
jdbc:oracle:oci:@SID
oracle.jdbc.driver.OracleDriver


To test your driver once it's installed, try the following code:
//Java code

{
Class.forName("Driver name");
Connection con = DriverManager.getConnenction("jdbcurl","username","password");
//other manipulation using jdbc commands
}
catch(Exception e)
{}

//Taken from a site: http://www.devx.com/tips/Tip/28818