javarevisited

javarevisited


Difference between equals method and "==" operator in Java - Interview Question

Posted: 29 Dec 2012 06:15 PM PST

Both equals() and "==" operator in Java is used to compare objects to check equality but main difference between equals method and  == operator is that former is method and later is operator. Since Java doesn't support operator overloading, == behaves identical for every object but equals() is method, which can be overridden in Java and logic to compare objects can be changed based upon business rules. Another notable difference between == and equals method is that former is used to compare both primitive and objects while later is only used for objects comparison. At the same time beginners struggle to find when to use equality operator (==) and when to use equals method for comparing Java objects. In this tutorial we will see how equals() method and == operator works in Java and what is difference between "==" and equals method in Java and finally when to use "==" and equals() to compare objects.
Read more »

How to add, subtract days, months, years, hours from Date and Time in Java

Posted: 29 Dec 2012 07:21 AM PST

Adding days, hours, month or years to dates is a common task in Java. java.util.Calendar can be used to perform Date and Time arithmetic in Java. Calendar class not only provides date manipulation but it also support time manipulation i.e. you can add, subtract hours, minutes and seconds from current time. Calendar class automatically handles date transition or month transition for example if you ask date after 30 days it will return you date based on whether current month is 30 or 31 days long. Same is true in case of adding and subtracting years, Calendar takes care whether current or following year is a leap year or not. For example 2012 is a leap year and it has February with 29 days, if you ask Calendar day before 365 it will return 24th July (assuming current date 23rd July) which shows it take care of leap year. By the way there are couple of more date and time related articles e.g. How to find current date and time in Java and How to convert Date to String in Java. If you haven't read them already, It's worth checking to know more about Date and Time in Java.
Read more »

Difference between Primary key vs Foreign key in table – SQL database tutorial

Posted: 29 Dec 2012 01:09 AM PST

Main difference between Primary key and Foreign key in a table is that, it's the same column which behaves as primary key in parent table and as foreign key in child table. For example in Customer and Order relationship, customer_id is primary key in Customer table but foreign key in Order table. By the way what is foreign key in a table and difference between Primary and Foreign key are some of the popular SQL interview questions, much like truncate vs delete in SQL or difference between correlated and noncorrelated subquery. We have been learning key SQL concepts along with these frequently asked SQL questions and in this SQL tutorial we will discuss about what is foreign key in SQL and purpose of foreign key in any table. By the way this is the third article related to primary key in SQL, other being difference between primary and unique key and How to find second highest salary in SQL. If you are preparing for any technical job interview where you expect some SQL questions, check out these questions, they are worth preparing.
Read more »

java.lang.classcastexception cannot be cast to in Java - cause and solution

Posted: 28 Dec 2012 09:43 PM PST

As name suggests ClassCastException in Java comes when we try to type cast an object and object is not of the type we are trying to cast into. In fact ClassCastException in Java is one of most common exception in Java along with java.lang.OutOfMemoryError and ClassNotFoundException in Java before Generics was introduced in Java 5 to avoid frequent instances of java.lang.classcastexception cannot be cast to while working with Collection classes like ArrayList and HashMap, which were not type-safe before Java 5. Though we can minimize and avoid java.lang.ClassCastException in Java by using Generics and writing type-safe parameterized classes and method, its good to know real cause of ClassCastException and How to solve it. In this Java tutorial we will see Why java.lang.ClassCastException comes and how to resolve ClassCastException in Java. We will also see how to minimize or avoid ClassCastException in Java by using Generics, as prevention is always better than cure.
Read more »


1 comment:

  1. Your blog has best articles on java programming.

    Thanks,
    https://www.flowerbrackets.com/difference-between-equal-operator-vs-equals-method-java/

    ReplyDelete