How do I know a class of an object?

For instance you have a collection of object in an list object and you want to do some logic base on the object class. This can easily be done using the instanceof operator. The operator returns true if an object is an instance of a class, if not it will return false.

The instanceof method is most likely used when implementing an equals(Object o) method of an object.

package org.kodejava.example.lang;

import java.util.ArrayList;
import java.util.List;

public class InstanceOfExample {
    public static void main(String[] args) {
        Person p = new Person("John");
        Animal a = new Animal("Highland");
        Thing t = new Thing("Red");
        String text = "hello";
        Integer number = 1000;

        List list = new ArrayList();

        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);
            if (o instanceof Person) {
                System.out.println("My name is " + ((Person) o).getName());
            } else if (o instanceof Animal) {
                System.out.println("I live in " + ((Animal) o).getHabitat());
            } else if (o instanceof Thing) {
                System.out.println("My color is " + ((Thing) o).getColor());
            } else if (o instanceof String) {
                System.out.println("My text is " + o.toString());
            } else if (o instanceof Integer) {
                System.out.println("My value is " + ((Integer) o));

class Person {
    private String name;

    public Person(String name) { = name;

    public String getName() {
        return name;

class Animal {
    private String habitat;

    public Animal(String habitat) {
        this.habitat = habitat;

    public String getHabitat() {
        return habitat;

class Thing {
    private String color;

    public Thing(String color) {
        this.color = color;

    public String getColor() {
        return color;

The result of the code snippet above:

My name is John
I live in Highland
My color is Red
My text is hello
My value is 1000

How do I check string for equality?

To compare string for their content equality we must use the String.equals() method. This method ensures that it will compare the content of both string instead of the object reference of the both string. This method returns true if both string in comparison have the same content.

Do not, never, use the == operator to compare string for its content. The == operator check for object reference equality, it returns true only if both objects point to the same reference. This operator returns false if the object doesn’t point to the same references.

package org.kodejava.example.lang;

public class StringEquals {
    public static void main(String[] args) {
        String s1 = "Hello World";
        String s2 = new String("Hello World");

        // This is good!
        if (s1.equals(s2)) {
            System.out.println("1. Both strings are equals.");
        } else {
            System.out.println("1. Both strings are not equals.");

        // This is bad!
        if (s1 == s2) {
            System.out.println("2. Both strings are equals.");
        } else {
            System.out.println("2. Both strings are not equals.");

In the example above we deliberately create an instance of s2 string using the new operator to make sure that we have a different object reference. When you run the program it will give you the following result:

1. Both strings are equals.
2. Both strings are not equals.