Wednesday, December 28, 2016

java: difference between == and equals() with examples

Both "==" and "equals()" are used to check if two things equal to each other.

The "==" is an operator which can be used to compare primitives as well as Objects. It checks if the two items reference to the same memory location. Since it is an operator, it cannot be overridden.

The "equals()" is a method in the Object class. The "equals()" method in the Object class works the same as the "==" operator. However, since it is a method, it can be overridden to compare based on object components or business logic. As in the case of ArrayList, the equals method compares if the two ArrayLists have the same sequence of objects.

Below are some examples.

1. The String class

The String class is peculiar for it can be initiated with a string constant or a new String instance. Two String constants with the same sequence of characters are always equal regardless of using "==" or "equals()". However, it is a good practice to use equals when comparing two Strings.

String strA = "Welcome!";
String strB = "Wel" + "come!";
String strC = new String(strA);
String strD = new String("Welcome!");

System.out.println("strA == strB? " + (strA == strB));
System.out.println("strA.equals(strB)? " + (strA.equals(strB)));
System.out.println("strA == strC? " + (strA == strC));
System.out.println("strA.equals(strC)? " + (strA.equals(strC)));
System.out.println("strC == strD? " + (strC == strD));
System.out.println("strC.equals(strD)? " + (strC.equals(strD)));

The output:
strA == strB? true
strA.equals(strB)? true
strA == strC? false
strA.equals(strC)? true
strC == strD? false
strC.equals(strD)? true

2. Without overriding the equals() method

public class ObjA {
   private ArrayList<String> array;
   private int numb;

   public ObjA() {
        array = new ArrayList<>();
        numb = 3;
   }
 
   public static void main(String[] args) {
        ObjA theFirst = new ObjA();
        ObjA theSecond = new ObjA();
        ObjA theThird = theFirst;

        System.out.println("Is theFirst == theSecond? " + (theFirst == theSecond));
        System.out.println("Is theFirst.equals(theSecond)? " + (theFirst.equals(theSecond)));
        System.out.println("Is theFirst == theThird? " + (theFirst == theThird));
        System.out.println("Is theFirst.equals(theThird)? " + (theFirst.equals(theThird)));
        System.out.println("Is theSecond == theThird? " + (theSecond == theThird));
        System.out.println("Is theSecond.equals(theThird)? " + (theSecond.equals(theThird)));
   }
}

The Outpput:
Is theFirst == theSecond? false
Is theFirst.equals(theSecond)? false
Is theFirst == theThird? true
Is theFirst.equals(theThird)? true
Is theSecond == theThird? false
Is theSecond.equals(theThird)? false

3. With overriding the equals() method

public class ObjB {
   private ArrayList<String> array;
   private int numb;

   public ObjB() {
        array = new ArrayList<>();
        numb = 3;
   }

   public ArrayList getArray() {
         return array;
   }

   public int getNumber() {
         return numb;
   }

   @Override
   public boolean equals(Object obj) {
         if (obj instancdof ObjB){
              ObjB ob = (ObjB)obj;
              if (getArray().equals(ob.getArray()) && getNumber() == ob.getNumber()){
                   return true;
              }
         }
         return false;
   }
 
   public static void main(String[] args) {
        ObjB theFirst = new ObjB();
        ObjB theSecond = new ObjB();
        ObjB theThird = theFirst;

        System.out.println("Is theFirst == theSecond? " + (theFirst == theSecond));
        System.out.println("Is theFirst.equals(theSecond)? " + (theFirst.equals(theSecond)));
        System.out.println("Is theFirst == theThird? " + (theFirst == theThird));
        System.out.println("Is theFirst.equals(theThird)? " + (theFirst.equals(theThird)));
        System.out.println("Is theSecond == theThird? " + (theSecond == theThird));
        System.out.println("Is theSecond.equals(theThird)? " + (theSecond.equals(theThird)));
   }
}

The Outpput:
Is theFirst == theSecond? false
Is theFirst.equals(theSecond)? true
Is theFirst == theThird? true
Is theFirst.equals(theThird)? true
Is theSecond == theThird? false
Is theSecond.equals(theThird)? true

---------------------------------------------------------------------------------------------------

                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Friday, December 23, 2016

NetBeans: warning: [unchecked] unchecked call to setModel(ComboBoxModel) as a member of the raw type

After building a project in NetBeans, the following messages are displayed in the Output area:

warning: [unchecked] unchecked call to setModel(ComboBoxModel<E>) as a member of the raw type JComboBox
        this.typeCombo.setModel(new DefaultComboBoxModel(type));
where E is a type-variable:
      E extends Object declared in class JComboBox

This is because, since Java 7, JComoBox has a generic type for its components.

If you are displaying String in the combo box, you may declare it as below.

       JComboBox<String> = new JComboBox<>();

If you dragged the Combo Box from the Palette to the Design area in NetBeans to build your display, click on the combo box in Design. In general, the Properties will display at the right of the screen. Select the Code tab, and enter <String> in Type Parameters.

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

Friday, December 16, 2016

SQL: UPSERT syntax and examples



UPSERT is part of the data manipulation language (DML) that allows one to use a single statement to atomically either INSERT a row, or on the basis of the row already existing, UPDATE that row instead, while safely giving little to no further thought to concurrency. The "essential property of UPSERT" is that one of those two outcomes must be guaranteed, regardless of concurrent activity.

PostgreSQL:

Starting PostgreSQL 9.5, UPSERT becomes part of its DML.

Use the ON CONFLICT clause:

For example, you have a store table and you want to set the store name to 'Unforgettable Fruits' where the store id is 559. You can use the following SQL to guarantee the result regardless if store 559 already exists. 
INSERT INTO store (id, name) values (559, 'Unforgettable Fruits')
ON CONFLICT (id)
DO UPDATE SET name = 'Unforgettable Fruits';

Use the WITH clause:

Syntax:
WITH upsert AS
      (UPDATE <table name>
            SET <column name> = <value>
            WHERE <condition> RETURNING *)
      INSERT into <table name> (<columns separated by comma>)
            SELECT <values separated by comma>
            WHERE not exists (SELECT * from upsert);
 With the above store example:
WITH upsert AS
      (update store set name = 'Unforgettable Fruits' where id=559 returning *)                        insert into store (id, name) select 559, 'Unforgettable Fruits' where not exists (select * from upsert);

Oracle:

The above SQL starting with 'WITH upsert AS' does not work in Oracle. However Oracle can use the MERGE clause to do the same job. MERGE is typically used to merge two tables, and was introduced in the 2003 SQL standard.

Syntax:
MERGE into <table name>
USING
      <source table/view/result of sub-query>
ON
       <match condition>
WHEN MATCHED THEN
       <update clause>
       <delete clause>
WHEN NOT MATCHED THEN
      <insert clause>
Using the above store example.
merge into store using                                                                                                       (select id, name from store_temp b) on (a.id = b.id)                                               when matched then                                                                                                             update set a.name = b.name                                                                               when not matched then                                                                                                       insert (a.id, a.name) values (b.id, b.name); 
The MERGE clause is also supported in DB2 and MS SQL.

MySQL:

The ON DUPLICATE KEY UPDATE clause: Checks if a primary key or unique index is violated by an insert, it will then do an update.

Syntax:
INSERT into <table name> (<columns separated by comma>) values (<values separated by comma>)
ON DUPLICATE KEY <update clause>

Using the above store example, which has the id as the primary key:
INSERT into store (id, name) values (559, 'Unforgettable Fruits')
ON DUPLICATE KEY UPDATE name='Unforgettable Fruits';
Of course, you can always do a select first to check if the record exists, then decide if you need to update or insert. You can also do an update first, if it returns zero, then insert.
-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

















Syntax: