Learn variables in java

Welcome to flower brackets blog. Here we are going to learn variables in java.

So, what are variables in java??

Variables are nothing but names which we are declared to store some value.

public class SampleExample
{
   public static void main(String[] args)
   {
      int age = 10;
      System.out.println(age);
   }
}

In java we declare a variable like this. So first, data type. Data type can be integer, float, double, long etc..

Also read – learn java overview

Then assign some value to this variable, 10. This simply means that age variable of type integer which has a value of 10. So age is our variable.

Now in java there are different types of data types which we can declare like we have declared a variable of type integer.

Primitive data types

Learn Variables In Java

So these are some of the basic data types. Byte, short, int and long data type can store a number but only difference between byte, short, int and long is their size.

Byte data type can store 1 byte, short can store 2 bytes. Integer can store 4 bytes and long can store 8 bytes. So the size differs in this case. Range of byte is -128 to +127.

For short data type, the range is  minus -32768 to +32767. So the range of these increases in the ascending order. Smallest is the byte, then short, integer and long.

Now to store decimal point value we use float. For example, to store decimal value we write,

public class SampleExample
{
   public static void main(String[] args)
   {
      float average = 4.5;
      System.out.println(average);
   }
}

But the problem with declaration of float in this case is you need to declare float something like this,

public class SampleExample
{
   public static void main(String[] args)
   {
      float average = (float)4.5;
      System.out.println(average);
   }
}

Because in java this 4.5 is natively considered as a double and double can also store a decimal point but it’s range is 8 byte.

That’s why we need to convert this double value 4.5 to float value. And the difference between this float and double is float is 4 bytes and double is 8 bytes.

In the same way you can declare a double value. Double can also store decimal points. For example,

public class SampleExample
{
   public static void main(String[] args)
   {
      double myAverage = 11.56;
      System.out.println(myAverage);
   }
}

The other data type is char. A char can store character. For example,

public class SampleExample
{
   public static void main(String[] args)
   {
      char myGrades = 'A';
      System.out.println(myGrades);
   }
}

Here I’m storing capital ‘A’ or any single character that you store in char value and the last data type is boolean. Boolean can store true or false. For example,

public class SampleExample
{
   public static void main(String[] args)
   {
      boolean isTrue = false;
      System.out.println(isTrue);
   }
}

So, these are some of the basic data types in java and above are the examples on how you can declare a variable in java.

Related Posts