Learning Java - 01
This is a summary of what I learnt in Chapter One of Core Java for the Impatient.
They are also pointers for topics to research more about as you try to learn Java.
Comments
There are three types of code comments in Java:
- Single line comments
// Every thing from here till the end of this line is ignored by the compiler.
- Multi line comments
/*
Once you start writing multiple single line comments it would be best to switch to multi line because they reduce
repetition.
*/
- Documentation comments
/**
Once comments become manuals rather than notes to your future self, this is your best buddy.
*/
Compilation
Java is a compiled language, which implies that there is a compilation process. It is a multi-stage process that involves:
- Converting the source code stored in
.java
files into bytecode (a platform independent intermediate language format that can be run anywhere there’s a Java Virtual Machine) using a command like this:
javac path/to/srcFile.java
The generated bytecode is stored in .class
files.
- The bytecode is then run in the Java Virtual Machine (a piece of software that can convert bytecode into platform specific machine code) using the command:
java path/to/byteFile # Notice that there are no extensions at the end of the path.
If you have a JDK installed and you just want to play around with some Java, run the command jshell
on your terminal. It opens a REPL interface that encourages quick hacking.
Types
-
Java is object oriented but there are variable types that are not objects. These are:
byte
,short
,int
,long
,float
,double
,char
andboolean
-
Every primitive has a corresponding object type so there are:
Byte
,Short
,Integer
,Long
,Float
,Double
,Character
,Boolean
. -
For 90% of your Java usage you would most likely be using:
int
andInteger
, a littlelong
andLong
,double
andDouble
, andboolean
andBoolean
, -
long
values must be suffixed withL
likevar humansOnEarth = 8_000_000_000L; // The underscores are to improve readability they are treated just like comments.
-
float
values must be suffixed withF
likevar pi = 3.142F; // Without the F it becomes a double
-
char
values must be within single quotes.var j = 'J'; // Double quotes makes them strings.
-
Strings are objects of class
String
which means they are a non-primitve type.var name = "Micah Asowata"; // type is String.
Variables
-
There are several ways to declare and initialize a variable
- Statically set the type.
int zero = 0;
- Infer the type.
var zero = 0;
- Declare now, initialize later.
int one; one = 1;
-
A variable cannot be used before it is initialized. It is a compile time error.
-
Scoping rules work as expected.
-
The
final
keyword makes a variable constant which means that its value cannot be changed after it is initialized.final int BMW_M5_HP = 727;
-
Identifiers can contain any letter and digit (non-Latin letters are accepted) and the symbols:
$
and_
.$
is generally used for auto generated code while_
can stand alone as an identifier. -
Identifiers cannot have spaces and must not be keywords.
-
Identifiers are conventionally written in
camelCase
Casting and Conversion
-
When converting from smaller number to larger number the conversion is implicit because no information would be lost.
// int -> double // short -> int // byte -> short
-
When information can be lost types have to be casted.
var x = (char) 75; // casting from int to char. should produce K
-
Converting between numbers and strings requires use of their corresponding class.
// String to int var iage = Integer.parseInt("42"); // int to String var sage = Integer.toString(iage);
Arithemetic Operators
-
+
,-
,*
,/
,%
work as expected. -
When an operation happens between two numeric types:
- If one of them is a
double
the other is converted and the result is adouble
- If one of them is a
float
the other is converted and the result is afloat
- If one of them is a
long
the other is converted and the result is along
- If one of them is a
-
Use the
Math
class for any calculation that you care about.System.out.println(Math.pow(2, 2)); // prints 4.
-
When you care deeply about the accuracy of a calculation more than performance of the operation use
BigInteger
andBigDecimal
classes
Standard I/0
-
The
System
class handles standard outputSystem.out.println("Hello, World!"); // outputs Hello, World! to the terminal
-
To read from standard input
// import java.util.Scanner; var in = new Scanner(System.in); System.out.println("How old are you?"); var name = in.nextInt(); in.close();
Control flow
-
if
works as usual.if (x != 0) { //... } // Condition must be a boolean
-
switch
works as usual. -
for
,while
anddo/while
loops work as expected. -
There is an enchanced
for
loop that simplifies thefor
loop structure for easier readability.int[] ages = {2, 5, 8, 19}; for (int age: ages) { age += age; }
Arrays and ArrayLists
-
Arrays are sequences of items of a single type, whose length is known at compile time. They are created with the syntax:
String[] names = {"Bill", "Joy"}
-
Use an
ArrayList
to create arrays that have dynamic lengths.var engineers = new ArrayList<String>(); engineers.add("Bill Joy"); engineers.add("Steve Jobs"); engineers.remove("Steve Jobs");
-
To create an array list of a primitive type use the corresponding class
var ages = new ArrayList<Integer>(); ages.add(42)
-
Variable arguments are basically just arrays.
public static void main(String... args) { }
Methods
- Methods are functions that describe what an object of a class can do.
public static void main(String[] args) { }