Variable Declaration - JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

Chapter 5. Variable Declaration

Previously, we declared a variable to contain a text user input. If you want to create more complex programs then you have to store values in variables. This chapter will now focus on declaring variables as part of your programming style.

Assigning what type of variable to be used is done in a declaration statement with the following syntax:

<type> <list-of-variables-separated-by-commas>;

Examples:

int rows, cols;

String companyName;

Again, a variable is a placeholder, the thing stored in it is a value and the kind of value that is stored in a variable is its type. Looking at the declaration statements above, the word at the left (int, String) specifies the type for the variable or variables at the right (rows, cols, companyName). By the way, a Java variable can hold only one type of value and its value can change during program execution. So in the examples above, rows and cols variables can hold only integers while the second declaration statement the companyName variable can hold only strings. Below are the basic variable types in Java language:

Type Name

Description

Range of Values

Whole Number Types

int (Integer)

- simplest data type for handling numbers

- no decimal points

- not ideal for precision data

- default value is 0

-2147483648

to

2147483647

byte

- has the smallest range for number data type

- contains an 8-bit signed 2s complement integer

- default value is 0

-128 to 127

short

- contains 16-bit signed 2s integer

- default value is 0

-32768 to 32767

long

- contains 64-bit signed 2s complement integer

- default value is 0

-9223372036854775808 to 9223372036854775807

Decimal Number Types

float

- contains 32-bit IEEE 754 numerical values

- contains decimal points

- default value is 0.0f

-3.4 x 1038

to

3.4 x 1038

double

- contains 64-bit IEEE 754 numerical values

- contains decimal points

- more precise than float

- default value is 0.0d

-1.8 x 10308

to

1.8 x 10308

Character Type

string

- contains a sequence of alphanumeric characters

- default value is null

char

- accepts a single character as data

- default value is \u0000 (represents an empty space)

thousands of characters and symbols

Logical Type

boolean

- default value is False

True, False

Please take note of the following declaration statements for each variable type:

int AnyVariable; or int AnyVariable = 0;

byte AnyVariable; or byte AnyVariable = 0;

short AnyVariable; or short AnyVariable = 0;

long AnyVariable; or long AnyVariable = 0L;

float AnyVariable; or float AnyVariable = 0.0f;

double AnyVariable; or double AnyVariable = 0.0d;

String AnyVariable; or String AnyVariable = null;

char AnyVariable;

Boolean AnyVariable; or Boolean AnyVariable = false;

As you can notice, String and Boolean are in uppercase letters. This is because they are data types that happen to be a class names too. Therefore, in code and conventional text we will use uppercase S and B.

Previously our Example Java program accepts a string value from the user. This time, we will alter the code so it will ask the user to enter an integer data type. Let us say the program will display the user’s age after entering it. Take a look at the new programming code below:

In the last programming line, we used nextInt() to instruct the program to save an integer data type. Other data types that we can also scan are nextByte(), nextShort(), nextLong(), nextFloat() and nextDouble().

If you need to create more complex operations in your programs that require using multiple input values, then it is better to save the user input and declare the variable. In this case, it is must that you already have an idea of what the data type of that variable should be. By modifying the program above, we will now have:

Based on the new program code, the Scanner variable InputVariableName was first saved into MyVariableName and then was used to display the user-generated input to the screen.

After you have learned the different variable types and how to properly declare them, you will now be able to create programs with additional functionalities and complexities. In the following chapter, another feature will be introduced which is the application of the Java language operator.