How do I create a queue using LinkedList class?

package org.kodejava.util;

import java.util.LinkedList;
import java.util.Queue;

public class QueueDemo {
    public static void main(String[] args) {
        // Create an instance of a queue, ere we use the LinkedList class which
        // implements the Queue interface. Add some elements to the queue using
        // the offer method.
        Queue<String> queue = new LinkedList<>();
        queue.offer("First visitor");
        queue.offer("Second visitor");
        queue.offer("Third visitor");
        queue.offer("Fourth visitor");

        // Let see the size of our queue
        System.out.println("Size: " + queue.size());

        // The peek and element method read the head of the queue without removing
        // the element. The difference is, if the queue is empty peek method
        // return null while element method throws a NoSuchElementException
        // exception.
        System.out.println("Queue head using peek   : " + queue.peek());
        System.out.println("Queue head using element: " + queue.element());

        // The poll method retrieves and then removes the head of the queue.
        // On the next code will process all the element of the queue. When no
        // item in the queue the poll method will return null.
        Object data;
        while ((data = queue.poll()) != null) {
            System.out.println(data);
        }
    }
}

The code snippet above print the following output:

Size: 4
Queue head using peek   : First visitor
Queue head using element: First visitor
First visitor
Second visitor
Third visitor
Fourth visitor

How do I create a method that accept varargs in Java?

Varargs (variable arguments) is a new feature in Java 1.5 which allows us to pass multiple values in a single variable name when calling a method. Of course, it can be done easily using array but the varargs add another power to the language.

The varargs can be created by using three periods (...) after the parameter type. If a method accept others parameter than the varargs, the varargs parameter should be the last parameter to the method. And please be aware that overloading a varargs method can make harder to figure out which method is called in the code.

package org.kodejava.lang;

import java.util.Arrays;

public class VarArgsExample {
    public static void main(String[] args) {
        VarArgsExample e = new VarArgsExample();
        e.printParams(1, 2, 3);
        e.printParams(10, 20, 30, 40, 50);
        e.printParams(100, 200, 300, 400, 500);
    }

    public void printParams(int... numbers) {
        System.out.println(Arrays.toString(numbers));
    }
}

Running the code snippet give you the following output:

[1, 2, 3]
[10, 20, 30, 40, 50]
[100, 200, 300, 400, 500]

How do I use the Stack class in Java?

Stack is an extension of the java.util.Vector class that provided a LIFO (last-in-first-out) data structure. This class provide the usual method such as push() and pop(). The peek method is used the get the top element of the stack without removing the item.

package org.kodejava.util;

import java.util.Stack;

public class StackExample {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();

        // We stored some values in the stack object.
        for (int i = 0; i < 10; i++) {
            stack.push(i);
            System.out.print(i + " ");
        }
        System.out.println();

        // Searching for an item in the stack. The position returned
        // as the distance from the top of the stack. Here we search
        // for the 3 number in the stack which is in the 7th row of
        // the stack.
        int position = stack.search(3);
        System.out.println("Search result position: " + position);

        // The current top value of the stack
        System.out.println("Stack top: " + stack.peek());

        // Here we're popping out all the stack object items.
        while (!stack.empty()) {
            System.out.print(stack.pop() + " ");
        }
    }
}

The result of the code snippet:

0 1 2 3 4 5 6 7 8 9 
Search result position: 7
Stack top: 9
9 8 7 6 5 4 3 2 1 0 

How do I know the class of an object?

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

The instanceof operator is most likely used when implementing an equals(Object o) method of an object to check if the compared object is from the same class.

package org.kodejava.lang;

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

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

        List<Object> list = new ArrayList<>();
        list.add(person);
        list.add(animal);
        list.add(thing);
        list.add(text);
        list.add(number);

        for (Object o : list) {
            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 final String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

class Animal {
    private final String habitat;

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

    public String getHabitat() {
        return habitat;
    }
}

class Thing {
    private final 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 change an applet background color?

By default, the applet will usually have a grey background when displayed in a web browser. If you want to change it then you can call the setBackground(java.awt.Color) method and choose the color you want. Defining the background color in the init() method will change the color as soon as the applet initialized.

package org.kodejava.applet;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class BackgroundColorApplet extends Applet {
    public void init() {
        // Here we change the default grey color background of an 
        // applet to yellow background.
        setBackground(Color.YELLOW);
    }

    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        g.drawString("Applet background example", 0, 50);
    }
}

Now we have the applet code ready. To enable the web browser to execute the applet create the following html page.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Applet Background Color</title>
</head>
<body>
<applet code="org.kodejava.applet.BackgroundColorApplet"
        height="150" width="350">
</applet>
</body>
</html>
** Deprecated: The Applet API is deprecated since JDK 9, no replacement.