Java variables,Data types,Keywords,Operators with easy explanation

Today we will learn about the variables,data types,keywords and operators.These are the basic building blocks of java which is used to perform the different operations.

Java variables

Variables are the container which holds the value into the memory.

Variables are something which provides the way in which we can store the value into the memory.

These value can be changed during the run time of program.

Example: int x=10;

Here: “x” is the variable. “int” is the data type.

Types of variables

There are three types of variables:

1.Local variable

2.Instance variable

3.Static variable

1.Local variable

When the variable is declared inside the body/method then it is known as local variable.Variables inside the method can not be static.

Example:

public class LocalVariableProgram
{
public static void main(String[] arg)
{
public void hello()
{
int x=10;//Here x is the local variable
System.out.println("The value of x="+x);
}
}
}

Output:

The value of x=10

2.Instance variable

When the variable is declared inside the class but outside the method then it is known as instance variables.

Example:

public class InstanceVariableProgram
{
public static main(String[] arg)
{
int x=10;//Here x is the instance variable
public void hello()
{
}
System.out.println(The value of x="+x);
}
}

Output:

The value of x=10

3.Static variable

Static variable is same as instance variables.But it uses the static keyword before the declaration of variable.It share the common property for the all the objects.

Example:

public class StaticVariableProgram
{
public static void main(String[] arg)
{
static int x=10;//Here x is the static variable
public void hello()
{
}
System.out.println("The value of x="+x);
}
}

Output:

The value of x=10

Java data types

Java data types tells the compiler that what kind of data do you want to store in the memory using variables.

Example: int x=10;

Here x is a variable and int is a data type.

Types of data types:

There are two types of data types in java

1.Primitive data types(int,char,short etc)

2.Non-primitive data types(string,arrays,class etc)

1.Primitive data types

Primitive data types are the predefined data types which is  already defined by the compiler.

We can not modify anything in the primitive data types.

It has always value it can not be null.

It size depends on the data types.

Example:

There are eight primitive data types is given with the size and range of that data types.

Data typesSizeRange
byte1 byte-128 to 127
int4 bytes -2,147,483,648 to 2,147,483,647
boolean1 bitstore true or false
short2 bytes-32,768 to 32,767
char2 bytesstore singe character or ASCII character
long8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4 bytes 3.4eāˆ’038 to 3.4e+038.
double8 bytes 1.7eāˆ’308 to 1.7e+038


2.Non-primitive data types

The non-primitive data types are given below

Strings,Arrays,Classes,Interfaces etc.These things we will learn in next post.

Java keywords

Java keywords are also known as reserved word.It is the basic building blocks of java.

Java keywords can not be used as a variables.

Some java keywords are given below:

  • abstract
  • interface
  • boolean
  • class
  • final
  • break
  • try
  • catch
  • extends
  • switch
  • finally
  • do
  • double
  • default
  • so on

Java operators:

Java operators are the symbols which is used to perform certain mathematical and logical operations such as +,-,/,* etc.

Example:

Int x=100+200;

Here 100 and 200 are the operands and ā€œ+ā€ is the operator.

Here + operator is used to add the numbers.

So x=300

There are different  types of java operators are given below:

1.Arithmetic operator

2. Relational operator

3.Logical operator

4.Bitwise operator

5 Assignment operator



sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *