Comprog 2

Cards (45)

  • Arrays in Java

    Objects containing elements of a similar data type, stored in contiguous memory locations
  • Types of Arrays
    • Single Dimensional Array
    • Two-Dimensional Array
  • Single Dimensional Array
    A list of variables of the same data type, accessed by specifying their index
  • Two-Dimensional Array
    An array of arrays, where data is stored in rows and columns
  • Declaration and Initialization
    1. Declaration Syntax: int[] a; // valid, int b[]; // valid
    2. Initialization Syntax: int[] a = new int[5]; // valid, Int b[] = new int[9]
  • Printing Array
    Using a for loop to iterate over elements and print them
  • Searching in Arrays
    1. Sequential search compares each element with the key until a match is found
    2. Methods like `binarySearch` and `compare` are available for more efficient searching
  • Sorting Arrays
    Bubble Sort, Selection Sort, Insertion Sort, and Merge Sort are common sorting algorithms
  • Arrays Class in Java

    The `Arrays` class in the `java.util` package offers static methods for dynamic array creation and manipulation
  • Methods in Arrays Class
    • asList, binarySearch, compare, copyOf, copyOfRange
  • Understanding arrays in Java is fundamental for storing and manipulating data efficiently
  • Java for loop
    A control flow statement that allows you to iterate over a range of values or elements in an array. It consists of an initialization expression, a condition, and an iteration statement, executed until the condition evaluates to false
  • Java for-each loop
    Also known as an enhanced for loop, it provides a simpler syntax for iterating over elements in arrays or collections. It eliminates the need for explicit indexing and allows direct access to each element in the array or collection
  • Java Arrays.toString() method

    A method provided by the Arrays class in Java that returns a string representation of the contents of an array. It converts the array into a comma-separated string enclosed within square brackets
  • Java Arrays.deepToString() method
    Similar to Arrays.toString(), this method is used to obtain a string representation of nested arrays. It recursively converts multi-dimensional arrays into a string representation
  • Java Arrays.asList() method

    A method provided by the Arrays class that returns a fixed-size list backed by the specified array. It allows converting an array into a list, enabling manipulation of array elements using List interface methods
  • Java Iterator Interface
    An interface in the Java Collections Framework used to iterate over elements in a collection. It provides methods like hasNext() to check for the next element and next() to retrieve the next element in the collection
  • Java Stream API
    Introduced in Java 8, the Stream API provides a fluent and functional approach for processing collections of objects. It allows performing aggregate operations on collections, such as filtering, mapping, and reducing, using functional-style operations like map, filter, and reduce
  • Java Fundamentals Reviewer: Variables and Methods
  • Variables
    Containers for storing data values, characterized by a data type, which determines the type of data that can be stored and the operations that can be performed on it
  • Scope of Variables
    • Local Variables
    • Global Variables
    • Formal Parameters
  • Local Variables

    Declared within a method, constructor, or block of code, accessible only within the scope of the block in which they are declared
  • Global Variables
    Declared outside any method, constructor, or block, accessible throughout the entire class
  • Formal Parameters
    Variables declared in the parameter list of a method or constructor
  • Key Differences between Local and Global Variables

    • Local variables are accessible only within the block of code in which they are declared, while global variables can be accessed from anywhere within the class
    • Local variables are created when the block is entered and destroyed when the block is exited, whereas global variables persist as long as the program is running
    • Local variables facilitate data encapsulation and prevent unintended access, while global variables provide easy access but can lead to potential issues like data inconsistency
  • Methods
    Blocks of code that perform specific tasks or operations, enhancing code modularity, reusability, and readability
  • Static Methods
    Belong to the class rather than instances of the class, can be called using the class name, commonly used for utility functions
  • Non-Static Methods

    Also known as instance methods, operate on specific instances of a class and can access instance variables
  • Arguments
    Values passed to a method when it is called, can be used within the method to perform computations or operations
  • Variables serve as containers for storing data, while methods encapsulate blocks of code to perform specific tasks
  • Understanding variable scope is crucial for managing data effectively and preventing unintended access
  • Methods enhance code modularity and reusability, and they can be either static or non-static based on their behavior and usage
  • Syntax codes provide a structured way to declare variables and methods, making code more readable and understandable
  • String Literal
    A sequence of characters enclosed in double quotes
  • String Object
    An instance of the java.lang.String class that represents a sequence of characters
  • String Pool
    A pool of unique string literals stored in memory by the JVM
  • String Methods
    • length(), charAt(int i), substring(int i), substring(int i, int j), concat(String str), indexOf(String s), equalsIgnoreCase(String anotherString), compareTo(String anotherString), toLowerCase(), toUpperCase(), trim(), replace(char oldChar, char newChar)
  • Java Math Class Methods
    • abs(), max(), min(), round(), sqrt(), pow(), log(), sin(), cos(), tan(), toDegrees(), toRadians()
  • Exception handling in Java helps manage runtime errors, maintaining the application's normal flow
  • Exceptions
    Unexpected events during program execution that disrupt the normal flow of instructions