Java Notes 2020
Java Notes 2020
Java Notes 2020
Java is used to develop mobile apps, web apps, desktop apps, games and much
more.
What is Java?
Java is a popular programming language, created in 1995.
It is used for:
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 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
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.
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
System.out.println("Hello World");
Run example »
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.
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:
Go to the Get Started Chapter for how to install Java. The output should be:
Any code inside the main() method will be executed. You don't have to
understand 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:
System.out.println("Hello World");
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.
Any text between // and the end of the line is ignored by Java (will not be
executed).
Example
// This is a comment
System.out.println("Hello World");
Run example »
Example
/* The code below will print the words Hello World
System.out.println("Hello World");
Example
System.out.println("Hello World"); // This is a comment
Run example »
ava Variables
Variables are containers for storing data values.
Syntax
type variable = value;
Where type is one of Java's types (such as int or String), and variable 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":
System.out.println(name);
o 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:
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:
System.out.println(myNum);
Final Variables
However, you can add the final keyword 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):
Example
final int myNum = 15;
Other Types
A demonstration of how to declare variables of other types:
Example
int myNum = 5;
float myFloatNum = 5.99f;
Display Variables
The println() method is often used to display variables.
Example
String name = "John";
Run example »
You can also use the + character to add a variable to another variable:
Example
String firstName = "John ";
System.out.println(fullName);
Example
int x = 5;
int y = 6;
Example
int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
Run example »
Java Identifiers
All Java variables must be identified with unique names.
Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).
Example
// Good
int m = 60;
Run example »
The general rules for constructing names for variables (unique identifiers) are:
Example
int myNum = 5; // Integer (whole number)
Floating point types represents numbers with a fractional part, containing one
or more decimals. There are two types: float and double.
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,
we 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
byte myNum = 100;
System.out.println(myNum);
Short
The short data type can store whole numbers from -32768 to 32767:
Example
short myNum = 5000;
System.out.println(myNum);
Example
int myNum = 100000;
System.out.println(myNum);
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":
Example
long myNum = 15000000000L;
System.out.println(myNum);
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":
Example
float myNum = 5.75f;
System.out.println(myNum);
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":
Example
double myNum = 19.99d;
System.out.println(myNum);
public class MyClass {
public static void main(String[] args) {
double myNum = 19.99d;
System.out.println(myNum);
}
}
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.
Booleans
A boolean data type is declared with the boolean keyword and can only take the
values true or false:
Example
boolean isJavaFun = true;
Run example »
OUTPUT
TRUE
FALSE
Characters
The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':
Example
char myGrade = 'B';
System.out.println(myGrade)
ANSWER WILL BE B
Example
char a = 65, b = 66, c = 67;
System.out.println(a);
System.out.println(b);
System.out.println(c);
Strings
The String data type is used to store a sequence of characters (text). String
values must be surrounded by double quotes:
Example
String greeting = "Hello World";
System.out.println(greeting);
The main difference between primitive and non-primitive data types are:
Example
public class MyClass {
int myInt = 9;
System.out.println(myInt); // Outputs 9
System.out.println(myInt);
System.out.println(myDouble);
}
}
OUTPUT WILL BE
9.0
Narrowing Casting
Narrowing casting must be done manually by placing the type in parentheses in
front of the value:
Example
public class MyClass {
System.out.println(myInt); // Outputs 9
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)
OUTPUT WILL BE
150
400
800
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
OUTPUT WILL BE 8
public class MyClass {
public static void main(String[] args) {
int x = 5;
int y = 3;
System.out.println(x - y);
}
}
OUTPUT WILL BE 2
OUTPUT WILL BE 15
OUTPUT WILL BE 4
OUTPUT WILL BE 6
OUTPUT WILL BE 4
In the example below, we use the assignment operator (=) to assign the
value 10 to a variable called x:
Example
int x = 10;