Home >>

Variables and Data Types in Java programming language.


Introduction

Lets get started with my yet another hub on Java Tutorials. I am going to give a good tutorial on Variables and Data types used in Java programming language. The objective of this hub is to make you familiar with few questions i.e.

What is a Variable ?.
What is a Data Type ?.
How to declare Variables ?.
How to initialize a Variable ?.
How to access a Variable ?.
What are Constants ?.
How is Automatic Conversions done ?.
How Explicit Conversions work ?.



Introduction

Lets get started with my yet another hub on Java Tutorials. I am going to give a good tutorial on Variables and Data types used in Java programming language. The objective of this hub is to make you familiar with few questions i.e.

What is a Variable ?.
What is a Data Type ?.
How to declare Variables ?.
How to initialize a Variable ?.
How to access a Variable ?.
What are Constants ?.
How is Automatic Conversions done ?.
How Explicit Conversions work ?.

Lets begin with explanation of each of questions above.

What is a Variable ?.

Variables are the temporary elements in Java which are used to store information about a program. Variables are highly used in Java. It resides in memory and are temporary placeholder for information needed to use in a program. Since value of a variable exists in memory therefore as soon as program exits value is lost. Each variable in Java has a type and a value. Since Java is strongly typed language, therefore each variable used is associated with type. Generally few language out there in market are loosely typed i.e any information can be stored in variable, Whereas in Java data type provides user information that whether variable is integer, string or float etc. The value of variable can be changed anytime in a program, unless the variables are marked with a keyword final. Generally final variables are constants, once a value is assigned to them it does not change.

What is a Data Type ?.

Java is a strongly typed language. It means that for each variable defined there must be a data type associated with it. In Java data types provides developers an idea about the size of variables, a range of values that a variable can therefore store and also about operators that can be used with it. All data stored in a variables are Objects, having exception as primitive data types. Data types used in Java are platform independent, i.e a integer can store same size of data on different platforms such as windows, unix etc. There size to store data is platform independent. This has helped developers a lot, because they now don't need to convert one data type to other when switching the same code on different platforms.

Now let us look at some of the data types available to us in Java.

Integer Data Types

Integer Data Types has capability to store integer values. Generally there are 5 types of Integer data types available in Java. Let look and classify them.

 

Floating Point Data Types

Floating Point Data Types has capability to store integer and floating point values. Generally there are 2 types of floating point data types available in Java. Lets look and classify them.

Boolean Data Types

Boolean Data Types has capability to store true and false values. Lets look and classify them.

How to Declare a Variable ?

Now that we know something about variables and data types, we can easily understand how to declare a variable. Generally by word declare we mean the creation of a variable. Creation of variable can be done any point of time in a code. Lets look at few examples below :-

// Declaring a variable follows below simple syntax.
// [data type][space][identifier][;]
// Few Examples :

int i ; // int -> data type and i -> identifier
float j ; // float -> data type and j -> identifier
long salary ; // long -> data type and salary -> identifier

How to initialize a Variable ?.

After declaration of a variable, we usually assign it a value. Thus by the term initialize we mean that providing a value to a variable at the time of declaration. By default a local variable gets a value which is left over in memory. Here local variable means that a variable is been declared in a method or a block of code. Generally initialization of a variable is done by assignment operator called as ' = '. Lets look at few examples :-

// Initialization of a variable follows below simple syntax.
// [data type][space][identifier][=][value][;]
// Few Examples :

int i=0 ; // int -> data type,i -> identifier and 0 -> value
float j=3.2f ; // float -> data type , j -> identifier and 3.2f -> value
long salary=1000L ; // long -> data type salary -> identifier and 1000L -> value

How to access a variable ?.

After having some basic knowledge about the variables such declaring and initialization. We can move ahead to look for how to access a variables value. By accessing we mean how can we get out of variable value stored in it. Before accessing a value of a variable, it should be initialized. If you try to access value compiler will throw an error at compile time. Generally in order to access a variable we just use identifier name that's it. One thing should be in mind that variable accessed should not be coded in double quotes "". As double quotes are by deault String literals. Instead of printing the value , it will print the identifier name. Below example code shows how to access a variable.

/* This is Simple Java program call it as Welcome.java.
It prints some text on the console.*/

public class Welcome
{
// main method begins execution of Java application
public static void main( String[ ] args )
{
int hubscore = 50; // declaring and initializing a variable

System.out.println( "Welcome Java Programs to Hubpages! with hubscore : "+hubscore ); // hubscore in quotes is String and outside quotes its name of variable which will print value in it.
} // end method main
} // end class Welcome


// Output of the program

Welcome Java Programs to Hubpages! with hubscore : 50

What are Constants ?.

Constants are the variable whose value we do not want to get change after it has been assigned a value. Generally to achieve this feature we have to use keyword called as final. A final keyword will make the variable constant. Any attempt to change value of final variable after its initialization would throw a compile time error. As a best practice we generally use capital letters in giving a name to an identifier. Let see how syntax looks like:

// Syntax for declaring and initializing a constant
// [final][data type][identifier][=][value][;]

final int constants = 10 ;

How is Automatic Conversions done ?.

When creating a Java application or code we usually have to convert one data type to another. This is somewhat quite hectic task. Java has an excellent feature of automatic conversion. In Java basically there are two types of conversion : 1. Upcasting 2. Downcasting. Upcasting is done automatically and called as Automatic conversion. While downcasting is done explicitly and called as Explicit conversion, which we will see later. In Automatic conversion also upcasting, Java let us do the upcasting. Here below image shows that small data types can be assigned to large data types without any much headache. Java does it for us.

// Automatic Conversion in Java
// int data type automatically gets assigned to float data type

int age = 10;

float oldAge = age;

// Everything is handled by Java just size matters
// small data type ---upcasted-- > large data types

How Explicit Conversions work ?.

Explicit conversions basically downcasting is been handled by the programmer itself. If we want to convert large data type to small data type then we have to write some extra code for that. Generally we have to use a special operator for that. The special operator is named as Cast Operator. Cast operator looks like ( data type ) . Here data type in brackets is the data type to which we want to convert. Let see example code :-

// We want to convert double (larger data type) to float
// (smaller data type)

final double PI = 3.14;
float newPi = (float)PI;

// Cast operator (float) converts double to float and
// assign it to newPi


For more of such hot topics read my other posts here :

Visit my new website

POSTED BY jontymagicman On 2012-05-11
Add new comment Report Article

More Articles Specially Recommended for You:

Top 8 List of Lost Cities of The World

LOST CITIES Now-a-days there are many cities which are now not existing. Now the names are not mentioned because they are destroyed or abandoned. S

Written By : jontymagicman
Importance of Punctuations in English Grammar

PUNCTUATIONS Punctuation in Latin sense means a point ‘a punctum’.it is an important branch of English grammer.the use of punctuations gives more c

Written By : jontymagicman
How to Choose Good Keywords For The Seo

Keywords are the terms and phrases that your target market uses when searching the major search engines and directories for the products and services

Written By : jontymagicman
Learn The Search Engine Ranking Criteria

Each search engine has its own unique ranking criteria and its own unique algorithm, or formula, giving different weighting to each of the criteria in

Written By : jontymagicman
Muscle Cars Still Reign Supreme

During the 1950’s and 1960’s the big motor companies were in the middle of a car revolution. The nation was turned upside down with people buying musc

Written By :nikmahajan
Lost Planet 2 Playstation 3 (PS3) Version Confirmed

After lots of speculations, Lost Planet 2 Playstation 3 version is finally confirmed. There is no reason left now to deny Lost Planet 2 for PS3 as

Written By :nikmahajan
Lays Chips HARAAM: Propaganda or Truth?

Some days back I received on SMS and an Email saying that, Lays Chips (Masala Flavor) is HARAAM. On the back of the Lays chips “E631 Fat”

Written By :ubpl
VolksWagen Polo - The Same But Different

Despite the facts that the new VolksWagen Polo resembles the old one and that it is also similar to the VW Golf MkVI it is indeed an all new vehicle.

Written By :nikmahajan
Please Login or Sign up to Comment on the Post

COMMENTS