MOOC.fi Java Programming I Part 1
Fundamentals of Java
My first post!
One of the most popular backend languages is Java. As an aspiring Backend developer and having relatively more experience with Python (~3 years), I decided to add Java into my skill list. I looked at many online Java courses like Udemy but MOOC.fi Java Programming seems the most recommended by reviewers, particularly by Reddit. So I accept the challenge! Here is what I learnt in MOOC.fi Java Programming I Part 1.
Getting started with programming
- Code we write is called source code (suprised cat face)
Printing
- System.out.println(), which is Java command for printing value of variable, can be replaced with typing sout + TAB (no space in between)
Reading input
-
Scanner scanner = new Scanner(System.in); and then String first = scanner.nextLine();
-
scanner.nextLine() for strings, scanner.nextInt() for int
Variables
-
“String literal” refers to a string enclosed in programming language’s quotation marks
-
Once a variable’s type has been declared, it cannot be changed (except from int to double but not vice versa to prevent loss of info) unlike the value of the variable which is changeable
-
Java convention of naming things is camelCase like holaBitch, meLoop
-
Integer.valueOf() converts string -> int, .valueOf() method turns string to whatever
//this is better than Integer.valueOf()
int integer = Integer.parseInt(scanner.nextLine());
int integer = Integer.valueOf(scanner.nextLine());
double floatingPoint = Double.valueOf(scanner.nextLine());
//like “TRue” -> true;
boolean trueOrFalse = Boolean.valueOf(scanner.nextLine());
-
Diff between Integer.valueOf(scanner.nextLine) and scanner.nextInt(): https://stackoverflow.com/questions/26586489/integer-parseintscanner-nextline-vs-scanner-nextint
-
Generally, one nextLine() / nextInt() call won’t make much of a difference. If you have a loop or if you are reading lot of data, then using readLine() with parseInt() will be very efficient.
-
When changing value assigned to variable, remember that variable type is stated once only on the initial variable declaration (e.g. int hola = 12, then later hola = 4, no need say int hola =4)
-
Decimal number cannot be represented exactly with float because both the exponent and mantissa must be integers (i.e. floats must be integer(mantissa) multiplied by 2 to the power of integer(exponent))
Calculating with numbers
-
Expression is combination of values that is turned to another value through calculation/evaluation.
-
If one of operands of operation + is a string, the other operand will become a string too. For example,
System.out.println("Four: " + (2 + 2));
// Four: 4
System.out.println("But! Twenty-two: " + 2 + 2);
// But! Twenty-two: 22
- If the dividend or divisor (or both!) of the division is a floating point number, the result is a floating point number as well
double whenDividendIsFloat = 3.0 / 2;
System.out.println(whenDividendIsFloat); // prints 1.5
- An integer can be converted into a floating point number by placing a type-casting operation (double) before it:
int first = 3;
int second = 2;
double result1 = (double) first / second;
System.out.println(result1); // prints 1.5
double result2 = first / (double) second;
System.out.println(result2); // prints 1.5
double result3 = (double) (first / second);
System.out.println(result3); // prints 1.0
- tricky example:
int dividend = 3;
int divisor = 2;
double result = dividend / divisor * 1.0;
System.out.println(result);
// answer is 1.0, not 1.5 bcos dvidied/divisor is executed first and is integer
- can affect the order of operations by using parentheses. Operations within parentheses are performed before those outside them.
Conditional statements and conditional operation
- Some terms explained: Conditional statement begins with keyword if, followed by parentheses. Expression is placed inside that parentheses, which is evaluated when conditional statement is reached. After that parentheses comes the code block in curly brackets, which is exececuted if expression in parentheses is true.
if (true) {
System.out.println("This code is unavoidable!");
-
if, else if (elif in Python), else
-
Cannot compare the equality of strings using two equals signs(==). Use .equals() method like if (input.equals(“hola”))
-
if (!(number > 4)); not-operator(!) in front of parentheses that encases expression
-
Sometimes, parsing of conditional statement stops at first condition that is true. Solution is to implement the most demanding condition first.
Scanner reader = new Scanner(System.in);
int number = Integer.valueOf(reader.nextLine());
// instead of if (number%3==0), implement most demanding condition first to avoid stop of parsing
if (number % 3 == 0 && number % 5 == 0) {
System.out.println("FizzBuzz");
} else if (number % 3 == 0) {
System.out.println("Fizz");
} else if (number % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(number);
}
-
Math library like Math.max(num1,num2) is useful
-
List comprehension System.out.println(str1.equals(str2) ? “Same” : “Different”);