JAVA Programs – 3 Ways of Assigning Value to Variables

In Java we can input the value of a variable in 3 different ways, in this article we will see all the ways in details with an example of each. You can use the way that best suits your program and requirement.

Check: What is JAVA And How To Write & Run Your First Program

  1. Constant values assigned during writing the program code.
  2. Assigning values as command line argument, before execution of the program.
  3. Assigning values at run time.

 

Assigning Value as Constant

This is the first and the most basic method of initializing values to the variables in any programming language, in this method we basically assign values while writing the program code itself.

It is very widely used for basic programs to make students easily understand the working of a program or it can be also be used for a variable whose value changes rarely, such as interest rates.

There are some advantages and disadvantages of using this method, let’s take a look:

Advantages Assigning Value as Constant:

  • No need of memory readers (BufferedReader & InputStreamReader)
  • Faster execution time
  • Higher security

Disadvantages:

  • To change the value you will have to edit the source code every time & compile it again.

Example of Assigning Value as Constant

This is a sample program for assigning value as constant in a Java program, in this program, we will assign value to x  and y  variable directly in the code and then add it and print it on the screen.

As you can see that we have provided the 2 constant values in the program and now the program can sum only given two number i.e 10 and 20. Each and every time you execute the code you are going to get the same output.

But wait, what if you wish to assign value later, after writing the code. Don’t worry, that will be discussed in the second type of assignment below.

Assigning Value as Command Line Argument

In this method we assign the values to a variable before the execution of the program, using command line argument while running the program in command prompt.

Advantages:

  • You can change values every time
  • No need of memory readers (BufferedReader & InputStreamReader)

Disadvantages:

  • Slower than first method
  • Gets Messy, when taking lots of input

Example of Assigning Value as Command Line Argument

How to run a program with command line argument ?

command-prompt-command-line-argument

Step 1. Write your code in notepad

Step 2. Save it at Java>jdk>bin folder (if you don’t have the path set yet) as add.java

Step 3. Compile using javac add.java

Step 4. Run using java add 2 6

* Here, 2 & 6 is the values for the x & y variables.

You will see as output on the screen.

Now, you may be thinking about taking the input from the user when the program is actually running. For that we will apply a third type of assignment method that is on Run Time.

Assigning Value at Run Time

This is method is the most common method used to assign values to variable at run time of the program, it asks for input from the users where required. In this method “BufferedReader” class is used to read data from the buffer memory which is the part of ‘io’ sub package.

Advantages:

  • Different values can be assigned each time
  • Faster on compile time

Disadvantages:

  • Larger code when compared to the other two methods

Example of Assigning Value at Run Time

You can simply run this program using the java classname command on command prompt, then it will ask for two numbers one by one and will show you the sum.

Also check our previous article What is JAVA And How To Write & Run Your First Program to know some basics about compiling & running a Java program on Command Prompt. And let us know if you face any problem by leaving a comment below.

January 13th, 2016

0 Responses on JAVA Programs - 3 Ways of Assigning Value to Variables|

    Leave a Message

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

    CommentLuv badge

    Pin It on Pinterest