JAVA PROGRAMMING BEGINNER TUTORIAL
JAVA TUTORIAL 1:
To get started, you need to download the Java Development Kit (jdk) and this can be found online by just searching jjdk download or just click and download via the link below (you may choose the download depending on your operating system, i.e windows, linux , mac.etc) :
Understanding the syntax/ Anatomy of Java.
Definition of Terms :
Function: A block of code that performs a function. Eg. function to send emails to people, convert kg to tonnes, etc
Void — reserved keyword
public class Main{
public void main () {
//your code goes here...
}
}
A class: a container of more than one function — contains related functions.
A method: is a function which is part of a class.
Access modifier: a special keyword that determines if other classes and methods can be accessed- Public, private
PascalNamingConvention : The first letter of every word should be uppercase
camelNamingConvention : The first letter of every word should be uppercase except for the first word.
Creating our first Java Project:
I use IntelliJ as my IDE and you can download the community version which is totally free to use via:
If you do not get the community version (free) you can also download via:
Package: we use it to group our classes.
The following is our first “Hello world” function in Java:
package com.codewithlawrence;
public class Main {
public static void main(String [] args) {
System.out.println("Hello World");
}
}
Explaining how JAVA code runs in a nutshell / execution:
2 steps are ususally involved here:
- Compilation: by the Java compiler which comes with the Java Development Kit when downloaded and takes the Java byte code (Main.class) and converts
To test and see this , right click Main.java file and open in terminal. You then type the following in the terminal:
javac Main.java
Then type “dir” if on windows or “ls” if on linux. You may now see what is contained on your working directory for the code.
Main.class is a byte code representation of the Main.java file and is “platform-independent” , i.e can be run anywhere in Mac, windows, linux etc.
Java Runtime Environment (JRE) has a software component called a Java virtual Machne (JVM) which takes our byte code andconverts the code to Native code which an OS , i.e Windows understands. Thus Java is really portable/ platform Independent. We can write a Java program on a windows machine and execute it on any other machine / OS .eg.linux or mac that has a Java Runtime Enviroment. Python and Csharp also have similar architecture.
2. Execution
You may go into the main code directory via the terminal and run the following code just to see and understand how the code is executed via the terminal:
java com.codewithlawrence.Main
Here we invoke the java virtual machine and type the full pass to our main class (com.codewithlawrence.Main) and we get to see “Hello world” printed out in the terminal.
Some Little History and Info. about Java:
Developed by James Gosling in 1995 and later acquired by Oracle in 2010.
Java has almost more than 9 Million developers globally.
Course outline:
- Fundamentals of Java
Types : numbers, booleans
Control flow statements: used to build algorthms
Clean coding
Finding and fixing bugs
2. Object oriented programming: web, mobile and desktop applications
3. Core Java API’s : many of the useful classes in the standard java library.
4. Advanced Features: strings, threads, databases, etc
i.) Types
Variables : temporarily store data in a computer’s memory. It is good practise to use meaningful and descriptive name for variables.
Simple Java code to test out types (integer in this case). Note I have used camelNamingConvention :
package com.codewithlawrence;
public class Main {
public static void main(String [] args) {
int myAge = 35;
int herAge = myAge;
System.out.println(herAge);
}
}
ii.) Primitive and reference types
Primitive types: storing simple values like numbers, characters , boolean values. etc.
Byte — 1 byte of memory — we can store upto 127-128 values
short — 2 bytes of memory— we can store upto 32,000 values
interger — 4 bytes of memory — we can store upto 2 billion values
long — 8 Bytes
float — 4 bytes
double — 8 bytes
char — 2 bytes — single character
boolean — 1 — True/ False
Java Code to define / declare some primitive types: (Note: We cannot use the declarations in other variables, classes , methods., etc elsewhere in the code again since they are reserved keywords)
package com.codewithlawrence;
public class Main {
public static void main(String [] args) {
byte age = 35;
long viewsCount = 3_123_456_789L;
float price = 10.99F;
char letter = 'A';
boolean isEligible = false;
}
}
Reference types: for storing complex objects. When dealing with primitive types, we do not need to allocate memory(it is usually allocated and released by Java runtime enviroment) but when dealing with Reference types, we shall always allocate memory , but we don’t have to release this memory, JRE will handle that.
package com.codewithlawrence;
import java.util.Date;
public class Main {
public static void main(String[] args) {
byte age = 30;
Date now = new Date();
System.out.println(now);
}
}
A “Date” class but in different packages.
Thus, Importance of packages: they create a namespace for our classes.
An object: is an instance of a class.
Instead of typing out the whole of “System.out.println();” you may use the shortcut “sout” and press tab which autocompletes it.
Let us meet on the Java Tutorial 2 as i would not like to make this too long. We will get into: casting, Numbers, strings and arrays ,How to read input from a user and many others.
You can find Youtube videos on how i wrote the code and follow along:
THANK YOU!
My contacts:
Twitter: https://twitter.com/lawrence_kim_?t=IgTw9ewUp1oQoKdcEirS5Q&s=09
LinkedIn: https://www.linkedin.com/in/lawrence-kimutai-6184541ba
My Github: https://github.com/KimutaiLawrence
Fiverr: https://www.fiverr.com/s2/fef0ee42cd
Upwork: https://www.upwork.com/freelancers/~01e0e9da97646e56f2
People per hour: https://www.peopleperhour.com/freelancer/lawrence-kimutai-geospatial-developer-mwjzyjx
Whatsapp Contact: +254759629059
Email: geospatialprime@gmail.com