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.

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">&lt;Data Type&gt;&lt;Variable name&gt; = &lt;Value of the variable&gt; 

 

Following code snippets will make the point clear:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #0000ff">int</span> x = 25 [stores 25 in a variable named x as integer]
<span style="color: #0000ff">float</span> y = 1.58 [stores 1.58 in a variable named y as <span style="color: #0000ff">float</span>]
Char z = 'b' [stores b in a variable named z as character]
String myname = &quot;<span style="color: #8b0000">David</span>&quot; [stores David in a variable myname as a String 

 

READ  Understanding Server-side I/O Performance: Node vs PHP vs Java vs Go

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

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">System.out.println( &quot;<span style="color: #8b0000"> The integer value is </span>&quot; +x); 
[prints the value of x ,25] 

System.out.println(&quot;<span style="color: #8b0000">The float value is </span>&quot;+y); 
[prints the value of y ,1.58] 

System.out.println(&quot;<span style="color: #8b0000">The character value is </span>&quot;+z); 
[prints the value of z, 'b'] 

System.out.println(&quot;<span style="color: #8b0000"> My name is </span>&quot; +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:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">condition ? <span style="color: #0000ff">true</span>: <span style="color: #0000ff">false</span> 

 

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,

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #0000ff">for</span>( i=0;i&lt;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.

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">Class chap3 {
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main (String args[] ) {
<span style="color: #0000ff">int</span> x = 20 , y = 30 ;
<span style="color: #0000ff">float</span> z = 34.67f; x+=4; y*=7;
System.out.println( &quot;<span style="color: #8b0000">The sum of x and y is </span>&quot; (x+y));
System.out.println( &quot;<span style="color: #8b0000">The sum of y and z is </span>&quot; + (y+z));
System.out.println( &quot;<span style="color: #8b0000"> The value of x is </span>&quot; +x);
System.out.println( &quot;<span style="color: #8b0000"> The value of y is </span>&quot; +y); } }

 

READ  Learn how to work with SQL

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 Reply

Your email address will not be published.