Learn Java With Me: Day 2
Day one we discussed learn the definition and purpose of Java and how to get it installed and connected to GitHub. On today we will cover some basics of Java including Naming Conventions, Data Types and Control Flow . But before we get into all the juicy java topics, we have to write our first lines of java code “Hello World” !
Every line of code that runs in Java must be inside a class
. A class should always start with an uppercase first letter.
The main Method
The main()
method is required and you will see it in every Java program:
public static void main(String[] args)
Any code inside the main()
method will be executed.
For now, just remember that every Java program has a class
name which must match the filename, and that every program must contain the main()
method.
System.out.println()
Inside the main()
method, we can use the println()
method to print a line of text to the screen:
public static void main(String[] args) {
System.out.println("Hello World");
}
It is important to understand the flow of Java code.
The flow of control
defines the order in which our code is executed by Java.
This module will explore how Java controls the order our code will be executed in, as well as scope and how it differs from classes, methods, and blocks of code.
Flow of Control
In Java, code execution will begin in the main method, this method can call other methods, create variables, etc.
If you try to run your code and get the error “main method not found” it is because you have either not declared the main method, or not declared it correctly.
The main method should be declared like this:
public static void main(String[] args) {}
Code execution starts in the main method and the first line is called followed by the second, then the third, and so on.
So if we had the following code:
public static void method1() {
System.out.println("Hello");
}public static void method2() {
System.out.println("World");
}public static String method3() {
return "!";
}public static void main(String[] args) {
method1();
method2();
System.out.println(method3());
}
- It would start in the main method, execute the first line, which is a method call to the method called method1().
- The code would then go into method1() and execute the first line of that method, which is to print “Hello” to the console.
- After that line is executed, the method has no more lines to execute; so Java goes back to the main method and executes the next line of code, which in this case is a method call to method2().
- Java will now go into method2() and execute the first line of that code, which is to print “World” to the console.
Once again, after that code of line has been executed the method has no more code to execute, so Java goes back to the main method. - Java will now execute the next line of code in the main method, which in this case is to print whatever is returned by method3() to the console.
So Java will first go into method3() to get the value to be printed, and the first line of code in method3() will be executed which is to return the value “!”.
Whenever the return keyword is used, no more code within that method will be executed, anything after the return statement will be unreachable. - Now that method3() has returned the value “!” for us, Java can print it to the console.
- This gives us the following output:
Hello World !
Each word is printed on a new line because we called the System.out.println() function to print to console instead of the System.out.print() function.
Naming Conventions in Java
Every language has it rules and these are Java’s. As we continue to code in Java these conventions will start to make more sense.
Data Types in Java
Data types are divided into two groups:
- Primitive data types — includes
byte
,short
,int
,long
,float
,double
,boolean
andchar
- Non-primitive data types — such as String, Arrays and Classes
Byte
The byte
data type can store whole numbers from -128 to 127.
Short
The short
data type can store whole numbers from -32768 to 32767:
Int
The int
data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int
data type is the preferred data type when we create variables with a numeric value.
Long
The long
data type can store whole numbers from -9223372036854775808 to 9223372036854775807.
Floating Point Types
You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.
Float
The float
data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "f".
Double
The double
data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a "d".
Booleans
A boolean data type is declared with the boolean
keyword and can only take the values true
or false
Characters
The char
data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c'.
Strings
The String
data type is used to store a sequence of characters (text). String values must be surrounded by double quotes
We covered a lot and now have a basic understanding of Java and some basics concepts in Java. Tomorrow we will cover Operators and conditionals.
If you made it this far thanks for learning with me. And as always
Love, Peace and Code ❤