IT1406 - Java Arrays



  • Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
  • To declare an array, define the variable type with square brackets:

String[] cars;
We have now declared a variable that holds an array of strings. To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly braces:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
To create an array of integers, you could write:

int[] myNum = {10, 20, 30, 40}

IT1406 - Java Operators



  • Operators are used to perform operations on variables and values.
  • In the example below, we use the + operator to add together two values:
Example
int x = 100 + 50;
  • Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:
Example
int sum1 = 100 + 50;        // 150 (100 + 50)
int sum2 = sum1 + 250;      // 400 (150 + 250)
int sum3 = sum2 + sum2;     // 800 (400 + 400)

IT1406 - Java Type Casting


Type casting is when you assign a value of one primitive data type to another type.

In Java, there are two types of casting:

Widening Casting (automatically)

  • converting a smaller type to a larger type size

byte -> short -> char -> int -> long -> float -> double

IT1406 - Java Data Types

As explained in the previous chapter, a variable in Java must be a specified data type:

Example
int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99f;    // Floating point number
char myLetter = 'D';         // Character
boolean myBool = true;       // Boolean
String myText = "Hello";     // String

IT1406 - Java Variables


Variables are containers for storing data values.

In Java, there are different types of variables, for example:


  1. String - stores text, such as "Hello". String values are surrounded by double quotes
  2. int - stores integers (whole numbers), without decimals, such as 123 or -123
  3. float - stores floating point numbers, with decimals, such as 19.99 or -19.99
  4. char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
  5. boolean - stores values with two states: true or false


IT1406 - Java Comments


  • Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code
  • Single-line comments start with two forward slashes (//).
  • Any text between // and the end of the line is ignored by Java (will not be executed).


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

Example
// This is a comment
System.out.println("Hello World");

This example uses a single-line comment at the end of a line of code:

Example
System.out.println("Hello World"); // This is a comment

Java Multi-line Comments

  • Multi-line comments start 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:


Example
/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");

Single or multi-line comments
It is up to you which you want to use. Normally, we use // for short comments, and /* */ for longer.

IT1406 - Java Syntax


In the previous chapter, we created a Java file called MyClass.java, and we used the following code to print "Hello World" to the screen:

MyClass.java
public class MyClass {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

Example explained

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

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

IT1406 - Java Quick start

In Java, every application begins with a class name, and that class must match the filename.

Let's create our first Java file, called MyClass.java, which can be done in any text editor (like Notepad).

The file should contain a "Hello World" message, which is written with the following code:

MyClass.java

public class MyClass {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

IT1406 - Setting up Java

Download Java SDK at Oracle (Follow the link) - https://www.oracle.com/technetwork/java/javase/overview/index.html

After installing java ,  configure as follows

  1. Go to "System Properties" (Can be found on Control Panel > System and Security > System > Advanced System Settings)
  2. Click on the "Environment variables" button under the "Advanced" tab
  3. Then, select the "Path" variable in System variables and click on the "Edit" button
  4. 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
  5. Then, click "OK", and save the settings
  6. At last, open Command Prompt (cmd.exe) and type java -version to see if Java is running on your machine

IT1406 - What is Java?

Java is a popular programming language, created in 1995.
Java was originally developed by James Gosling at Sun Microsystems
It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:

  • Mobile applications (specially Android apps)
  • Desktop applications
  • Web applications
  • Web servers and application servers
  • Games
  • Database connection
  • And much, much more!

Why Use Java?

  • Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
  • It is one of the most popular programming language 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 a huge community support (tens of millions of developers)
  • Java is an object oriented language which 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

IT1406 - What is programming


  • Computer programming is the process of designing and building an executable computer program for accomplishing a specific computing result. 
  • Programming involves tasks such as; analysis, generating algorithms, profiling algorithms' accuracy and resource consumption, and the implementation of algorithms in a chosen programming language