Lesson 2, Topic 1
In Progress

Java: Variables Review

Yasin Cakal 14 Oct 2021
Lesson Progress
0% Complete

Let’s review the basics of variables in Java!

What is a Boolean Data Type?

The boolean primitive data type is used in Java to represent a value that is either true or false. Remember, the keywords true and false must be lower composed of all lower case letters. Here is an example:

boolean var1 = true;
boolean var2 = false;

What are String Variables?

In Java, a String is an Object that contains multiple characters. This is not a primitive type of data. Strings are constructed by enclosing characters in a pair of double quotes " ". To compare Strings, you must use the equals() method rather than the primitive equality comparator ==. Here is an example:

// String variable message is created.
String message1 = "HELLO";
String message2 = "hello";

// Remember, Java is case sensitive.
System.out.println(message1.equals(message2));

Output:

false

What is an int Data Type?

In Java, integer values are stored in the int datatype. This means that it can store all whole numbers, positive and negative, as well as zero. Here are some examples:

// Positive, Negative and Zero are all integers.
int num1 = 12;
int num2 = 0;
int num3 = -2
// The following is not an integer:
// int num4 = 3.2;

What is the char Data Type?

A single character is stored in a char variable in Java. Single quotes ' ' must be used to enclose the character as follows:

char letter = 'C'; // Case-sensitive

What are Primitive Data Types?

Java’s most basic data types are referred to as primitive data types, and they are imported into your Java programs by default. The following are all the primitive data types:

  • int
  • char
  • boolean
  • byte
  • short
  • long
  • float
  • double

Here are some examples:

int fav_number = 8;
char letter = 'c';
boolean statement = false;
byte x = 11;
short num = 12;
long num_var = 987654321;
float pie = (float)3.14;
double exact_pie = 3.14159265359;

What is Static Typing?

The type of a variable is checked during the compilation process in Java. Static typing is a term that refers to this. It has the advantage of catching errors during the compilation process rather than during execution. Otherwise, the program will fail to compile. Variables must be declared with the appropriate data type. Here are some examples:

// Variable is declared with the correct data type.
int x = 23;

// The following examples will not compile
y = 24; // No data type is given.
String z = 25; // Incorrect data type.

What does the Keyword final do?

If a variable was declared with the final keyword, its value cannot be changed.

Take note that when a variable is declared final, it must be assigned a value. Changes to final variables are not permitted; any attempt will result in an error message.

// Following variable can't be altered!
final int pizza_slices = 8;

What are the Arithmetic Operators in Java?

The following are the basic aritmetic operators in Java that can be used with types int, double and float:

  • addition x + y
  • subtraction x - y
  • multiplication x * y
  • division x / y
  • modulo (remainder) x % y

Here are some examples:

class Arithmetics {
    public static void main(String[] args) {
        int x = 10, y = 3;
        System.out.println(x + y); // addition
        System.out.println(x - y); // subtraction
        System.out.println(x * y); // multiplication
        System.out.println(x / y); // division
        System.out.println(x % y); // modulo
    }
}

Output:

13
7
30
3
1

What are the Comparison Operators in Java?

Two values can be compared using comparison operators. They are supported for primitive data types and return a boolean value of true or false as the result of a comparison. The comparison operators used in Java are as follows:

  • equals ==
  • not equals !=
  • greater than >
  • greater than or equal to >=
  • less than <
  • less than or equal to <=

Here are some examples:

class Comparison {
    public static void main(String[] args) {
        int x = 5, y = 3;
        System.out.println(x == y);
        System.out.println(x != y);
        System.out.println(x > y);
        System.out.println(x >= y);
        System.out.println(x < y);
        System.out.println(x <= y);
    }
}

Output:

false
true
true
true
false
false

What are Shorthand Assignment Operators?

Shorthand assignment operators enable one line of code to change and reassign the value of a variable. They are also known as compound assignment operators as shown below:

  • Shorthand Addition +=
  • Shorthand Subtraction -=
  • Shorthand Multiplication *=
  • Shorthand Division /=
  • Shorthand Modulo %=

Here are some examples:

class Shorthand {
    public static void main(String[] args) {
        int x = 10; 
        
        // See how the value of x changes.
        x += 1; 
        System.out.println(x);
        
        x -= 5;
        System.out.println(x);
        
        x *= 2;
        System.out.println(x);
        
        x /= 4;
        System.out.println(x);
        
        x %= 2;
        System.out.println(x);
    }
}

Output:

11
6
12
3
1

What are the Increment and Decrement Operators?

The increment operator ++ increases the value of a number-based variable by one, while the decrement operator -- decreases its value by one. Here are some examples:

int counter = 0;
counter++; // Now counter = 1
counter--; // counter = 0 again

What is the Order of Operations?

The order of operations determines the order in which an expression with multiple operators is evaluated. Here is the order:

  • 1st : Parentheses ()
  • 2nd : Multiplication * and Division /
  • 3rd : Addition + and Subtraction -

Here are some examples:

class Pemdas {
    public static void main(String[] args) {
        
        // Prints 20
        System.out.println((2 + 2) * 5);
        
        // Prints 12
        System.out.println(2 + 2 * 5);
    }
}
%d bloggers like this: