Monday, 7 September 2015

Java – Basic Syntax

Every Java program is made up of a collection of objects that can communicate to each other. Below is a brief description of what the fundamental parts of a Java program do.
Class – Classes contain Variables and Methods. Think of a Variable as something that can hold a value. A class is a blueprint for creating Objects.
Methods – A Method, also known as a function, is like a behavior. A class can contain lots of Methods. Within each method logic, which is just code that will be executed when the Method is called.
Object – An Object is an instance of a class. When Objects are created they are using the information created from a particular Class.
Instance Variables – Each object that is created has a unique set of Instance Variables. These variables come from the class that the object was instantiated from.

Java Basic Syntax:

When programming it is good to keep naming conventions universal across all projects. These conventions are good practice and should be followed at all times, however are not syntactically incorrect if done differently.
Class Names – The first letter in all class names should be uppercase. If several words are used for a class name, each inner words first letter should be uppercase. This is also known as camel case. (Example: MyAwesomeClass)
Method Names – The first letter in all method names should be lowercase. If several words are used for the methods name, each inner word’s first letter should be uppercase. (Example: myAwesomeMethod).

The following naming conventions will cause compile time errors if incorrect and must be strictly followed.
Case Sensitivity – Java is case sensitive. This means that example and Example are completely different as far as Java is concerned.
Program File Names – The name of the program file should exactly match the Class name. When saving a file, you should always save it as the class name, followed by the extension (.java). (Example: If you have a class called ExampleClass, you would save this file as ExampleClass.java).

Let’s briefly look at how a program knows where to start running. What happens if our program has 10 different classes? How does it know where to start?
public static void main(String[] args) – This is a method that every Java application must implement. There can only ever be one of these methods inside a Java project, and it is the first method called when you program is executed. In later tutorials you will have a better understanding of what exactly each part of that method statement means.     
          
Modifiers:
In Java it is possible to place modifiers on classes, methods, variables, etc. There are two main types of modifiers.
                Access Modifiers: default, public, protected, private.
                Non-access Modifiers: final, abstract, strictfp.
In the next tutorial we will look into more detail of what these modifiers mean.

Variables:
Here are some of the main variable types inside of Java. These variable types are different from data types, these are to do with the scope (who can access) of the variables.
                Local Variables
                Class Variables (static variables)
                Instance Variables (non-static variables)
  
Comments
Java allows for single line and multi-line comments, which are very similar to c++. Any characters that are placed inside a comment are ignored by the compiler and will not be executed at runtime. Comments are a good way to explain how parts of your code work and make it more readable. Below are examples of single and multi-line comments.


Java - Introduction

Introduction:

The ‘Java Programming Language’ is a general-purpose computer programming language. It is classified as a high-level language because of its automated ‘garbage collection’ of objects and automatic memory management.

Java is also a statically typed programming language. This means that unlike dynamically typed languages that do type checking at run time, Java does it at compile-time. Having errors checked at compile time allows for a lot of bugs to get caught during early stages of development.
Being a statically typed language means that data types must be explicitly declared before run time. In comparison, a dynamically typed language such as JavaScript does not require you to specify the data types for your variables because they are determined at run time.

Java is one of the most popular programming languages in the world, powering over 3 billion devices. Java is able to run on all platforms that support Java without needing to recompile. Java’s “write once, and run anywhere” model is why it’s a popular choice for developers who want to create applications that will be run on multiple platforms, without having to adapt their code implementations for each individual platform.

Javas features:

Object Oriented: Everything inside a Java program is an object. This makes Java easily extendible because it is object based.

Simple: Java is an object oriented programming language (OOP). Knowing the basic concepts of OOP programming would make Java easy to learn.

Cross-platform: Java programs are able to run on any devices that support Java without needing to recompile for each device. It is able to do this because it has no implementation dependent aspects. When Java code is compiled, it is made into platform independent byte code which can then be interpreted by any platform that has a Java Virtual Machine (JVM) installed.

Multithreaded: Programming multithreaded applications using Java allows for programs to execute many task simultaneously. This allows developers to make the most of the user’s systems resources to provide the user with a smooth, lag free experience.

Error checking: Javas is a statically typed language. The compiler will attempt to determine the types of everything and make sure that all the code is type safe, and will give errors at compile time if it detects a problem.
The compiler is unable to see the run-time information which can sometimes cause it to produce errors when in fact the code will execute without error at runtime. This will be discussed more in later tutorials.

Security: Java programs use authentication techniques that are based on public key encryption. This allows for virus-free, tamper-free applications.

Audience:
These tutorials are designed to be worked through systematically. This course has tutorials that both beginners and seasoned programmers will find useful. If this is your first time learning a programming language we recommend that you start from the beginning and don’t skip over tutorials, as you will miss some important concepts that will be needed in later tutorials.

Prerequisites – none

Assumed Knowledge:
Basic knowledge of how a computer works, and what a program is. Understanding basic concepts of how programming languages are used.

Please note that these tutorials will be written for windows operating system. All of the concepts will be identical to that of linux and IOS apart from the environment setup. It should be easily translatable if you are well accustomed to your OS.

Java – Environment Setup

JRE & JDK environment setup:

Before we can start creating awesome java programs we first need to install the Java Development Toolkit (JDK). You can find this on the main download page provided by oracle (here), or by doing a quick google search for “Install JDK latest version”.

Once you have downloaded the JDK, run the executable and follow the installation steps.
I recommend that you specify the JDK installation path as something easy to remember because we will be needing it again in a later step. Use a path such as ‘C:\Java\jdk18’. When the installer prompts you to install the Java Runtime Environment (JRE) install it so a path name similar to the JDK, such as C:\Java\jre8.

Once the installation is complete, open up your command prompt. You can does this by searching for cmd in the windows start menu.
To verify that Java is in our environment type the following command ‘java –version’. This will display the current version of Java installed on your machine and the JRE build. If you see these two things it means that the installation was a success.

If your computer couldn’t locate Java try reinstalling the JDK.

We are almost there, we just need to set one more environment variable. This step is very important!
On windows open ‘advanced system settings’. An easy way to do this is by searching for ‘view advanced system settings’ in the start menu. Select the ‘Advanced’ tab and press the button in the bottom called ‘Environment Variables’.

Now we need to add a new system variable with the following details;
-          Variable name: JAVA_HOME
-          Variable value: C:\Java\jdk18

Please note that your ‘Variable value’ is the location of the JDK that we installed earlier.
Press OK, and open up a new command prompt (this is required). Type ‘echo %JAVA_HOME%’. After doing this you’re done.

Congratulations, you have successfully set up your Java Runtime Environment (JRE) and Java Development Kit (JDK) environment.
Don’t worry about trying to memorize all of these steps as you shouldn't need to do it that often. Although it doesn't hurt to know how to do it.

IDE Installation:

For these tutorials we will be using a very popular Integrated Development Environment (IDE) called NetBeans. NetBeans is created by Oracle, the people who created Java, so it is an excellent IDE to begin with, and isn’t as cluttered as others. If you have your own preference of IDE, such as Eclipse or IntelliJ.

Firstly we need to download NetBeans, a quick google search of ‘NetBeans IDE download’ will allow you to locate a place to download the IDE from. Or you can just click here.
You will be presented with different versions of NetBeans to install, select ‘Java SE’, which stands for Java standard Edition, the file should be around 90MB in size.

After the download completes, run the installer by double clicking the .exe extension.
The installer will guide you through installing NetBeans on your computer.
 If you followed the previous tutorial where we installed the JRE and JDK runtime environments to the non-default path, you may need to specify the path which you did install them to if prompted by the NetBeans installer. The file path from the previous tutorial looked something like this, C:\Java\jdk18.

When asked for a location to install NetBeans you may choose a custom path, or leave it as the default.
Before the installation begins you will be presented with a summary page. Check this page to verify that all the components listed to be installed are correct.

We recommend that you select the ‘Check for Updates’ checkbox so that NetBeans will automatically check for future update releases.

Click Install to begin the installation.

After the setup is complete, click Finish.

Congratulations on completing all the tedious boring stuff. If all went well you are now ready to move onto the next tutorial and start writing your very own computer programs. Wooo!