how to know More on java variable and data type in java

Variables and Data Types


Variables.

A variable is a place where the program stores data temporarily. As the name implies the value 
stored in such a location can be changed while a program is executing (compare with constant).

class Example2 {

 public static void main(String args[]) {

 int var1; // this declares a variable

 int var2; // this declares another variable

 var1 = 1024; // this assigns 1024 to var1

 System.out.println("var1 contains " + var1);

 var2 = var1 / 2;

 System.out.print("var2 contains var1 / 2: ");

 System.out.println(var2);

       }

}


Predicted Output:

var2 contains var1 / 2: 512
The above program uses two variables, var1 and var2. var1 is assigned a value directly while var2 is 
filled up with the result of dividing var1 by 2, i.e. var2 = var1/2. The words int refer to a particular 
data type, i.e. integer (whole numbers).


Data Types.


The following is a list of Java’s primitive data types:

Data Type Description

int Integer – 32bit ranging from -2,147,483,648 to 2,147,483,648

byte 8-bit integer ranging from -128 to 127

short 16-bit integer ranging from -32,768 to 32,768

long 64-bit integer from -9,223,372,036,854,775,808 to -9,223,372,036,854,775,808

float Single-precision floating point, 32-bit

double Double-precision floating point, 64-bit

char Character , 16-bit unsigned ranging from 0 to 65,536 (Unicode)

boolean Can be true or false only

The ‘String’ type has not been left out by mistake. It is not a primitive data type, but strings (a

sequence of characters) in Java are treated as Objects.

class Example8 {

 public static void main(String args[]) {

 int var; // this declares an int variable

 double x; // this declares a floating-point variable

 var = 10; // assign var the value 10

 x = 10.0; // assign x the value 10.0

 System.out.println("Original value of var: " + var);

 System.out.println("Original value of x: " + x);

 System.out.println(); // print a blank line
// now, divide both by 4

 var = var / 4;

 x = x / 4;

 System.out.println("var after division: " + var);

 System.out.println("x after division: " + x);

   }

}

Predicted output:

Original value of var: 10

Original value of x: 10.0

var after division: 2

x after division: 2.5

One here has to note the difference in precision of the different data types. The following example

uses the character data type. Characters in Java are encoded using Unicode giving a 16-bit range, or

a total of 65,537 different codes.


class Example9 {

 public static void main(String args[]) {

char ch;

ch = 'X';

System.out.println("ch contains " + ch);

ch++; // increment ch

System.out.println("ch is now " + ch);

ch = 90; // give ch the value Z

System.out.println("ch is now " + ch);
   }
}


Predicted Output:

ch is now X

ch is now Y

ch is now Z

The character ‘X’ is encoded as the number 88, hence when we increment ‘ch’, we get character

number 89, or ‘Y’.

The Boolean data type can be either TRUE or FALSE. It can be useful when controlling flow of a

program by assigning the Boolean data type to variables which function as flags. Thus program flow

would depend on the condition of these variables at the particular instance. Remember that the

output of a condition is always Boolean.

class Example10 {

 public static void main(String args[]) {

boolean b;

b = false;

System.out.println("b is " + b);

b = true;

System.out.println("b is " + b);

// a boolean value can control the if statement

if(b) System.out.println("This is executed.");

b = false;

if(b) System.out.println("This is not executed.");

// outcome of a relational operator is a boolean value

System.out.println("10 > 9 is " + (10 > 9));

}

}

Predicted output:

b is false

b is true

This is execute
10 > 9 is true

If you like this post please comment for improvement

Comments