Java Lesson 1
Java Lesson 1
Java Lesson 1
Introduction
What is Java?
Java is a popular programming language, created in 1995.
It is used for:
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):
If Java is installed, you will see something like this (depending on version):
If you do not have Java installed on your computer, you can download it for free
at oracle.com.
Note: In this tutorial, 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.
Step 2
Step 3 »Step 4 »
Step 4
Step 5 »
Step 5
Write the following in the command line (cmd.exe):
If Java was successfully installed, you will see something like this (depending on
version):
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
System.out.println("Hello World");
Try it Yourself »
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.
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:
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");
Try it Yourself »
Note: The curly braces {} marks the beginning and the end of a block of code.
. . ("Hello World");
Submit Answer »
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).
System.out.println("Hello World");
Try it Yourself »
Example
System.out.println("Hello World"); // This is a comment
Try it Yourself »
Example
/* The code below will print the words Hello World
System.out.println("Hello World");
Try it Yourself »
Submit Answer »
Java Variables
Java Variables
Variables are containers for storing data values.
Syntax
type variable = value;
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);
Try it Yourself »
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:
System.out.println(myNum);
Try it Yourself »
You can also declare a variable without assigning the value, and assign the
value later:
Example
int myNum;
myNum = 15;
System.out.println(myNum);
Try it Yourself »
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);
Try it Yourself »
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;
Display Variables
The println() method is often used to display variables.
Example
String name = "John";
Try it Yourself »
System.out.println(fullName);
Try it Yourself »
Example
int x = 5;
int y = 6;
Try it Yourself »
Example
int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
Try it Yourself »
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;
Try it Yourself »
The general rules for constructing names for variables (unique identifiers) are:
= ;
Submit Answer »
Java Data Types
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 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
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);
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.
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);
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:
Example
float f1 = 35e3f;
double d1 = 12E4d;
System.out.println(f1);
System.out.println(d1);
Try it Yourself »
Booleans
A boolean data type is declared with the boolean keyword and can only take the
values true or false:
Example
boolean isJavaFun = true;
Try it Yourself »
Boolean values are mostly used for conditional testing, which you will learn
more about in a later chapter.
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);
Try it Yourself »
Example
char a = 65, b = 66, c = 67;
System.out.println(a);
System.out.println(b);
System.out.println(c);
Try it Yourself »
Tip: A list of all ASCII values can be found in our ASCII Table Reference.
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);
Try it Yourself »
The String type is so much used and integrated in Java, that some call it "the
special ninth type".
myNum = 9;
myFloatNum = 8.99f;
myLetter = 'A';
myBool = false;
myText = "Hello World";
Submit Answer »