Java

Official website

The official website for Java is: https://www.java.com

You can visit this site to download Java, access documentation, and find additional resources related to the Java programming language and platform.

Static typing

Yes, Java is a statically typed language. This means that:

  • Type Checking at Compile-Time: Variable types are determined at compile time. If you try to assign a value of the wrong type to a variable, the code will fail to compile.
  • Explicit Type Declarations: In Java, you must declare the type of a variable when it is created. For example:

    int number = 5;
    String text = "Hello";
    
  • Early Error Detection: Statically typed languages like Java allow errors to be caught early in the development process rather than at runtime, which can help improve code reliability and maintainability.

Overall, the static typing in Java contributes to robust code development through structured type enforcement and compile-time checking.

Example code

Below is an example Java implementation of a binary search tree (BST) along with a method to search for a value within the tree. The algorithm takes advantage of the BST property—where for any given node, all values in the left subtree are smaller, and all values in the right subtree are larger than the node’s value—to decide whether to search left or right. This results in an efficient search with an average time complexity of O(log n).

// Define the Binary Search Tree Node class
class TreeNode {
    int value;
    TreeNode left;
    TreeNode right;

    TreeNode(int value) {
        this.value = value;
        left = null;
        right = null;
    }
}

// Define the Binary Search Tree class
class BinarySearchTree {
    TreeNode root;

    BinarySearchTree() {
        root = null;
    }

    // Method to insert a new node into BST
    public void insert(int value) {
        root = insertRecursive(root, value);
    }

    private TreeNode insertRecursive(TreeNode node, int value) {
        if (node == null) {
            return new TreeNode(value);
        }

        if (value < node.value) {
            node.left = insertRecursive(node.left, value);
        } else if (value > node.value) {
            node.right = insertRecursive(node.right, value);
        }
        // if the value is equal, you can decide to do nothing or handle duplicates
        return node;
    }

    // Search method: returns true if value is found, false otherwise
    public boolean search(int target) {
        return searchRecursive(root, target);
    }

    private boolean searchRecursive(TreeNode node, int target) {
        if (node == null) {
            // Reached a leaf without finding the value
            return false;
        }

        if (node.value == target) {
            // Found the target value
            return true;
        } else if (target < node.value) {
            // Target is less, search in left subtree
            return searchRecursive(node.left, target);
        } else {
            // Target is greater, search in right subtree
            return searchRecursive(node.right, target);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a BinarySearchTree instance and add some values
        BinarySearchTree bst = new BinarySearchTree();
        bst.insert(50);
        bst.insert(30);
        bst.insert(20);
        bst.insert(40);
        bst.insert(70);
        bst.insert(60);
        bst.insert(80);

        // Search for a value in the BST
        int target = 60;
        boolean found = bst.search(target);
        System.out.println("Value " + target + (found ? " found in" : " not found in") + " the BST.");
        
        // Search for another value
        target = 25;
        found = bst.search(target);
        System.out.println("Value " + target + (found ? " found in" : " not found in") + " the BST.");
    }
}

Explanation

  1. TreeNode Class:
    • Represents a node in the tree with an integer value and left/right child pointers.
  2. BinarySearchTree Class:
    • Contains the root of the BST and methods to insert values (insert and insertRecursive) and search for values (search and searchRecursive).
    • The insert method builds the tree by ensuring that for each node, values less than the node go to the left, and those greater go to the right.
    • The searchRecursive method works by comparing the target with the current node’s value. Based on the comparison, it decides whether to continue search in the left or right subtree, terminating when the value is found or when a null pointer is reached.
  3. Main Class:
    • Demonstrates how to create a BST, insert values, and search for specific values.

You can compile and run this Java code to see how the algorithm works.