Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to use variables in Java

In this article we will discuss details about different variables in Java. Variables are integral part of any programming language.

During the programming on many occasions we need to store data in memory. For storing data in memory we declare variables. It mean that we reserved a space in memory when the data will be execute using the variable. But the main thing is that type of variable. We need to mention the primitive data type of variable. And there are three types of variables in Java Programming Language.

  • Static variable
  • Local variable
  • Instance variable

Static Variables in Java Programming:

We use static variables in Java Programming classes. And the static variables are just associated with class, as we know there also an object of every class. But we don’t use the static variable with the class object because it is not associated (connected) with object of the class. The keyword static is also used for some important purposes, as it never changes the condition. We can use it when we need to store some to data for long time and when we want to change it should be simple and easy to change it. There is an easy example in listing 1& 2 about the static variables of Java Programming.

Local Variables in Java Programming:

The name of the variable type is enough to understand its features. The local variable is used to declare in functions which we call them methods. And we can just use them in the method not out of the methods. For example if we declared a local variable in method A, so we can’t use it in method B we will just able to use it in method A. There is an example about the local variables in listing 1& 2.

Instance Variables in Java Programming:

When we need to declare any variable inside any class and we use it inside the class. It is called the instance of the class. As we know there are other two variables beside this. Static and local variables, but as we know the static is use to connected variables with class which could be use outside of the class. And the local is just use to declare inside any method and should be use there not outside of the method. Like them we declare the instance variable inside the class which we can use inside class anywhere. As we can use them in any method which is declared in the same class. Due to the instance variables we send and receive data with the help of the constructors. There is an example about instance variables in listing 1& 2.

Listing 1: Variables in Java Programming (BankDetails.java)

public class BankDetails{
public static String name = "NexonJoney Bank Account"; //Static variable with string primitive type
staticintCustomerNumber = 0; //Static variable with int primitive type
intAccountNumber = 345192;   //Instance variable with int primitive type
floatAccountBalance = 1500; //Instance variable with float primitive type
public void CustomerInformation()
  {
System.out.println("Your Account Number is: "+AccountNumber);
System.out.println("Your Account Balance is: "+AccountBalance);
  }
public void BankInformation()
  {
int value = 0; //Local variable with int primitive type
value++;
CustomerNumber = CustomerNumber + value;;
System.out.printf("You are the number %d Customer at our bank...\n",CustomerNumber);
  }
}

Listing 2: Variables in Java Programming (BankDetailsDisplay.java)

public class BankDetailsDisplay{
public static void main(String[] args){
BankDetailsAccountInformation = new BankDetails();  //declaring reference variable
AccountInformation.BankInformation();
System.out.println(BankDetails.name);
AccountInformation.CustomerInformation();
  }
}

Data Types in Java Programming:

The data types play a vital role in any Object Oriented Programming Language. Which we call it OOP Language. And during the execution of variables data, the compiler get the information about the variable checking its type (data type). And if you try to declare a variable without using any type so it will be bug(syntax error). There are almost two types of data types in Java programming languages as follows.

  • Reference Data Types
  • Primitive Data Types

Reference Data Types in Java Programming:

We use reference data types to use the data of any class in java programming language. As there is a class name CAR and if we use its object in the main method in java programming so it will be use like this: CARcar = new CAR();in this case the name in capital I use CAR to show you the difference between class and its object. So in this case the CAR in capital letters is class name and the car in small letters is object name. We commonly call the object as a reference variable. Because we can use the all data of the class CAR using its reference variable. If you check the listing 2, we also used the reference variable.

Primitive Data Types in Java Programming:

The primitive types in java are about to 8. But we use commonly 4 types as we used in C++ programming. i.e. Char, int, float, string. But here in java programming we should understand the 8 primitive types of Java Programming in details. Because it is very important to understand the use of different and very important primitive data types.

  • Byte
  • Int
  • Short
  • Long
  • Float
  • Double
  • Boolean
  • Char

Let’s we learn them separately and for their use in Java Programming Language check the listing 3 example for your knowledge and doubts about Java Programming primitive data types.

Byte:

In Java programming sometimes we use big or large type of arrays. Because we declare arrays in Java Programming according to our Java Application. So if we want to secure the memory, in this case we use Byte as a primitive type to save the space. Because Byte is two’s compliment integer and it is 8-bit data type.

Int:

You are fully familiar with int, it is used for whole numbers. But we can use the Byte in the place of int. because Byte is four times smaller than int. So if we use Byte in case of int, it could be better idea. The int is a 32-bit two’s compliment. For its details check the listing 3, but we also used the int in listing 1. You can also check the listing 1 for details.

Short:

The short primitive data type is also a type of integer. But it is 16-bit two’s compliment integer in Java Programming Language. And we use it for save the memory same like the Byte. But it is 2 times smaller than the int, as the Byte is 4 times smaller than int.

Long:

In Java Programming sometimes we need to store wide data in primitive data types. So in this case we usually use the Long data type in java programming. And it is 32-bit two’s compliment integer. Commonly where int fails to store wider data we use long.

Float:

Programmers are developing many types of applications in java programming.it is 32-bit two’s compliment floating point.But when we develop applications about calculation so we need to use float. Because in calculation we should take care about the decimal precision. In calculation every one want to clear about point to point calculation. For an example you can check the listing 1 for details about the float primitive data type.

Boolean:

For checking the bit priority we use the Boolean primitive data type in programming. There are just two states of Boolean as True or False. And we use this data type commonly in condition statements. Check the listing 3 for details about the Boolean.

Double:

As there is a difference between int and long same like that there is a difference in float and double. We use float for smaller values and we use double for wider data. And it is used for double decimal precision during the calculations. It is 64-bit two’s compliment floating point. For details about the double read the listing 3.

Char:

Sometimes we need to write a simple letter or take input from the user a letter. So for this purpose we use char data type. And it is being used commonly in java programming. it helps during the applications where we need to store data by grading. The char data type is 16-bit but it is Unicode character. Check the listing 3 for details as an example about the char data type.

Listing 3: Primitive Data Types in Java Programming

public class Variables{
public static void main(String[] args){
int a = 20; //integer variable containing whole number
float b = 0.5f; //float variable containing decimal number
char grade = 'A'; //charcter variable containing letter
longTotalPopulation = 198776; //long variable containing wide number
doubleTotalBalance = 12345.23; //double variable containing double precision floating point
    Byte c = -20; //byte variable containing negative number
    Boolean result1 = true; //boolean variable containing true state
    Boolean result2 = false; //boolean variable containing false state
    //displaying the data of all variables
System.out.println("\nIt is Integer: "+a);
System.out.println("\nIt is Float: "+b);
System.out.println("\nIt is Char: "+grade);
System.out.println("\nIt is Long: "+TotalPopulation);
System.out.println("\nIt is Double: "+TotalBalance);
System.out.println("\nIt is Byte: "+c);
System.out.println("\nIt is Boolean as True: "+result1);
System.out.println("\nIt is Boolean as False: "+result2);
  }
}
Result of Primitive Data types

Figure 1: Result of Primitive Data types

Conclusion

In this tutorial we learnt something important about the Java Programming. And that are variables. The use of variables and good knowledge about the variables is very important thing for a good programmer. As we learnt there are two types of data types in Java programming, i.e. Reference data type and Primitive data type. And in primitive data type there more 8 data types. Which we explained separately also used them in an example using Java Programming language.

We learnt about the variables there are three types of variables in Java programming. i.e. local variables, instance variables, and static variables. And we also learnt them in details separately also used them in and example using the Java Programming Language.



Java and Android application developer with more than 3 years experience.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login