Java Variables with programming Examples

What are the variables in Java?

Java Variables:- So far, our sample programs have all been simple. That is exactly what you do with Java variables. The best way to think of a variable is like a mailbox: A mailbox is in The principle is nothing more than a container that is assigned a unique key in usually the post office box number – is marked and in which we put something can. Later we can use the unique key the Postbox number – find the postbox and click on the one stored there Access content.

Java Variables

However, one should always be aware of the fact that we are dealing with very special mailboxes in which there are never several Letters can lie and the letters of only one, exactly on the respective Fan can accommodate suitable size or shape. It is the same with Java variables: A variable is a memory location. Using a unique key, in this case, the variable name we access a variable and thus the memory space. The variable name is therefore a kind of symbolic address (our post office box number) for the Storage space (our mailbox). You can give a variable a certain content assignment and read it out again later. That’s basically all we do need to make our programs a little more interesting. In the above figure illustrates this state using the variable b, which is the integer value 107 includes.

How do you declare Java variables?:

Before you can use a variable in a Java program, you must first create the variable, by declaring their names and the type of information the variable should store. The first thing is the type of information given, followed by the name of the variable. After this paragraph you will see a few Examples of variable declarations:

int highScore;

String username;

boolean gameOver;

Local Java variables can be declared at any point in a method, just like any other Java statement. Before they can be used, however, they must be declared. Usually will Place variable declarations directly after the statement that declares a method.

In the following example, three Java variables are declared at the beginning of the main () method of the program:

public static void main (String arguments []) {

int total;

String reportTitle;

boolean active;


If you declare multiple Java variables of the same type, you can do so on a single line. To do this, separate the individual variable names with commas. The following statement creates three string  with the names street, city ​​and state:

String street, city, state;

Variables can be assigned a value when they are created. To do this, use the equal sign (=) followed of the value. The following statements create new  Java variables and assign values ​​to them:

int zipcode = 11111;

String name = “fawad khan”;

boolean cheatedOnKelly = true;

int age = 26, height = 5.7, weight = 79;

As the last statement suggests, you can assign values ​​to multiple variables of the same type by using them separated by commas. Local  Java variables must be assigned values ​​before they can be used in a program. Otherwise the program cannot be compiled successfully. Because of this, it is a good habit assign initialization values ​​to all local variables.

Instance and class variables are automatically given an initialization value – depending on the type of Information these should include:

Numeric variables: 0

Character variables: ‘\ 0’

Boolean variables: false

Object variables: null

Naming  Java variables:

Variable names in Java must begin with a letter, an underscore (_), or a dollar sign ($). You must not start with a number. After the first character, variable names can be any combination of Contains letters and numbers.

Note: Java also supports the Unicode character set, which includes the standard character set plus thousands of other characters, to represent international alphabets. Accented characters and other symbols can be used in variable names can be used as long as they have a Unicode number above 00C0.

When you name a variable and use it in a program, it is important to remember that Java is case sensitive. That means the use of upper and lower case letters must be consistent. For this reason there can be one variable X and another variable x in a program and a abc is not Abc is not a ABC.

 Java variables are often given meaningful names that include consist of several connected words. To make it easier to recognize individual words within the name the following rule of thumb is used:

  • The first letter of a variable name is lowercase.
  • Each subsequent word in the name begins with a capital letter.
  • All other letters are lower case.

The following variable declarations conform to these rules:

Button loadFile;

int areacode;

boolean playerSetNewHighScore;

Let’s consider a simple

Java Variables

The calculated perimeter and the area are divided into two more Variables stored in terms of scope and area. Two System.out.println Instructions are used to print the data on screen.

 Programming example:

suppose we wanted to be in a program Calculate how much money we got in our part-time job in the last week have earned. Our hourly wage in this job is 15 EUR, and last week we worked a total of 18 hours. Then we could with our previous knowledge to tinker a program of the following kind:

The program can of course be compiled without any problems, and it also generates the following (correct) output:

Java Variables

One possibility would be simply that To change the number of working hours in the program – but we would have to do now in two places, namely in lines 4 and 8 each with the value 18, 12 change. It can easily happen after three (four, five, …) weeks we forget to change one of the two lines, or that we are in one of the Mistype lines. So it would be better if we counted the number of hours worked would only have to change at a single point in the program. Same thing Of course, this applies to wages, which will (hopefully) also change at some point elevated. This is exactly where  Java variables come into play. To be able to use  Java variables in our program, we have to use Java First tell the compiler what the variables should be called and what kind (i.e. what type) of values ​​we want to store in them so that appropriate Storage space can be provided. We call this instruction also as a declaration. To stay with the post box analogy: We have to Set up the mailbox with a certain size (letter box, parcel box, …) first and provide it with a unique post box number. Such a variable declaration always has the following form:

Syntax rule


“VARIABLE TYPE” always corresponds to either a simple data type (byte, short, int, long, float, double, char or boolean), a field type or a class name (learn what the last two variants mean You later). ≪VARIABLE IDENTIFIER≫ is a unique character string, which corresponds to the rules for identifiers. It has natural, variable names in Java in lower case letters and without special characters to write down. It starts with a lowercase letter and capitalize each new word within the identifier, such as in great variable designation. We want to adhere to this in the future. To assign values ​​to such variables, we use the assignment operator =. Of course, we can only assign values ​​to a variable that are are within the value range of the specified variable type.For example, we could create a variable a of type To declare int and assign it the value 5, write the following:

int a;

a = 5;

If you immediately put a value in the variable after a variable declaration want to write, the following short form is also allowed in Java:

int a = 5;

With this we have mastered two tasks at once, namely

  1. the declaration, setting up the variables, and
  2. the initialization, setting the first value of the variable.

Back to our example. We wanted the program Hour Calculator like this rewrite that the number of hours worked only on one Position appears. The solution for this lies in the use of a variable for the number of hours. The program now looks like this:

Java Variables

Lines 4 and 5 contain the required declarations and initializations of the  Java variables number of hours and hourly wages. On lines 8, 10 and 12 the values ​​are now only accessed via the variable name. In order to sufficient if we want to calculate our new weekly wage next week, the change of a single program line. Sometimes it can be useful to arrange  Java variables according to their value the initialization cannot be changed in the further program. One then speaks of so-called final  Java variables (or symbolic Constants). To such an immutable or symbolic variable To declare constants, one has to follow the usual declaration with initialization put the key word in front:

Syntax rule


So in our example program above, did we want to ensure that our Variable hourly wages becomes a symbolic constant, so we could simply with

final int HOURS_WAGE = 15;

Read input and output  Java variables:

The introductory example of this article, the calculation of scope and the area of ​​a rectangle, has one serious flaw: length and width are fixed in the program. To the scope and the To calculate the area of ​​another rectangle, the program must be changed and recompiled. It would be much more elegant if the user when executing the program by latitude and longitude would be asked and could enter this data yourself.

That sounds more trivial than it is in reality, and leads to the complex “Input and Output,” which we discussed in later in detail. This article is just an Anticipation that shows how to get java variables data.

Related Article:

Java Arrays

Java Layout Manager

Java Font Class

java Color Class

Related Articles

Leave a Reply

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

Back to top button