Introduction to Java
What is Java?
Java is a popular programming language, developed in 1995.
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!
2
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 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.
It has huge community support.
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.
3
Java Installation
If you do not have Java installed on your computer, you can download it for free at
oracle.com.
4
Installation on Windows
5
Java
In Java, every application begins with a class name, and that class must match the
filename. Let's open our first Java file, called Main.java, which can be done in any text
editor (like Notepad). The file contains a "Hello World" message, which is written with the
following code:
6
Java
7
Example explanation
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.
8
Java Output / Print
You can add println() method several times. Note that it will add a new line for each
method:
When you are working with text, it must be wrapped inside double quotations marks "".
If you forget the double quotes, an error occurs.
9
Java Output / Print
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:
10
Print Numbers
You can also use the println() method to print numbers.
However, unlike text, we don't put numbers inside double quotes:
You can also perform mathematical calculations inside the println() method: println(2*5);
11
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:
12
Java 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:
13
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
14
Java variables
To make variable, you must specify the type and assign it a 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.
15
Java variables
Note that if you assign a new value to an existing variable, it will overwrite the previous
value:
16
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):
17
Other types
A demonstration of how to declare variables of other types:
18
Concatination
To combine both text and a variable, use the + character:
For numeric values, the + character works as a mathematical operator (notice that we use
int (integer) variables here):
19
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 make
understandable and maintainable code:
20
Identifiers
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 cannot contain whitespace.
-Names can also begin with $ and _ .
-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
21
Data types
Data types are divided into two groups: primitive and non-primitive.
Primitive data types - includes byte, short, int, long, float, double, boolean and char.
Non-primitive data types - such as String, Arrays and Classes.
22
Primitive Data Types
A primitive data type specifies the size and type of variable values, and it has no
additional methods.There are eight primitive data types in Java:
23
Boolean Data Type
Very often in programming, you will need a data type that can only have one of two
values, like:
YES / NO
ON / OFF
TRUE / FALSE
For this, Java has a boolean data type, which can only take the values true or false:
24
Characters
The char data type is used to store a single character. The character must be surrounded
by single quotes, like 'A' or 'c':
The String data type is used to store a sequence of characters (text). String values must be
surrounded by double quotes:
25
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:
Primitive types are predefined (already defined) in Java. Non-primitive types are made by
the programmer and is not defined by Java (except for String).
Non-primitive types can be used to call methods to perform certain operations, while
primitive types cannot.
A primitive type always has a value, while non-primitive types can be null.
A primitive type starts with a lowercase letter, while non-primitive types starts with an
uppercase letter.
26
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:
27
Widening casting example
Widening casting is done automatically when passing a smaller size type to a larger size
type:
28
Narrowing casting example
Narrowing casting must be done manually by placing the type in parentheses () in front of
the value:
29
Java Operators
Operators are used to perform operations on variables and values. Java divides the
operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
30
Arithmetic Operators
31
Assignment Operators
32
Comparison Operators
33
Logical Operators
34
Used links
1)https://www.w3schools.com/java/java_getstarted.asp
35