Java Programming Keywords Summary

Here are the summary of the available keywords in the Java programming language. Keywords are reserved words that already taken and internally used by Java, so we cannot create variables and name it using this keyword.

Keyword Meaning
abstract an abstract class or method
assert used to locate internal program errors
boolean the Boolean type
break breaks out of a switch or loop
byte the 8-bit integer type
case a case of a switch
catch the clause of a try block catching an exception
char the Unicode character type
class defines a class type
const not used
continue continues at the end of a loop
default the default clause of a switch
do the top of a do/while loop
double the double-precision floating-number type
Keyword Meaning
else the else clause of an if statement
enum define an enum type
extends defines the parent class of a class
final a constant, or a class or method that cannot be overridden
finally the part of a try block that is always executed
float the single-precision floating-point type
for a loop type
goto not used
if a conditional statement
implements defines the interface(s) that a class implements
import imports a package
instanceof tests if an object is an instance of a class
int the 32-bit integer type
interface an abstract type with methods that a class can implement
long the 64-bit long integer type
Keyword Meaning
native a method implemented by the host system
new allocates a new object or array
null a null reference
package a package of classes
private a feature that is accessible only by methods of this class
protected a feature that is accessible only by methods of this class, its children, and other classes in the same package
public a feature that is accessible by methods of all classes
return returns from a method
short the 16-bit integer type
static a feature that is unique to its class, not to objects of its class
strictfp Use strict rules for floating-point computations
super the superclass object or constructor
switch a selection statement
synchronized a method or code block that is atomic to a thread
this the implicit argument of a method, or a constructor of this class
Keyword Meaning
throw throws an exception
throws the exceptions that a method can throw
transient marks data that should not be persistent
try a block of code that traps exceptions
void denotes a method that returns no value
volatile ensures that a field is coherently accessed by multiple threads
while a while loop type

How do I read an XML document using JDOM?

This example provide a small code snippet on how to use JDOM to parse an XML document, iterates each of the element and read the element or attributes values. To play with this example you have to have the JDOM library. You can download it from JDOM website.

package org.kodejava.jdom;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class ReadXmlDocument {
    public static void main(String[] args) {
        // Our imaginary xml file to be processed in this example.
        String data = """
                <root>
                    <row>
                        <column name="username" length="16">admin</column>
                        <column name="password" length="128">secret</column>
                    </row>
                    <row>
                        <column name="username" length="16">jdoe</column>
                        <column name="password" length="128">password</column>
                    </row>
                </root>""";

        // Create an instance of SAXBuilder
        SAXBuilder builder = new SAXBuilder();
        try {
            // Tell the SAXBuilder to build the Document object from the
            // InputStream supplied.
            Document document = builder.build(
                    new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)));

            // Get our xml document root element which equals to the
            // <root> tag in the xml document.
            Element root = document.getRootElement();

            // Get all the children named with <row> in the document.
            // The method return the children as a java.util.List
            // object.
            List<Element> rows = root.getChildren("row");
            for (Element row : rows) {
                // Convert each row to an Element object and get its
                // children which will return a collection of <column>
                // elements. When we have to column row we can read
                // the attribute value (name, length) as defined above
                // and also read its value (between the <column></column>
                // tag) by calling the getText() method.
                //
                // The getAttribute() method also provide a handy method
                // if we want to convert the attribute value to a correct
                // org.kodejava.data type, in the example we read the length attribute
                // value as an integer.
                List<Element> columns = row.getChildren("column");
                for (Element column : columns) {
                    String name = column.getAttribute("name").getValue();
                    String value = column.getText();
                    int length = column.getAttribute("length").getIntValue();

                    System.out.println("name = " + name);
                    System.out.println("value = " + value);
                    System.out.println("length = " + length);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6.1</version>
</dependency>

Maven Central

How do I sort items of an ArrayList?

This example shows you how we can sort items of an ArrayList using the Collections.sort() method. Beside accepting the list object to be sorted we can also pass a Comparator implementation to define the sorting behaviour such as sorting in descending or ascending order.

package org.kodejava.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ArrayListSortExample {
    public static void main(String[] args) {
        /*
         * Create a collections of colours
         */
        List<String> colours = new ArrayList<>();
        colours.add("red");
        colours.add("green");
        colours.add("blue");
        colours.add("yellow");
        colours.add("cyan");
        colours.add("white");
        colours.add("black");

        /*
         * We can sort items of a list using the Collections.sort() method.
         * We can also reverse the order of the sorting by passing the
         * Collections.reverseOrder() comparator.
         */
        Collections.sort(colours);
        System.out.println(Arrays.toString(colours.toArray()));

        colours.sort(Collections.reverseOrder());
        System.out.println(Arrays.toString(colours.toArray()));
    }
}

The code will output:

[black, blue, cyan, green, red, white, yellow]
[yellow, white, red, green, cyan, blue, black]

How do I sort array values in case-insensitive order?

By default, when sorting an arrays the value will be ordered in case-sensitive order. This example show you how to order it in case-insensitive order.

package org.kodejava.util;

import java.util.Arrays;

public class SortArrayCaseSensitivity {
    public static void main(String[] args) {
        String[] teams = new String[5];
        teams[0] = "Manchester United";
        teams[1] = "chelsea";
        teams[2] = "Arsenal";
        teams[3] = "liverpool";
        teams[4] = "EVERTON";

        // Sort array, by default it will be sorted in case-sensitive order.
        // [Arsenal, EVERTON, Manchester United, chelsea, liverpool]
        Arrays.sort(teams);
        System.out.println("Case sensitive  : " + Arrays.toString(teams));

        // Sort array in case-insensitive order
        // [Arsenal, chelsea, EVERTON, liverpool, Manchester United]
        Arrays.sort(teams, String.CASE_INSENSITIVE_ORDER);
        System.out.println("Case insensitive: " + Arrays.toString(teams));
    }
}

The result of the code snippet above:

Case sensitive  : [Arsenal, EVERTON, Manchester United, chelsea, liverpool]
Case insensitive: [Arsenal, chelsea, EVERTON, liverpool, Manchester United]

How do I sort array values in descending order?

Here you will find an example on how to sort the values of an array in ascending or descending order.

package org.kodejava.util;

import java.util.Arrays;
import java.util.Collections;

public class SortArrayWithOrder {
    public static void main(String[] args) {
        Integer[] points = new Integer[5];
        points[0] = 94;
        points[1] = 53;
        points[2] = 70;
        points[3] = 44;
        points[4] = 64;
        System.out.println("Original  : " + Arrays.toString(points));

        // Sort the points array, the default order is in ascending order.
        // [44, 53, 64, 70, 94]
        Arrays.sort(points);
        System.out.println("Ascending : " + Arrays.toString(points));

        // Sort the points array in descending order.
        // [94, 70, 64, 53, 44]
        Arrays.sort(points, Collections.reverseOrder());
        System.out.println("Descending: " + Arrays.toString(points));
    }
}

The result of the code snippet above are:

Original  : [94, 53, 70, 44, 64]
Ascending : [44, 53, 64, 70, 94]
Descending: [94, 70, 64, 53, 44]