String Concatenation
The +
operator can be used between strings to combine them. This is called concatenation:
Example
String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);
The +
operator can be used between strings to combine them. This is called concatenation:
String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);
Strings are used for storing text.
A String
variable contains a collection of characters surrounded by double quotes:
Create a variable of type String
and assign it a value:
String greeting = "Hello";
Operators are used to perform operations on variables and values.
In the example below, we use the +
operator to add together two values:
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:
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
Java divides the operators into the following groups:
Type casting is when you assign a value of one primitive data type to another type.
In Java, there are two types of casting:
byte
-> short
-> char
-> int
-> long
-> float
-> double
double
-> float
-> long
-> int
-> char
-> short
-> byte
Non-primitive data types are called reference types because they refer to objects.
The main differences between primitive and non-primitive data types are:
String
).int
), while non-primitive types typically starts with an uppercase letter (like String
).null
.Examples of non-primitive types are Strings, Arrays, Classes etc. You will learn more about these in a later article. Continue reading Java Non-Primitive Data Types
Here’s a real-life example of using different data types, to calculate and output the total cost of a number of items:
// Create variables of different data types
int items = 50;
float costPerItem = 9.99f;
float totalCost = items * costPerItem;
char currency = '$';
// Print variables
System.out.println("Number of items: " + items);
System.out.println("Cost per item: " + costPerItem + currency);
System.out.println("Total cost = " + totalCost + currency);
The char
data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘c’:
char myGrade = 'B';
System.out.println(myGrade);
Alternatively, if you are familiar with ASCII values, you can use those to display certain characters: Continue reading Java Characters
Very often in programming, you will need a data type that can only have one of two values, like:
For this, Java has a boolean
data type, which can only take the values true
or false
: Continue reading Java Boolean Data Types
Primitive number types are divided into two groups:
Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte
, short
, int
and long
. Which type you should use, depends on the numeric value.
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.
As explained in the previous chapter, a variable in Java must be a specified data type:
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