Monday 26 October 2015

Java Variables, DataTypes, Declaration and Initialization


Before I begin this post I need to tell you that Java was invented by a man named James Gosling.
That's all.

Now I begin
In the previous post we talked about expressions and how they can be computed using Java programming language.

Now I want to discuss how to use variables.

Before I show you how to make use of variables, here is a very important point you should know.


An identifier is a name or sequence of characters that consists of letters, digits and symbols used to represent something in a chunk of code.
All identifiers must begin with a letter.
An identifier cannot be a reserved word or keyword.

Note: All variables are identifiers but not all identifiers are variables.

Now from what I just said about identifies I think you should be getting a hang of what a variable is.
What is a variable?
A variable is a name used by a programmer to represent a value in a program.
For a variable to be effective in a Java program an assignment must occur which brings me to an operator called assignment operator.

An assignment operator is the equal to sign (=).

Now take for example this operation or expression.

ball = 45;

What this expression above is saying is this: let 45 be represented by ball. Therefore, where ever ball is in the rest of the program it holds 45 in it.

You will comprehend further as we go so don't worry.

Now this expression ball = 45; cannot be used in a Java program without something called Declaration and Initialization.

Declaration is the assigning of the appropriate datatype to a variable while initialization is the assigning of a value to the variable;

What is a Datatype?
A datatype is a keyword that tells the computer what to be stored in a variable.

Explaining Further...

For a variable, there are four major formats which are:

1.    Integers
2.    Floating Point
3.    String
4.    Character
5.    Boolean

Integers are numbers that has no decimal point or fraction.
The datatypes under integers are:

byte  -   Ranges from -128  to 127
short  -   Ranges from -32768 to 32767
int  -   Ranges from -2147483648 to 2147483647
long  -   Ranges from -2^63 to 2^63 - 1

The ranges are the kind of numbers that can stored in a variable using that datatype

The datatypes under floating point (This is a number that has a decimal point or fraction) are:

float
double

The datatypes under String is String.
String represents a group of characters or words that must be embedded in quotation marks.
The String datatype must be written with S in capslock.
For Example: String name = "Idoko Nicholas Chinazom";

The datatype under Character is char.
char is a datatype used on variables that store values bearing a single character or symbol like a, b embedded in apostrophes.
For Example: char apple = 'a';

The datatype under Boolean is boolean.
The boolean datatypes are used to store variables that carry only true or false;

In this post there are five datatypes that are of major interest to us which are:

int
double
String
char
boolean

Don't worry about byte, short, long and float. We won't be needing them for a while. Just know them, OK?

Note that when assigning numeric and boolean values do not embed it in quotation marks.

Declaring a variable means placing the appropriate datatype before the variable.

For Example:

int ball;
double average;
String name;
char cup;
boolean check;

Once a variable has been declared, the datatypes should not come before the variable in the following codes.

Initializing a variable means assigning a value to the variable.

For Example, let's initialize the variables we declared above.

ball = 11;
average = 8.3;
name = "Idoko Nicholas Chinazom";
char = 'd';
check = false;

As you can see I did not include datatypes in the intialization because they have already been declared. 

Datatyping a variable is done only in declaration.

There are situations where you do not want to declare and intialize separately.
You just want to do the both at once. Yes! It is possible.

Here is how to do both declaration and initialization at once.

int ball = 11;
double average = 8.3;
name = "Idoko Nicholas Chinazom";
cup = 'c';
check = false;

That's all my dear!

Please note that declaration, initialization and assignments are all statements in Java. Therefore, they should end with a statement terminator, semicolon (;).

Let's do a little exercise.
First We are going to declare and intialize five variables with these five datatypes (int, double, String, char and boolean) separately and output them to display on our console.
Second we are going to declare and initialize five variables with these five datatypes (int, double, String, char and boolean) and output them to display on our console.

If you are done with the coding, customize and run.

Here is my code for the first exercise

package nicholas;
public class DeclareInitialize{
public static void main(String [] args){
// Declaring My Variables
int count;
double average;
String address;
char index;
boolean check;

// Intializing My Variables
count = 100;
average = 2.5;
address = "Phase 6, Transekulu, Enugu";
index = 'a';
check = false;

// Displaying My Variables
System.out.println(count);
System.out.println(average);
System.out.println(address);
System.out.println(index);
System.out.println(check);
}
}

Here is the screenshot of the code:

Here is the output:

Here is my code for the second exercise:

package nicholas;
public class DeclareInitialize{
public static void main(String [] args){

// Declaring and Intializing My Variables
int count = 45;
double average = 3.5653;
String address = "Eziobodo, FUTO Road, Owerri";
char index = '@';
boolean check = true;

// Displaying My Variables
System.out.println(count);
System.out.println(average);
System.out.println(address);
System.out.println(index);
System.out.println(check);
}
}

Here is the screenshot of the code:

Here is the output:


Make sure you try yours, if you have any problem understanding this post please do well to comment I am here to help.

Peace!!

No comments:

Post a Comment