Java Basics and Quick Reference Guide
Java Basics and Quick Reference Guide
Strings in Java are immutable, meaning once a String object is created, its value cannot be changed. For example, calling concat() on a string creates a new string without modifying the original one: `String str = "hello"; str.concat("world");` results in the original `str` remaining "hello" . Unlike strings, mutable objects like arrays can have their values altered after creation, e.g., `int[] a = {1, 2, 3}; a[0] = 9;` changes the first element to 9 .
Arrays can be declared using array literals, which initializes the array with predefined values, e.g., `int[] a = {1, 2, 3};` and using the new keyword with explicit size or predefined values, e.g., `int[] a = new int[]{1, 2, 3};` or `int[] a = new int[3];` where elements can be assigned later . Using literals is more concise and preferred when initial values are known.
A for-each loop simplifies iteration over arrays, enhancing readability by abstracting loop counters and index handling. It eliminates index-out-of-bounds errors since it automatically iterates over all elements. For instance, `for (int number: numbers) { System.out.print(number); }` automatically covers each element without manual index management, unlike a traditional loop that requires managing counters explicitly .
When the input month is 3 in a switch statement, it outputs "March": `switch (month) { case 3: str = "March"; break; }` . If no case matches the input, the default case executes, e.g., `default: str = "Some other month";` ensures some output regardless of input .
The continue statement skips the current iteration, jumping to the loop's next iteration start. It's effective for bypassing specific conditions without exiting the loop. For example, in `for (int i = 0; i < 5; i++) { if (i == 3) { continue; } }` the iteration for `i == 3` is skipped, maintaining control flow and aiding in cleaner code by avoiding additional boolean checks .
Swapping variables in Java with a temporary variable, e.g., `int temp = a; a = b; b = temp;`, ensures that data isn't lost through overwriting in the swap process. This method sequentially transfers values, preventing any intermediate loss typical in techniques like adding and subtracting values, which might result in overflow or require explicit type casting .
The == operator checks if two string references point to the same object in memory, whereas the equals() method compares the actual content of the strings. For instance, `String s1 = new String("QuickRef"); String s2 = new String("QuickRef");` results in `s1 == s2` being false, as they are different objects, but `s1.equals(s2)` is true, as they contain the same characters .
Java performs implicit widening conversions when necessary, promoting smaller types to larger compatible data types during expressions. For instance, in expressions involving int and double, Java implicitly casts int to double to avoid data loss, as in `int i = 5; double d = i + 2.0;` . Such widening conversions ensure that operations are performed on the most suitable data type to maintain precision and avoid errors.
Java handles console input using the `Scanner` class. For string input, it uses `in.nextLine()` and for integer input `in.nextInt()`. For example, `Scanner in = new Scanner(System.in); String str = in.nextLine(); int num = in.nextInt();` reads a string and an integer from the console, then prints them respectively .
Widening casting, such as converting an int to a long (`int i = 10; long l = i;`), automatically converts a smaller data type to a larger one without data loss . Narrowing casting explicitly converts a larger data type to a smaller one, which might lead to data loss, e.g., converting a double to a long (`double d = 10.02; long l = (long)d;`) results in truncating the decimal part .