I just graduated from Flatiron Software Engineer Immerse Course. While I’m job searching, I realized many companies want candidates to know Java, especially in the finance industry. I want to use this serial to document my Java journey and see where I can get with java in a 30 days.
Variables in Java
There are two types of variables in JS — Primitive and Reference:
Primitive hold simple values: there are 8 primitive types in Java:
Reference Type holds complex object — for example, Date, String, Point…
byte < short < int < long
“Smaller type” can be converted to “bigger type” by default
“bigger type” can be converted to “smaller type” with (type)operater
***Things to take note of:
a. When declaring primitive type, we don't need to allocate memory. memory is allocated and release by the java run time environment.
When declaring reference type, we need to use the new operator to allocate memory of this var.
byte age = 30;
Date date = new Date()
b. Memory management of the two types:
Primitive type store values, when we assign x to y, we copied the value store in x into y. If x value changes, It would not impact y.
byte x = 1;
byte y = x;
byte x = 2System.out.println(y) => 1
Reference type store reference(address). when we assign x to y, we copied the address store in x into y. If we change the value store in the x variable, of course, y would be changed as well. It’s like you and me both have a copy of the key of a house. If you change something of the room since I hold the key of the same room. I’m affected as well.
Point point1= new Point(1, 3);
Point point2= point1;
point2.x = 2System.out.println(point2.x) => 2
String in Java:
The string is a reference type, but we use it so much. We can declare it just like how we declare primitive, omit the new operator
Also, in java String is immutable. If you manipulate the string in any way. It would always return a new string.
// When we declare string, we can just write below as a shortcut, it's the same as String a = new String("Hello World")String message= "Hello World"// Some Useful method of the string class
message.endWith("d") => True
messsage.startWidh("E") => False
message.indexOf("H") => 0 (return -1 if not presence)
message.length() => 11
message.replace("l", "*") => "He**o Wor*d"
message.toLowerCase() => "hello world"
message.trim() => trim the white space in the begininging and end end of the string. Not the one in the middle.// Escape charactor with \ in java:
String message = "Hello \"World\"" => hello "world"
String message = "c:\\windows\\..." => c:\windows\...
String message = "c:\n windows" => c:
windows
String message = "c:\t windows" => c: windows
Arrays in java
Array has a fixed size in java, cannot remove or add elements after declaration.
//declare an array of int length of 6
int[] nums = new int[6];
nums[0] = 1;
nums[1] = 2;
//Print an array directly, give you a string cal base on the address in the memory.
System.out.println(nums) => [I@e9e54c2
//Default to be 0(int array) False(Boolean array) ""(String array)
System.out.println(Arrays.toString(numbers)) => [1, 2, 0, 0, 0, 0]//a easier way to declare an array
int[] numbers = {1,2,3,4,5};
System.out.println(Arrays.toString(numbers)); => [1,2,3,4,5]
System.out.println(numbers.length); => 5//sort an array:
int[] numbers = {1,3,2,5};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)) => [1, 2, 3, 5]
Multi-Dimensional Array in Java
// declare a 2 * 3 dementional array
int[][]numbers = new int[2][3];
numbers[0][0] = 1;
System.out.println(Arrays.deepToString(numbers));
=>[
[1,0,0],
[0,0,0]
]// declare a 2 * 3 * 4 dementional arrayint[][][] numbers = new int[2][3][4];
numbers[0][0][0] = 1;
System.out.println(Arrays.deepToString(numbers));
=> [
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
]// another way to declare with {}
int[][] numbers = {{1,2,3},{3,4,5}};
System.out.println(Arrays.deepToString(numbers));
=>[
[1, 2, 3],
[3, 4, 5]
]
- * How to declare a constant value in java — Use final keyword and Capital name convention for constant variable
final Float PI = 3.14F
Arithmetic In Java
//
int result = 10 / 3;
double result2 = 10 / (double)3;
System.out.println(result); => 3 (INT/INT=>INT)
System.out.println(result2); => 3.3333333333333335 (CONVERT 3 TO DOUBLE, AND 10 WILL BE CONVERTED TO DBL BY DEFAULT, RETURN A DOUBLE RESULT)
Implicit casting during calculation(only between competible type)
byte < short < int < long < float < double
Java will convert the “smaller/less precise” type into “bigger/more precise” type by default
**If you want the result to be a “smaller” type, you will have to use (type)
to explicitly convert.
- * if you want to convert a string into number, use java wrapper class:
String x = "1"
int y = Integer.parseint(x) + 2
System.out.println(y) => 3
Explore Math Class
int result1 = Math.round(1.1F);
int result2 = (int) Math.ceil(1.1F);
int result3 = (int) Math.floor(1.1F);
int max = Math.max(1,3);
double random = Math.random();
int random2 = (int)(Math.random() * 100);1
2
1
3
0.43817367742176094
20
Explore NumberFormat Class
Numberformat is an abstract class, cannot use the new operator to initiate
String result = NumberFormat.getCurrencyInstance().format(123445.456);
System.out.println(result);
=>$123,445.46String result = NumberFormat.getPercentInstance().format(0.1);
System.out.println(result);
=> 10%
Explore Input in Java
Scanner scanner = new Scanner(System.in);
System.out.print("What is your name:");
String name = scanner.nextLine().trim();
System.out.println("Your name is " + name);
System.out.print("What is your age:");
byte age = scanner.nextByte();
System.out.println("Your age is " + age);=>
What is your name:Elsa Chen
Your name is Elsa Chen
What is your age:35
Your age is 35
This is now for today. Very fundamental basic knowledge of Java. If you know other programming languages, it’s should be very easy to follow.
Ciao Ciao World! Let me go do some Algo problems!