DEV Community

Cover image for One Commit at a Time ! Series - 9
Annapoorani Kadhiravan
Annapoorani Kadhiravan

Posted on

One Commit at a Time ! Series - 9

JAVA

import java.util.Arrays;

class Main {
    public static void main(String[] args) {

        int [] source = {1, 2, 3, 4, 5, 6};
        int [] destination = new int[6];

        // iterate and copy elements from source to destination
        for (int i = 0; i < source.length; ++i) {
            destination[i] = source[i];
        }

         // converting array to string
        System.out.println(Arrays.toString(destination));
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation :

  • import java.util.Arrays; Imports the built-in utility class Arrays from the standard java.util package, allowing access to helper methods like Arrays.toString() for array manipulation and display.
  • class Main { Declares a class named Main. In Java, all code must reside inside a class structure.
  • public static void main(String[] args) { Defines the entry point of the Java application:
  • public: Accessible from anywhere.
  • static: Can be invoked without creating an instance of Main.
  • void: Does not return any value.
  • main: Name of the standard startup method.
  • String[] args: Accepts optional command-line arguments as an array of strings.
  • int [] source = {1, 2, 3, 4, 5, 6}; Declares and initializes an integer array named source containing six elements: 1, 2, 3, 4, 5, 6.
  • int [] destination = new int[6]; Allocates a new integer array named destination of size 6. By default, Java initializes all its integer elements to 0.
  • // iterate and copy elements from source to destination A single-line comment explaining the purpose of the subsequent for loop (ignored by the compiler).
  • for (int i = 0; i < source.length; ++i) { Sets up a loop to iterate through each index of the array:
  • int i = 0: Initializes loop index i at 0 (the first element).
  • i < source.length: Condition to keep looping while i is less than 6 (indices 0 through 5).
  • ++i: Pre-increments i by 1 after each loop cycle.
  • destination[i] = source[i]; Copies the element at index i of the source array into the matching index i of the destination array.
  • }Marks the end of the for loop body.
  • // converting array to string A single-line comment indicating that the array will be converted into a readable string format.
  • System.out.println(Arrays.toString(destination));
  • Arrays.toString(destination) converts the destination array into a readable string representation: "[1, 2, 3, 4, 5, 6]".
  • System.out.println(...) prints this string to the console followed by a newline.
  • } Closes the main method.
  • }Closes the Main class definition.

SQL

SELECT TOP Clause
The SELECT TOP clause is used to limit the number of records to return.

The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.

The following SQL selects only the first 3 records of the "Customers" table:

#Select only the first 3 records of the Customers table:

SELECT TOP 3 * FROM Customers;
Enter fullscreen mode Exit fullscreen mode

References :

https://www.programiz.com/java-programming/copy-arrays
https://www.w3schools.com/sql/sql_top.asp

Top comments (0)