Java Else

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax

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
}

Java If … Else

Java Conditions and If Statements

You already know that Java supports the usual logical conditions from mathematics:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

Java has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

Continue reading Java If … Else

Java Booleans

Java Booleans

Very often, in programming, you will need a data type that can only have one of two values, like:

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, Java has a boolean data type, which can store true or false values.


Continue reading Java Booleans

Java Math

The Java Math class has many methods that allows you to perform mathematical tasks on numbers.


Math.max(x,y)

The Math.max(x,y) method can be used to find the highest value of x and y:

Example

Math.max(5, 10);

 

Continue reading Java Math

Java Special Characters

Strings – Special Characters

Because strings must be written within quotes, Java will misunderstand this string, and generate an error:

String txt = "We are the so-called "Vikings" from the north.";

The solution to avoid this problem, is to use the backslash escape character. Continue reading Java Special Characters

Java Numbers and Strings

Adding Numbers and Strings

WARNING!

Java uses the + operator for both addition and concatenation.

Numbers are added. Strings are concatenated.

If you add two numbers, the result will be a number:

Example

int x = 10;
int y = 20;
int z = x + y;  // z will be 30 (an integer/number)

If you add two strings, the result will be a string concatenation : Continue reading Java Numbers and Strings

Java String Concatenation

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);

 

Continue reading Java String Concatenation

Java Strings

Java Strings

Strings are used for storing text.

A String variable contains a collection of characters surrounded by double quotes:

Example

Create a variable of type String and assign it a value:

String greeting = "Hello";

 

Continue reading Java Strings

Java Operators

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)
int sum2 = sum1 + 250;      // 400 (150 + 250)
int sum3 = sum2 + sum2;     // 800 (400 + 400)

 

Java divides the operators into the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

Continue reading Java Operators

Java Type Casting

Java Type Casting

Type casting is when you assign a value of one primitive data type to another type.

In Java, there are two types of casting:

  • Widening Casting (automatically) – converting a smaller type to a larger type size
    byte -> short -> char -> int -> long -> float -> double
  • Narrowing Casting (manually) – converting a larger type to a smaller size type
    double -> float -> long -> int -> char -> short -> byte

Continue reading Java Type Casting