Java Variables And Data Types Introduction

If you have a good and effective understanding of Java variables and data types then you can make your programming skills excellent. In this chapter, we will introduce basic java variables and data types with easy understanding code examples to you.

1. Variable Description In Java.

  1. Variables stand for memory space where the data is saved.
  2. A variable consists of three parts which are variable name, variable value, and variable data type.
  3. Name is just used as a symbol to represent the memory space where the data is stored.
  4. Value is certainly the information to be stored in that memory space.
  5. The data type determines the type of value that can be stored.
  6. For example, the int type variable can only store an int type value. If you store a float type value, then the value may be changed to int type automatically.
  7. This is a java variable definition example int firstNumber = 3.
  8. In this example, the variable name is firstNumber, the variable value data type is int,  and the variable’s value is 3.
  9. You can store integer values in the int data type. Integer values include both zero, positive number, and negative numbers.

2. Java Data Type Definition.

  1. Java data type determines what kind of information can be stored in the variable.
  2. There are reference and primitive data types in java.
  3. The primitive data type is used as a basic data type that is not made up of other data types, but reference types are made up of other types.
  4. Reference types include classes or arrays. We will show you what is class and how to initiate objects from classes and use a variable to refer to it.
  5. If you want to store a group of the same data type object then you can use Arrays. We will introduce more about arrays in other posts.
  6. We just introduce primitive types in this post. There are 8 primitive types in java, each primitive data type can be used to store a special type of data.
  7. Each primitive data type also has a value range limit. If you store data values outside of the primitive data type range then an error will occur.
  8. The below table list all java primitive data types, and required memory amount, and the value range for each primitive data type.
    Data Type Size In Bits Value Range
    int 32 – 2147483648 to 2147483647
    short 16 – 32768 to 32767
    long 64 – 9223372036854775808 to 9223372036854775807
    byte 8 – 128 to 127
    char 16 0 to 65536
    float 32 1.40129846432481707e-45 to 3.40282346638528860e+38
    double 64 4.94065645841246544e-324d to 1.79769313486231570e+308d
    boolean Not properly defined true and false

3. How To Declare And Use Java Variables.

  1. To use a java variable in a java program, you need first declare a variable type, give the variable a name and assign an initial value to it.
    Variable_Data_Type Variable_Name = Variable_Value;
  2. Declare: Require some memory space from the java compiler to save the variable value. The compiler needs to know the variable data type, thus it can assign just desired memory space to the variable.
  3. Naming: When reading saved data in the java program, the java compiler should go to the location of the saved value memory space to retrieve the value back. The java compiler needs the data type and name from the program to make this transition effective. Give the variable a name and tell the name to the java compiler. Therefore when you use the name in your code later, the java compiler can know the location of the memory space that you refer to. The name is also called Identifier.
  4. Initialization: You can store data value in the variable reserved memory space after declaring it. Initialization is also extremely important before using it for any operations. Applying an initial value to it is called variable initialization.
  5. Below are some guidelines when you declare and use java variables.
  6. Do not name a java variable start with a digit but the java variable name can include digits.
  7. The java variable name can only start with underscore ‘_’, letter, or the dollar sign ‘$’.
  8. You can name a java variable using a single letter from a-z or A-Z.
  9. The java variable name cannot be java languages reserved keywords such as this, static, void, for, final, while, etc.
  10. Everyone has their own hobby to name java variables. I normally declare string variable starts with a small character ‘s’ and integer variable start with a small character ‘i’.
  11. Therefore when I find any variable start with character ‘s‘, I know that it must be a String. For example:
    String sUrl = “http://dev2qa.com";
    int iAge = 100;
    bollean bPassed = true;

4. Java Variable Exercises.

  1. Use java to print “Hello World” to console.
    //Use system out to print string to console
    System.out.println("Hello World");
  2. Declare 3 variables and assign 1.2, 3, and 0.618 to each of them in java and print the variable and its value to console.
    //Declare and initiate their value.
     double x = 1.2;
     int y = 3;
     double z = 0.618;
     //Print each name an value to console
     System.out.println(" x = " + x);
     System.out.println(" y = " + y);
     System.out.println(“ z = ” + z);

Subscribe to receive more programming tricks.

We don’t spam!

Subscribe to receive more programming tricks.

We don’t spam!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.