An object used to store elements of the same data type in contiguous memory locations
Characteristics of a single-dimensional array
It can store elements of the same data type
It represents a list of variables of the same data type
Elements are accessed by specifying their index
Declare a single-dimensional array in Java
int[] arrayName;
Declare and initialize a single-dimensional array in Java
int[] numbers = new int[5];
Two-dimensional array
An array of arrays where data is organized into rows and columns
Print elements of an array using a Java for loop
for (int i = 0; i < array.length; i++) { System.out.println(array[i]); }
Java for-each loop
Allows you to traverse an array without explicitly using indices
Arrays.toString() method
Converts an array to a stringrepresentation
Variables in Java
Containers for storing data values
Local variables
Declared inside a method, constructor, or block of code
Instance variables
Can be accessed from anywhere within the class
Difference 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
abs() method in Java Math class
Returns the absolute value of a number
sqrt() method in Java Math class
Returns the square root of a number
max() method in Java Math class
Returns the larger of two values
String literal
A sequence of characters enclosed in double quotes
Where string literals are stored
String pool
length() method for strings
Used to obtain the length of a string
Java Stream API
Processes collections of objects with functional-style operations
Iterator interface
Used to iterate over elements in a collection
Arrays.equals() method
Compares two arrays for equality
Arrays.asList() method
Converts an array to a list
Arrays.sort() method
Sorts the elements of an array
Java for-each loop
Allows you to iterate over elements in an array without using indices
log() method in Java Math class
Calculates the natural logarithm of a number
round() method in Java Math class
Rounds a floating-point value to the nearest integer
sin(), cos(), tan() methods in Java Math class
Calculate the trigonometric sine, cosine, and tangent of an angle
substring(int i, int j) method for strings
Returns the substring from the specified start index to the end index - 1
toLowerCase() method for strings
Converts all characters in the string to lowercase
replace(char oldChar, char newChar) method for strings
Replaces all occurrences of a specified character with another character in a string
Difference between string literal and string object
A string literal is stored in the string pool, while a string object is stored in the heap memory
charAt(int i)
Returns the character at the specified index position in this string
substring(int i)
Returns a new string that is a substring of this string
indexOf(String s)
Returns the index within this string of the first occurrence of the specified substring
String literal
Stored in the string pool
String object
Stored in the heap memory
String literal
Immutable
String object
Mutable
String literals are created using double quotes, while string objects are created using the new keyword</b>
String literals have a fixed size, while string objects can dynamically resize