Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest
docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
docker exec -it c8cfa46e8843 bash
System.out.print("I will print on the same line.");
System.out.println("Hello World!");
Scanner input=new Scanner(System.in);
float marks =input.nextFloat();
// This is a comment
/* The code below will print the words Hello World
to the screen, and it is amazing */
type variableName = value;
final int myNum = 15;
| Data Type | Size | Description |
|---|---|---|
byte |
1 byte | Stores whole numbers from -128 to 127 |
short |
2 bytes | Stores whole numbers from -32,768 to 32,767 |
int |
4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long |
8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
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 |
boolean |
1 bit | Stores true or false values |
char |
2 bytes | Stores a single character/letter or ASCII values |
byte myNum = 100;
short myNum = 5000;
int myNum = 100000;
long myNum = 15000000000L;
float myNum = 5.75f;
double d1 = 12E4d;
boolean isJavaFun = true;
char myGrade = 'B';
String greeting = "Hello World";
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
double myDouble = 9.78d;
int myInt = (int) myDouble; // Manual casting: double to int
| Operator | Name | Description | Example |
|---|---|---|---|
| + | Addition | Adds together two values | x + y |
| - | Subtraction | Subtracts one value from another | x - y |
| * | Multiplication | Multiplies two values | x * y |
| / | Division | Divides one value by another | x / y |
| % | Modulus | Returns the division remainder | x % y |
| ++ | Increment | Increases the value of a variable by 1 | ++x |
| -- | Decrement | Decreases the value of a variable by 1 | --x |
| Operator | Example | Same As |
|---|---|---|
| = | x = 5 | x = 5 |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| %= | x %= 3 | x = x % 3 |
| &= | x &= 3 | x = x & 3 |
| |= | x |= 3 | x = x | 3 |
| ^= | x ^= 3 | x = x ^ 3 |
| >>= | x >>= 3 | x = x >> 3 |
| <<= | x <<= 3 | x = x << 3 |
| Operator | Name | Example |
|---|---|---|
| == | Equal to | x == y |
| != | Not equal | x != y |
| > | Greater than | x > y |
| < | Less than | x < y |
| >= | Greater than or equal to | x >= y |
| <= | Less than or equal to | x <= y |
| Operator | Name | Description | Example |
|---|---|---|---|
| && | Logical and | Returns true if both statements are true | x < 5 && x < 10 |
| || | Logical or | Returns true if one of the statements is true | x < 5 || x < 4 |
| ! | Logical not | Reverse the result, returns false if the result is true | !(x < 5 && x < 10) |
String greeting = "Hello";
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length());
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7
System.out.println("Doe" + " " + "John");
System.out.println("Doe" + " " +.concat("John"));
| Escape character | Result | Description |
|---|---|---|
| \' | ' | Single quote |
| \" | " | Double quote |
| \\ | \ | Backslash |
| Code | Result | |
|---|---|---|
| \n | New Line | |
| \r | Carriage Return | |
| \t | Tab | |
| \b | Backspace | |
| \f | Form Feed |
Math.max(5, 10);
Math.min(5, 10);
Math.sqrt(64);
Math.abs(-4.7);
Math.random();
if (condition) {
// block of code to be executed if the condition is true
}
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
variable = (condition) ? expressionTrue : expressionFalse;
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
while (condition) {
// code block to be executed
}
do {
// code block to be executed
}
while (condition);
for (int i = 0; i < 5; i++) {
}
for (type variableName : arrayName) {
// code block to be executed
}
break;
continue;
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
public class Main {
static void myMethod() {
// code to be executed
}
}
int myMethod(int x)
float myMethod(float x)
double myMethod(double x, double y)