Variables – variables are names which stored some values into memory.

Ex – int a=90;

It’s mean we store value ‘90’ into integer variable ‘a’.

Data type – data type means which type of value will be stored into variable. In java. There are two type of datatypes – primitive and non-primitive.

Primitive datatypes –

Primitive types are defined by the Java programming language as special types that do not have a supporting class and are named by their reserved keyword. Variables of these types are saved on the stack memory and when values are assigned to them using the = (equals) operator, the value is actually copied. Examples – int, char , double , boolean etc.

int a; //declaring integer variable ‘a’
a=20; //initializing value ’20’ into variable ‘a’
String a; //declaring string variable ‘a’
a=”microcodes”; //initializing string value ‘microcodes’ into variable ‘a’
float a; //declaring float variable ‘a’
a=20.5f; //initializing value 20.5 into variable ‘a’

** remember – when you initializes any float value into any variable, then at the end of value ‘f’ or ‘F’ letter mandatory.By default 20.5 is double literal. To tell compiler to treat it as float explicitly -> use ‘f’ or ‘F’.

char gen; //declaring character variable ‘gen’
gen=’M’; // initializing character value ‘M’ into variable ‘gen’
booleanamnt; // declaring Boolean variable ‘amnt’
amnt=true; // in Boolean variable ‘amnt’ we can store either true or false

Non-primitive datatypes –

Non-primitive data types or reference data types refer to instances or objects. They cannot store the value of a variable directly in memory. They store a memory address of the variable. Unlike primitive data types, which are defined by Java, non-primitive data types are user-defined.

Examples – array, classes, string etc.

We will discuss about arrays, classes, strings in later chapters/articles.

If we does not follow the rule and stores different values into variable against to its datatypes, then what happened?

class code{
    public static void main(String[] args) {
int a;

We understand what data types are there in java are. Now let’s see how we can get any variable data-type. For this we need use a .getClass() and .getSimpleName() methods and particular variable will treated like an object.

Let’s understand through below code…

public class code2 {
    public static void main(String[] args) {
int a = 20;
    String b = “microcodes”;
    float c = 34.5f;

Keyword – these are reserved words which have a special meaning to the compiler.

In java there are total 51 keywords available. Some of most used keywords are – break, continue, if, else-if, else etc.

Constant – constants are those variables which values can’t be changed further if once declared.

For implementing constant into work in Java, we need to use ‘final’ keyword.

Let’s understand through an example

public class conStant {
    public static void main(String[] args) {
// declaring integer variable ‘b’ as constant
final int b=30;
      // b=40 can’t be initialize
System.out.println(“b is “+b);


Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion. In Java, we can cast both reference and primitive data types. By using casting, data cannot be changed but only the data type is changed.

In simple words, typecasting is a process to change a variable data-type into another data-type explicitly.

For doing this we can use several methods – toString(), toHexString() etc.

** remember: we change data-types like –

Integer -> string, float -> string, float -> integer etc.

We will see some programs for typecasting…

// typecasting integer into string
public class typecast {
    public static void main(String[] args) {
int a=10;
System.out.println(“variable type defore “);
System.out.println(“value of a : “+a);
        // explicitly change integer ‘a’ into string ‘b’
        String b=Integer.toString(a);
System.out.println(“variable type after”);
System.out.println(“value of b “+b);

Let’s see another example of type-casting…

public class typeCast2 {
    public static void main(String[] args) {
int m=10;
        float amnt=34.5f;
System.out.println(“before typecasting”);
System.out.println(“integer m is: “+m);
System.out.println(“float amnt is: “+amnt);
        // type-casting float into integer
System.out.println(“after typecasting “);
System.out.println(“integer m is: “+m);
System.out.println(“float amnt is: “+amnt);

But every time we can’t type-casts variables

public class typeCastErr {
    public static void main(String[] args) {
        String name=”angry prash”;
int a=Integer.parseInt(name);