Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

Variables and Data Types in Java

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.
  1. What is a Variable ?.
  2. What is a Data Type ?.
  3. How to declare Variables ?.
  4. How to initialize a Variable ?.
  5. How to access a Variable ?.
  6. What are Constants ?.
  7. How is Automatic Conversions done ?.
  8. 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
   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
© 2021 Learn Java by Examples Template by Hubberspot