This chapter will explain various variable types available in Java Language. There are three kinds of variables in Java:
1. Local variables
2. Instance variables
3. Class/static variables

Java is built on top of basic variable types called primitives.

There are eight primitives in Java:

How to Declare a Variable

Java is a strongly typed programming language. This means that every variable must have a data type associated with it. For example, a variable could be declared to use one of the eight primitive data types: byte, short, int, long, float, double, char or Boolean.

  1. byte (number, 1 byte)
  2. short (number, 2 bytes)
  3. int (number, 4 bytes)
  4. long (number, 8 bytes)
  5. float (float number, 4 bytes)
  6. double (float number, 8 bytes)
  7. char (a character, 2 bytes)
  8. boolean (true or false, 1 byte)

 int a, b, c;     // Declares three ints, a, b, and c.  
 int a = 10, b = 10; // Example of initialization  
 byte B = 22;     // initializes a byte type variable B.  
 double pi = 3.14159; // declares and assigns a value of PI.  
 char a = 'a';    // the char variable a iis initialized with value 'a'  

How to declare Number for variable

First one can declare Variable in the form of integer
int myNumber;
myNumber = 9;

Integer can also declared as
int myNumber = 9;

Variable can be defined as double Variables
double d = 6.5;
d = 7.0;

Float can be used as
float f = (float) 6.5;

or

float f = 6.5f (f is a shorter way of casting float)

The Example of boolean is

boolean b = false;
b = true;
boolean toBe = false;
b = toBe || !toBe;
if (b) {
System.out.println(toBe);
}
int children = 0;
b = children; // Will not work
if (children) { // Will not work
// Will not work
}


No Comment to " Java Variables "