Understanding Data Types and Variables in Java

In every programming language, you will learn about data types like Integer, Float etc. Data types are necessary for variable declaration. Data Types in Java are primitive types i.e.; they are built into the Java language itself. Data types in Java can be classified as Byte, Short, Long, Integer, Float, Double, Character, Boolean. Let us discuss them in short.

Integers are used for sorting integer values. The capacity of the data types in Java is given below for your reference byte (8 bits), short (16 bits), int (32 bits) and long (64 bits). The bits represents its size. Float is mainly used to store decimal values. There are two types of float namely Single precision( 32 bits ) & double precision (64 bits). Character is used for storing characters. Boolean stores True and False values. String represents a combination of characters like "Java", "Program" etc.

You cannot declare the variables just by giving the names and values. It should be declared according to the rules and conventions. The next session explains how to declare and use the variables in Java.

Variables

Variables are nothing but locations in memory that can hold values. First a Data Type as described above should have to be selected, and a variable should be declared before assigning values. Generally we use the following Syntax while declaring the Variables.

<Data Type><Variable name> = <Value of the variable> 

 

Following code snippets will make the point clear:

int x = 25 [stores 25 in a variable named x as integer]
float y = 1.58 [stores 1.58 in a variable named y as float]
Char z = 'b' [stores b in a variable named z as character]
String myname = "David" [stores David in a variable myname as a String 

 

Variable names like this can be declared anywhere in the java program, but it is common to declare them at the beginning .Let us now look at how the above declarations can be returned in a java program. Note the statement below

System.out.println( " The integer value is " +x); 
[prints the value of x ,25] 

System.out.println("The float value is "+y); 
[prints the value of y ,1.58] 

System.out.println("The character value is "+z); 
[prints the value of z, 'b'] 

System.out.println(" My name is " +myname); 
[prints the String specified] 

Control Flow Statements

Java consists of If else, while, for, Switch and a ternary Operator. The If else returns the specified value or String based upon each program. The conditional or ternary operator returns true or false. This is used as a alternative to If else. The syntax is as shown below:

condition ? true: false 

 

The while loop executes until a specified condition is false. For example if the condition is x<20 the program will continue till the value of x is 20.

The for loop is used for iteration. For example the java code,

for( i=0;i<10,i++) 

 

executes until is greater than 10. i++ increments the value of i until the value of i is 10.

The Switch statement execute until the value is reached. For example int b = 5; Switch(b) executes until the case is equal to 5.

Class chap3 {
public static void main (String args[] ) {
int x = 20 , y = 30 ;
float z = 34.67f; x+=4; y*=7;
System.out.println( "The sum of x and y is " (x+y));
System.out.println( "The sum of y and z is " + (y+z));
System.out.println( " The value of x is " +x);
System.out.println( " The value of y is " +y); } }

 

Save the file as chap3.java and compile the code. On compilation chap3.class is created. Run the program by typing java chap3 at the DOS prompt. Following output is displayed:

In the same way, you can manipulate the operators by applying different values.

Leave a Comment