Java Variables

Java Variables Course

Engineers From These Top Companies and Universities Trust EXLskills

1M+ Professionals | 100+ Institutions



This is the EXLskills free and open-source Java Variables Micro Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of understanding regarding Java variables and operators.

Is this course FREE?

Yes, this a 100% free course that you can contribute to on GitHub here!

Have more questions?

Feel free to reach out to us via live chat here!

Primitive Data Types

Examples

🙋 Need help? Ask an expert now!

Every variable must have a data type assigned to it. The following code depicts all eight primitive data types and shows how a variable may be associated to it.

DataTypesExample.java
package exlcode;
            
            public class DataTypesExample {
            
              // this declares and initializes all eight primitive data types
              public static int exampleVariableOne = 5;
              public static boolean exampleVariableTwo = true;
              public static char exampleVariableThree = 'c';
              public static byte exampleVariableFour = 20;
              public static short exampleVariableFive = 300;
              public static long exampleVariableSix = 300000000;
              public static float exampleVariableSeven = 3.5f;
              public static double exampleVariableEight = 3.141519;
            
              public static void main(String[] args) {
                // this prints all eight primitive data types
                System.out.println("Integer: " + exampleVariableOne);
                System.out.println("Boolean: " + exampleVariableTwo);
                System.out.println("Character: " + exampleVariableThree);
                System.out.println("Byte: " + exampleVariableFour);
                System.out.println("Short: " + exampleVariableFive);
                System.out.println("Long: " + exampleVariableSix);
                System.out.println("Float: " + exampleVariableSeven);
                System.out.println("Double: " + exampleVariableEight);
              }
            }

The eight primitive data types, byte, short, int, float, double, char and boolean will be covered more in depth later. Let's start by exploring what a data type is. Data types enable us to tell the program what kind of data we are trying to represent and use. Always keep in mind that data type names are extremely case sensitive. "Int" is not the same as "int". The primitive, or fundamental, data types can function as building blocks. There are also larger, non-primitive data types that are made up of smaller components. We will dive into data types by learning the eight primitive data types first.

Edit Me on GitHub!

Application Question

Which one of the following is not a primitive data type?