Archi's Academy

GetStarted

GetStarted
Get in touch

Computer Science

Computer programming

Java

Java is a general-purpose, class-based, object-oriented programming language created in 1995 by James Gosling. It is designed for having fewer implementation dependencies. It is a computing platform for application development. Java is fast, secure, and reliable.

Java is used for:

● Mobile applications (especially Android apps)

● Desktop applications

● Web applications

● Web servers and application servers

● Games

● Database connection and much more.

Why Use Java?

● Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)

● It is one of the most popular programming languages in the world

● It is easy to learn and simple to use

● It is open-source and free

● It is secure, fast, and powerful

● It has huge community support (tens of millions of developers)

● Java is an object-oriented language that gives a clear structure to programs and allows code to be reused, lowering development costs

● As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa

In this blog post, I will work you through how to get started programming in Java. To start programming in Java, you need Java installed, some PCs might have Java already installed.

To check if you have Java installed on a Windows PC, navigate to your terminal and execute the command shown in the diagram below.

1.png

If Java is installed, you will see the version of Java installed when the command has been successfully executed. In this case it is (version “11.0.11).

If you do not get the above result about your java version, it means you do not have Java installed on your computer. You can download it for free from here

Note:

(1) I will be writing Java code in an IDE called IntelliJ. However, if you want to code along with me, you can use any IDE or text editor of your choice such as IntelliJ IDEA, NetBeans, Eclipse, or VS code which are particularly useful when managing larger collections of Java files.

(2) After installing Java, there are some steps that must be taken to set it up properly. To set up Java after installing the JDK, follow the below steps:

● Go to "System Properties" (Can be found on Control Panel > System and

2.png

● Security > System > Advanced System Settings)

3.png

● Click on the "Environment variables" button under the "Advanced" tab.

4.png

Then, select the "Path" variable in System variables and click on the "Edit" button.

● Click on the "New" button and add the path where Java is installed, followed by \bin. By default, Java is installed in C:\Program Files\Java\jdk-11.0.1 (If nothing else was specified when you installed it). In that case, you will have to add a new path with C:\Program Files\Java\jdk-11.0.1\bin. Then, click "OK", and save the settings.

5.png

● Open your Command Prompt (cmd.exe) and type java -version to see if Java is running on your machine.

6.png

Quick Start

Every application begins with a class name, and that class must match the name of a file.

Let's create our first Java file, called Main.java, which can be done in any text editor or IDE (like Notepad). I will be using IntelliJ IDE.

This file will print a "Hello World" message while running in the terminal which is written with the following code:

7.png

Don't worry if you don't understand the code above - I will explain it in detail later. For now, focus on how to run the code above.

Save the code as "Main.java". Open your Command Prompt, and navigate to the directory where you saved your file, and type "javac Main.java":

8.png

Running this command will compile your code. If there are no errors in the code, the command prompt will take you to the next line.

Now, type "java Main" to run the file:

9.png

Congratulations! You have written and executed your first Java program.

Java Syntax

Every line of code that runs in Java must be inside a class. In the above example, we named the class Main. A class should always start with an uppercase first letter.

Note: Java is case-sensitive: "MyClass" and "myclass" has a different meaning.

The name of the java file must match the class name. When saving the file, save it using the class name and add ".java" to the end of the filename. To run the code written above on your computer, make sure that Java is properly installed.

The main method

The main() method is required to run the above, and you will see it in every Java program:

Code written inside the main() method will be executed. You will get to know the keywords before and after the main methods.

For now, just remember that every Java program has a class name that must match the filename and that every program must contain the main() method.

System.out.println()

If you are familiar with JavaScript, you know we use the console.log() function to print strings in the browser console, in Java we use the System.out.println() to print stuff to the terminal.

NOTE:

● The System.out.println() is inside the main method.

● Each code statement must end with a semicolon.

Comments in Java

Comments can be used to explain code, and to make it more readable. It can also be used to prevent execution when testing alternative code.

There are two types of comments in Java, which includes:

● Single-line comments start with two forward slashes (//). Any text between // and the end of the line is ignored by Java and will not be executed by the Java compiler.

This example uses a single-line comment before a line of code:

10.png

● Multi-line comment starts with /* and ends with /. Any text between / and */ will be ignored by Java. This example uses a multi-line comment (a comment block) to explain the code:

11.png

NOTE: single-line (//) comments are normally used for short comment, while multi-line comments (/* */) for longer comments.

Variables in Java

We use variables as a container for storing data values.

There are different types of variables in Java, for example:

● We use String - to store text, such as "Hello". String values are surrounded by double quotes.

● We use int – to store integers (whole numbers), without decimals, such as 123 or -123.

● We use float - to store floating-point numbers, with decimals, such as 19.99 or -19.99.

● We use char - to store single characters, such as 'a' or 'B'. Char values are surrounded by single quotes.

● We use boolean – to store values with two states: true or false.

Create/Declare variable in Java

To create a variable in Java, you must specify the type of variable and assign it a value:

Type variableName = variable value.

Type: String, int, boolean char, etc.

VariableName: Can be any name of your choice.

Note: The equal sign is used to assign value to variables.

Example:

To create a variable to store text and number, look at the following example:

12.png

The above example creates a variable of type String and assigns it the value of “Olamide”.

The second variable in the example is of type int and has a value of 10, and the value of these variables is being printed to the screen when executed.

NOTE: You can also declare a variable without assigning the value, and assign the value later.

Example:

29.png

If you assign a new value to an existing variable, it will overwrite the previous value:

Example:

14.png

Final variables

If you are familiar with programming languages like JavaScript, you know that you can declare a constant variable. i.e a variable that can not be changed, in other words, a constant variable can not be assigned a new value once declared. In Java, we use the final keyword to declare a constant variable, if you don't want others (or yourself) to overwrite existing values (this will declare the variable as "final" or "constant", which means unchangeable and read-only).

15.png

Other Types of Variables In Java

Below is an example of other types of variables in Java.

16.png

Data Types in Java

Recall that a variable in Java must be a specified data type:

● Integer (whole number)

● Floating point number

● Character

● Boolean

● String etc.

Data Types are divided into two groups namely:

Primitive Data Types

● Byte

● Short

● Int

● Long

● Float

● Double

● Boolean and

● Char

Non-primitive Data Types

● Strings

● Arrays

● Classes

Primitive data types

A primitive data type specifies the size and type of variable values, and it has no additional methods.

In Java there are eight primitive data types:

17.png

Numbers

Primitive number types are divided into two groups:

Integer types store whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int, and long. Which type you should use depends on the numeric value.

Floating-point types represent numbers with a fractional part, containing one or more decimals. There are two types: float and double.

NOTE: Even though there are many numeric types in Java, the most used for numbers are int (for whole numbers) and double (for floating-point numbers).

However, I will describe them all as you continue to read.

Integer Types

Byte

The byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types to save memory when you are certain that the value will be within -128 and 127.

Example:

27.png

Short

The short data type can store whole numbers from -32768 to 32767.

18.png

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.

19.png

Long

The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L".

20.png

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".

21.png

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".

22.png

NOTE: Use float or double?

The precision of a floating-point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore, it is safer to use double for most calculations.

Scientific Numbers

A floating-point number can also be a scientific number with an "e" to indicate the power of 10.

23.png

Booleans

A boolean data type is declared with the boolean keyword and can only take the values true or false.

24.png

Boolean values are mostly used for conditional testing.

Characters

The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c'.

25.png

Strings

The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes.

26.png

NOTE: The String type is so much used and integrated into Java, that some call it "the special ninth type".

A String in Java is actually a non-primitive data type because it refers to an object. The String object has methods that are used to perform certain operations on strings. Don't worry if you don't understand the term "object" just yet. We will learn more about strings and objects later in a future blog post.

Non-primitive data types

Non-primitive data types are called reference types because they refer to objects.

The main difference between primitive and non-primitive data types are:

28.png

There are a lot of things to cover about the Java Programming Language. In this post, I will stop here and continue in the next blog post. If you do find the blog post interesting, then keep an eye out for the next blog of Java Programming!

archis-trainee

Olamide Jubril Muiz

Tuesday, Aug 24, 2021