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.

Java variable definition

Variables stand for memory space where the data is saved. Below is a physical description of it:

physical description of java variables

A variable consists of three parts which are variable name, value and it’s data type. Name is just used as a symbol to represent the memory space where the data is stored. Value is certainly the information to be stored in that memory space. And data type determines the type of value that can be stored. For example, int type variable can only store int type value. If you store a float type value, then the value maybe changed to int type automatically.

In the example of above picture, the variable name is firstNumber with int data type, and it’s value is 3. You can store integer values in int data type. Integer values includes both zero, positive number and negative numbers.

Java data type definition

Java data type determines what kind of information can be stored in the variable. There are reference and primitive types in java. Primitive types is used as a basic type that is not made up of other data types, but reference types are made up of other types.

Reference types includes classes or arrays. We will show you what is class and how to initiate object from classes and use a variable to reference to it. If you want to store a group of same data type object then you can use Arrays. We will introduce more about arrays in other posts.

READ :   Java I/O(Input/Output) Overview

We just introduce primitive types in this post. There a 8 primitive types in java, each can be used to store a special type of data. Additionally, each one also has value range limited.

If you store data values outside of it’s type range then an error will occur. Below table list all java primitive types, required memory amount and the value range for each 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

How to use variables

VariableDataType     VariableName  = Variable Value;

Declare: Require some memory space from java compiler to save it’s value. The compiler need to know it’s data type, thus it can assign just desired memory space to the variable.

Naming: When reading saved data in program, java compiler should go to the location of the saved value memory space to retrieve the value back. Compiler need the data type and name from program to make this transition effective. Give the variable a name and tell the name to compiler. Therefore when you use the name in your code later, java compiler should knows the location of memory space that you refer to. The name is also called Identifier.

Initialization: You can store data value in the variable reserved memory space after declare it. Initialization is also extremely important before using it for any operations. Applying an initial value to it is called variable initialization.

READ :   How To Install Tomcat 9.0 Correctly On Your Machine

Guidelines to identify java variables

  1. Can not name a java variable begin with digit but can include digits.
  2. Name can only begin with under score ‘_’ , letter or the dollar sign ‘$’.
  3. Can name a java variable using a single letter from a-z or A-Z.
  4. Name cannot be java languages reserved words such as this, static, void, for, final, while etc.

Everyone has their own hobby to name java variables. I normally declare string variable begin with a small character ‘s’ and integer variable begin with a small character ‘i’. Therefore when I find any variable begins with character ‘s‘, I know that it must be a String. For example:

String sUrl = “http://dev2qa.com";
int iAge = 100;
bollean bPassed = true;

Exercises

Use java to print “Hello World” to console

//Use system out to print string to console
System.out.println("Hello World");

Declare 3 variables and assign 1.2 , 3 and 0.618 to each of them in java and print the variable and it’s 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);
(Visited 105 times, 1 visits today)

Leave a Reply

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.