3.arrays and Operators
3.arrays and Operators
Type[] arrayName;
This declares an array named arrayName that will store elements of type Type.
Type arrayName[];
This is an alternative syntax that also declares an array. It's less preferred because it mixes
the type with the array's name, which can be confusing in complex declarations.
To allocate memory for the array (i.e., specify the size of the array), use the
new keyword:
This not only declares the array but also allocates space for size elements in
the array.
Example: int[] myArray = new int[10]; declares and allocates space for an
array of 10 integers.
Fixed ??????
In Java, when we say the length of an array is "fixed," it means that once an array is created with a
specified number of elements, the size of the array cannot be changed. The capacity to store elements
is determined at the time of array creation and remains constant throughout the lifetime of the array.
Initialization: When you create an array in Java, you must either specify the size of the array upfront
or initialize it with a specific set of elements. For example,
int[] myArray = new int[10]; creates an array that can hold 10 integers. Alternatively,
int[] myArray = {1, 2, 3, 4, 5}; creates an array with 5 elements. In both cases, the size is fixed
at creation.
No Resizing: After an array is created, you cannot add more elements to the array than it was initially
declared to hold. If you attempt to store more elements than its declared capacity, you'll get an
ArrayIndexOutOfBoundsException. For instance, attempting to access or assign a value to
myArray[10] in an array declared with a size of 10 (indexes 0 through 9 are valid) would cause an
error.
Advantages
Code Optimization: It makes the code optimized, we can retrieve or sort the
data efficiently.
Random access: We can get any data located at an index position.
Disadvantages
Size Limit: We can store only the fixed size of elements in the array. It doesn't
grow its size at runtime. To solve this problem, collection framework is used
in Java which grows automatically.
Types of Array in java
This declares an array, allocates space for the given elements, and initializes it
with the provided values.
Type[][] arrayName;
Declares and allocates a two-dimensional array with a specified number of rows and columns.
Example: int[][] matrix = {{1, 2}, {3, 4}}; creates a 2x2 matrix.
Initializing an Array in Separate Steps
You can declare an array and then allocate memory for it using the new keyword,
and finally initialize its elements:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}; // A 3x3 matrix
This creates a 3x3 matrix where the elements are initialized to the specified
values.
public class Main {
public static void main(String[] args) {
// Declare an array of integers
int[] numbers;
// Allocate memory for 5 integers
numbers = new int[5];
// Initialize elements
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Access and print the third element
System.out.println("The third number is: " + numbers[2]);
}}
Arithmetic Operators :
Examples:
System.out.println(10 + 5); // 15
System.out.println(10 - 5); // 5
System.out.println(10 * 5); // 50
System.out.println(10 / 5); // 2
System.out.println(10 % 5); // 0
Relational Operators
Relational operators compare two operands and return a boolean value.
Examples:
Examples:
Examples:
int a = 10;
a += 5; // a = a + 5
a -= 3; // a = a - 3
a *= 2; // a = a * 2
a /= 4; // a = a / 4
System.out.println(a); // 3
Unary Operators
Unary operators operate on a single operand.
Examples: