Data types
Variables come in all shapes and sizes. Some are used to store numbers, some are used to store
text and some are used for much more complicated types of data.
The data types to know are:
String (or str or text). Used for a combination of any characters that appear on a
keyboard, such as letters, numbers and symbols.
Character (or char). Used for single letters.
Integer (or int). Used for whole numbers.
Float (or Real). Used for numbers that contain decimal points, or for fractions.
Boolean (or bool). Used where data is restricted to True/False or yes/no options.
Java streams
Featured snippet from the web
A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired
result. ... The features of Java stream are – A stream is not a data structure instead it takes input from the Collections,
Arrays or I/O channels
Conditionals
Java uses boolean variables to evaluate conditions. The boolean values true and false are returned when an
expression is compared or evaluated. For example:
int a = 4;
if (a == 4) {
System.out.println("Ohhh! So a is 4!");
}
Loops in Java
Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions
repeatedly while some condition evaluates to true.
Java provides three ways for executing the loops.
Arrays
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the
array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main method of the "Hello
World!" application. This section discusses arrays in greater detail.
Example:
class ArrayDemo {
public static void main(String[] args) {
// declares an array of integers
int[] anArray;
// allocates memory for 10 integers
anArray = new int[10];
// initialize first element
anArray[0] = 100;
// initialize second element
anArray[1] = 200;
// and so forth
anArray[2] = 300;
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;
System.out.println("Element at index 0: "
+ anArray[0]);
System.out.println("Element at index 1: "
+ anArray[1]);
System.out.println("Element at index 2: "
+ anArray[2]);
System.out.println("Element at index 3: "
+ anArray[3]);
System.out.println("Element at index 4: "
+ anArray[4]);
System.out.println("Element at index 5: "
+ anArray[5]);
System.out.println("Element at index 6: "
+ anArray[6]);
System.out.println("Element at index 7: "
+ anArray[7]);
System.out.println("Element at index 8: "
+ anArray[8]);
System.out.println("Element at index 9: "
+ anArray[9]);
}
}
The output from this program is:
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000