Java: An Introduction to Variables

Author: Ronald S. Holland
at
Total Application Works



To contact us



HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

© 2002 - 2016 All Rights Reserved Total Application Works


    Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
    <html>
    <head>
    <title>Java: An Introduction to Variables</title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/Java/JAV_Variables.html">
    Table Tags </a>
    </table>
    </body>
    </html>


Table of Contents

Java Resources

Introduction to Java Variables

The purpose of this tutorial is to introduce you to the basics of Java variables. Even if you've never programmed in a language similar to Java before, this tutorial should be very easy to follow and understand. It assumes that you have some basic exposure to programming (some object-oriented experience would be helpful, but is not necessary) and you have some kind of Java compiler (if you would like to run the examples). First, you know that Java is a language designed by Sun Microsystems. What do we need to first understand about variables?

Java Variables

What is a variable? A variable in programming is a structure that holds data and is uniquely named/identifies by the programmer. It holds the data assigned to it until a new value is assigned or the program is finished. You must declare all variables before they can be used. The basic form of a variable declaration is shown below:

 
data type variablename1 [ = value][, variablename2 [= value] ...] ;
  
where
  • data type is one of Java's datatypes; and
  • variablename is the name/identifier of the variable and it is case sensitive.
To declare more than one variable of the specified type, you can use a comma-separated list. There are three kinds of variables in Java:

Three kinds of variables in Java:

The three kinds of variables are:

  • Local variables

    • Local variables are declared in methods, constructors, or blocks (e.g., ifs, loops, etc). Basically it is a block of code found within a set of braces.
    • Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
    • Access modifiers (e.g., public, private,, protected) cannot be used for local variables.
    • Local variables are visible only within the declared method, constructor or block.Variables declared in method1 cannot be accessed by methodi. This applies to constructors and blocks.
    • Local variables are implemented at stack level internally.
    • There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use. Otherwise.they won't compile.
  • Instance variables an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy, or instance.

  • Class/static variables has a value that is same for all the object(or instances) of the class or in other words you can say that all instances(objects) of the same class share a single copy of static variables.

A variable can be depicted as:

 
  This variable contains the value of ten
             |    |
             |    |
variable     | 10 | 
container -> +----+
 
Figure 1: A variable


The variable name/identifier contains the memory location where the data is stored. Think of an empty box. When you put a ring in the box, the box contains a ring. The container in Figure 1 could just as easily be a ring container. How do we name/identify a local variable?

Rules for naming variables:
  • All variable names must begin with a letter of the alphabet, an underscore, or ( _ ), or a dollar sign ($).  The convention is to always use a letter of the alphabet.  The dollar sign and the underscore are discouraged..

  • . After the first initial letter, variable names may also contain letters and the digits 0 to 9.  No spaces or special characters are allowed.

  • The name can be of any length, but don't get carried away.  Remember that you will have to type this name..

  • Uppercase characters are distinct from lowercase characters.  Using ALL uppercase letters are primarily used to identify constant variables.  Remember that variable names are case-sensitive..

  • You cannot use a java keyword (reserved word) for a variable name..

Variables can contain
  • Integers - which can be expressed in any whole number, octal, or hexadecimal format.
    • numbers - can be expressed in the form of 10, 10.0, or -2
      • Floating point - Examples are 10.15, -10.15, 1015e-2
    • hexadecimal (base 16) - are characters preceded by a 0x (zero-x) and followed by numbers from 0-9, and/or a-f. The value of forty-four is expressed as "0x2C".
    • octal (base 8) - the value of forty-four is expressed as 054. In this example, you precede the number 54 by the number '0' (zero).

    Integer Long Octal Hexadecimal
    0 0L 0 0x0
    1 1L 01 0x1
    8 8L 001 0x8
    10 10L 012 0xA
    15 15L 017 0XF
    16 16L 020 0x10
    100 100L 0144 0x64
    Figure 2: Examples of integer literals.


  • Boolean - values of true or false.
  • String values - May be enclosed by single ' or double " quotes. The \' or \"sequence of characters will insert a quote character into a string.
  • Arrays - is a container that holds any of the above types.
  • Objects - can be thought of as an instance of a class.

Numeric

Range of numeric data types in Java
TypeSizeRange
byte8 bits-128 .. 127
short16 bits-32,768 .. 32,767
int32 bits-2,147,483,648 .. 2,147,483,647
long64 bits-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
float32 bits 3.40282347 x 1038, 1.40239846 x 10-45
double64 bits 1.7976931348623157 x 10308, 4.9406564584124654 x 10-324

Java: Primitive data types

A primitive data type is either of the following:

  • a basic type is a data type provided by a programming language as a basic building block. Most languages allow more complicated composite types to be recursively constructed starting from basic types; or
  • a built-in type is a data type for which the programming language provides built-in support.
The eight primitive data types in Java are:
  • boolean, the type whose values are either true or false
  • char, the character type whose values are 16-bit Unicode characters
  • the arithmetic types:
    • the integral types:
      • byte
      • short
      • int
      • long
    • the floating-point types:
      • float
      • double

When a variable is declared in Java, it is declared with a data type and variable name. When you define a variable in Java, you must tell the compiler what kind of variable it is: an integer, a character, etc. This information tells the compiler how much room to set aside and what kind of value you want to store in your variable. For example,
    data type anyName - the variable anyName has a data type declared with it.
In the next example,
    int anyName = 10 <-- the variable anyName is declared with the data type of integer.

The first letter in the Variable name is an upper or lower case A through Z or the "_" character. The remaining characters may consist of the same characters or it may contain digits 0 through 9.

Variable declaration examples

Consider the following example:

Definition and Creation Variable int Array float Array double Array
  
    int a = 5 ;

    int x[] ;
    x = new int[5] ;

    float y[] ;
    y = new float[5] ;

    double xx[] ;
    xx = new double[5] ;
  
   
| 5 |
+---+
  
| 5 |   <-- index 0
+---+
| 5 |   <-- index 1
+---+
| 5 |   <-- index 2
+---+
| 5 |   <-- index 3
+---+
| 5 |   <-- index 4
+---+
   
   
| 5 |   <-- index 0
+---+
| 5 |   <-- index 1
+---+
| 5 |   <-- index 2
+---+
| 5 |   <-- index 3
+---+
| 5 |   <-- index 4
+---+
   
   
| 5 |   <-- index 0
+---+
| 5 |   <-- index 1
+---+
| 5 |   <-- index 2
+---+
| 5 |   <-- index 3
+---+
| 5 |   <-- index 4
+---+
   
Figure 3: Variable and Array Definition and Creation


There are a few points that are being made by the depictions in Figure 1. Each array shown above:

  1. Can only contain one data type;
  2. Are of a fixed length as defined;
  3. Can be referenced by a variable name;
  4. Depict data that is stored in contiguous memory locations;
  5. Depict data that is stored and referenced in sequential order.

Size of various primitive data types

The size of various primitive data types is found in the following table.

Type Size
bits
Type Size
bits
Type Size
bits
Type Size
bits
bool
1
byte
8
int
32
float
32
char
8
short
16
long
64
double
64
Figure 5: Size of variable types.


In Figure 5, a byte is not a data type. It is a measure of size.

Quiz questions

Definitions

Procedural Language

    Procedural programming is a programming paradigm, derived from structured programming, based upon the concept of the procedure call. Procedures are also known as routines, subroutines, methods, or functions found in languages like C.

programming paradigm










HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#


To contact us

© 2002 - 2016 All Rights Reserved Total Application Works