0% found this document useful (0 votes)
5 views18 pages

Java

Uploaded by

edward kariuki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views18 pages

Java

Uploaded by

edward kariuki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Java Introduction

What is Java?
Java is a popular programming language, created in 1995.

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 has a large demand in the current job market
 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

Java Install
Some PCs might have Java already installed.

To check if you have Java installed on a Windows PC, search in the start bar for
Java or type the following in Command Prompt (cmd.exe):
C:\Users\Your Name>java -version

If Java is installed, you will see something like this (depending on version):

java version "11.0.1" 2018-10-16 LTS


Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed
mode)

If you do not have Java installed on your computer, you can download it for free
at oracle.com.

Note: we will write Java code in a text editor. However, it is possible to write
Java in an Integrated Development Environment, such as IntelliJ IDEA,
Netbeans or Eclipse, which are particularly useful when managing larger
collections of Java files.

Setup for Windows


To install Java on Windows:

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
Then, click "OK", and save the settings
5. At last, open Command Prompt (cmd.exe) and type java -version to see
if Java is running on your machine
6. At last, open Command Prompt (cmd.exe) and type java -version to see
if Java is running on your machine
How to install Java step-by-step with images

Step 1

Step 2
Step 3
Step 4

Step 5
Write the following in the command line (cmd.exe):

C:\Users\Your Name>java -version

If Java was successfully installed, you will see something like this (depending on
version):

java version "11.0.1" 2018-10-16 LTS


Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
Java Quickstart
In Java, every application begins with a class name, and that class must match
the filename.

Let's create our first Java file, called Main.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:

Main.java

public class Main {

public static void main(String[] args) {

System.out.println("Hello World");

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

Save the code in Notepad as "Main.java". Open Command Prompt (cmd.exe),


navigate to the directory where you saved your file, and type "javac Main.java":

C:\Users\Your Name>javac Main.java

This 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:

C:\Users\Your Name>java Main

The output should read:

Hello World

Congratulations! You have written and executed your first Java program.
Java Syntax
In the previous chapter, we created a Java file called Main.java, and we used
the following code to print "Hello World" to the screen:

Main.java

public class Main {

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 Main. A class should always start with an uppercase first
letter.

Note: Java is case-sensitive: "MyClass" and "myclass" has 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 example above on your computer, make sure that Java is properly installed:
The output should be:

Hello World

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. Don't worry about the
keywords before and after main. You will get to know them bit by bit while
reading this tutorial.
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");

Note: The curly braces {} marks the beginning and the end of a block of code.

System is a built-in Java class that contains useful members, such as out, which
is short for "output". The println() method, short for "print line", is used to
print a value to the screen (or a file).

Don't worry too much about System, out and println(). Just know that you
need them together to print stuff to the screen.

You should also note that each code statement must end with a semicolon (;).
Java Output / Print
(Print Text /Print Numbers)
Print Text
You learned from the previous chapter that you can use the println() method
to output values or print text in Java:

Example
System.out.println("Hello World!");

You can add as many println() methods as you want. Note that it will add a
new line for each method:

Example
System.out.println("Hello World!");

System.out.println("I am learning Java.");

System.out.println("It is awesome!");

Double Quotes
When you are working with text, it must be wrapped inside double quotations
marks "".

If you forget the double quotes, an error occurs:

Example
System.out.println("This sentence will work!");

System.out.println(This sentence will produce an error);


The Print() Method
There is also a print() method, which is similar to println().

The only difference is that it does not insert a new line at the end of the output:

Example
System.out.print("Hello World! ");

System.out.print("I will print on the same line.");

Note that we add an extra space (after "Hello World!" in the example above),
for better readability.

NB. In this UNIT, we will only use println() as it makes it easier to read the
output of code.

Print Numbers
You can also use the println() method to print numbers.

However, unlike text, we don't put numbers inside double quotes:

Example
System.out.println(3);

System.out.println(358);

System.out.println(50000);

You can also perform mathematical calculations inside the println() method:

Example
System.out.println(3 + 3);

Example
System.out.println(2 * 5);
Java Comments
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
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");

NB: It is up to you which you want to use. Normally, we use // for short
comments, and /* */ for longer.

Java Variables
Java Variables, Java Print Variables,
Java Declare Multiple Variables and
Java Identifiers

Java Variables
Variables are containers for storing data values.

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

 String - stores text, such as "Hello". String values are surrounded by


double quotes
 int - stores integers (whole numbers), without decimals, such as 123 or -
123
 float - stores floating point numbers, with decimals, such as 19.99 or -
19.99
 char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
 boolean - stores values with two states: true or false
Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a value:

Syntax
type variableName = value;

Where type is one of Java's types (such as int or String), and variableName is
the name of the variable (such as x or name). The equal sign is used to
assign values to the variable.

To create a variable that should store text, look at the following example:

Example
Create a variable called name of type String and assign it the value "John":

String name = "John";

System.out.println(name);

To create a variable that should store a number, look at the following example:

Example
Create a variable called myNum of type int and assign it the value 15:

int myNum = 15;

System.out.println(myNum);

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

Example
int myNum;

myNum = 15;

System.out.println(myNum);
Note that if you assign a new value to an existing variable, it will overwrite the
previous value:

Example
Change the value of myNum from 15 to 20:

int myNum = 15;

myNum = 20; // myNum is now 20

System.out.println(myNum);

Final Variables
If you don't want others (or yourself) to overwrite existing values, use
the final keyword (this will declare the variable as "final" or "constant", which
means unchangeable and read-only):

Example
final int myNum = 15;

myNum = 20; // will generate an error: cannot assign a value to a final


variable

Other Types
A demonstration of how to declare variables of other types:

Example
int myNum = 5;

float myFloatNum = 5.99f;

char myLetter = 'D';

boolean myBool = true;


String myText = "Hello";

You will learn more about data types as we progress.

Java Print Variables


Display Variables
The println() method is often used to display variables.

To combine both text and a variable, use the + character:

Example
String name = "John";

System.out.println("Hello " + name);

You can also use the + character to add a variable to another variable:

Example
String firstName = "John ";

String lastName = "Doe";

String fullName = firstName + lastName;

System.out.println(fullName);

For numeric values, the + character works as a mathematical operator (notice


that we use int (integer) variables here):

Example
int x = 5;

int y = 6;

System.out.println(x + y); // Print the value of x + y

From the example above, you can expect:

 x stores the value 5


 y stores the value 6
 Then we use the println() method to display the value of x + y, which
is 11

Java Declare Multiple Variables


Declare Many Variables
To declare more than one variable of the same type, you can use a comma-
separated list:

Example
Instead of writing:

int x = 5;

int y = 6;

int z = 50;

System.out.println(x + y + z);

You can simply write:

int x = 5, y = 6, z = 50;

System.out.println(x + y + z);

One Value to Multiple Variables


You can also assign the same value to multiple variables in one line:

Example
int x, y, z;

x = y = z = 50;

System.out.println(x + y + z);
Java Identifiers
Identifiers
All Java variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).

Note: It is recommended to use descriptive names in order to create


understandable and maintainable code:

Example
// Good

int minutesPerHour = 60;

// OK, but not so easy to understand what m actually is

int m = 60;

The general rules for naming variables are:

 Names can contain letters, digits, underscores, and dollar signs


 Names must begin with a letter
 Names should start with a lowercase letter and it cannot contain
whitespace
 Names can also begin with $ and _ (but we will not use it in this tutorial)
 Names are case sensitive ("myVar" and "myvar" are different variables)
 Reserved words (like Java keywords, such as int or boolean) cannot be
used as names

You might also like