Java Fundamentals: Type Conversion, Scope, Arrays, and More
Type Conversion
int n = Integer.parseInt("53");
// compile-time error
double m = Integer.parseInt("53");
// automatic conversion (int to double)
int n = Double.parseDouble("53");
// compile-time error
int n = Integer.parseInt("cat");
// run-time error
double
→ int
: (int)
castingString Conversion
String
→ int
: int x = Integer.parseInt(" ");
String
→ double
: double y = Double.parseDouble(" ");
String
→ boolean
: boolean z = Boolean.parseBoolean("true/false");
Scope
The parts of the program where the variable exists.
A variable only exists inside of the method in which it is declared.
In other words, a variable exists only within a { }.
Order of Operations
1. Casting; 2. ( ); 3. (*, /, %); 4. (+, -); 5. (<, <=, etc); 6. (==, !=); 7. (&&, ||)
String Concatenation
int x = 4;
int y = 5;
System.out.println(x + " + " + y + " is " + x + y);
// prints: 4 + 5 is 45 – Numbers after a String become String concatenation
System.out.println(x + y + " is " + x + y);
// prints: 9 is 45 – The + operator is evaluated left-to-right
System.out.println(x + y + " is " + (x + y));
// prints: 9 is 9
What prints…?
int x = 3; double y = (32 % x) * (x / 2);
// y = 2.0 * 1.0 because integer divisionPrefix & Postfix
++x
increments the value of x and then returns xx++
returns the value of x and then increments
int x = 0; int x = 5;
int y = 0; System.out.println(++x);
= ++x; // result: y=1, x=1 // prints 6
int x = 0; int x = 5;
int y = 0; System.out.println(x++);
= ++; // result: y=0, x=1 // prints 5
Reversing Elements in a String Array
int n = a.length;
for (int i = 0; i < n/2; i++) {
String temp = a[n-i-1];
a[n-i-1] = a[i];
a[i] = temp;
}
Array Methods
At the top of your .java file, add
import java.util.Arrays;
Arrays.equals(a, b)
– returns a boolean indicating whether or not the contents of the two arrays are the sameArrays.toString(arr)
– returns the contents of an array as a String valueArrays.deepToString(arr)
– prints all of the elements of arr, where arr is a multi-dimensional array
Methods
- Methods can return and can only return one value, or they can be void.
- Methods can take no input or any number of inputs.
- If a method is void, a return statement is not necessary but can still be added. In this case, it simply stops the method. However, it cannot return a value.
- If a method is not void, then there must be at least one return statement or multiple return statements.
- “Prints” means the method is void.
- If you don’t have a main method in your class, you can compile it but cannot run it.
- You cannot call a void method using
System.out.print(ln)
statements because it will return void (nothing). - Calling any
Math.whatever
method will give you a double value.
Conditions
Multiple ifs – might execute each if block multiple times if they are both true // they are totally separate
if/ else if/ else – will only execute the other block if the last block is false // they are related
Errors
Accessing an array at index -1: run-time error (ArrayIndexOutOfBoundsException)
Dividing an integer by 0: run-time error (ArithmeticException)
Accessing a variable outside of its scope: compile-time error
Omitting a semicolon at the end of a statement: compile-time error
Arrays
The value of an array variable is the memory address of that list.
All the elements of an array must be of the same type.
Default Value of Empty Arrays
int/double []
: 0boolean []
: falsereference types (Strings)
[]
: nullSwapping Methods
If you are swapping primitive types as parameters, it does not affect the content of the array since it does not take a reference as input.
When you swap parameters that are reference types (arrays), the method will change the elements.
Aliasing
int[] a = {1, 2, 3, 4, 5};
int[] b = a;
b[0] = 22;
System.out.println(a[0]);
// prints 22public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
change(a);
System.out.println(a[0]);
}
public static void change(int[] arr) {
arr[0] = 5; arr = new int[5];
arr[0] = 55; }
// prints 5 (changed it) // prints 1 (no change)
Immutable Strings
public static void main(String[] args) {
String s = "abcdef";
change(s);
System.out.println(s);
}
public static void change(String t) {
t = t + "ghijk";
}
// prints abcdef; strings are immutable. Java will only change the address in t, not s, so only the String t is changed, not the original s.
You cannot use any method to change the value of a String once it’s created, not even with the char method. Nothing can be changed as long as it’s a String. You must build a new String yourself using a for loop.