Variables on Java

Java LogoA typical program uses various values and these values keep changing while the program is running. For example, you create a program that is used to perform calculations, the values entered by one user will obviously be different from the values entered by another user. This also means that, when creating the program, you cannot know all possible values that will be entered in your program. You should still be able to manage the values that the users will eventually enter in your program.If you create a program used to perform calculations as introduced above, when a user enters a new value that would be involved in the calculation, to manage that value, you can (temporarily) store it in the computer memory. Since the values entered in a reserved memory area change regularly, they are called variables. Because neither you nor the compiler can predict all possible values that would be used, there are safeguards you can use. First, you must ask the compiler to reserve an area of memory for a value you intend to use. Asking the compiler to reserve an area of memory is referred to as Declaring a Variable. Remember that when you declare a variable, the compiler reserves an area of the compiler memory for you. Eventually, you can put the desired but appropriate values in that memory space.After declaring a variable, when you need the value stored in its memory area, you can ask the compiler to retrieve it and hand it to you. To effectively handle this transaction, the compiler would need two pieces of information from you: a name of your choice for the memory area that will be reserved, and the type of value that will be stored in that area of memory. Based on this, the formula to declare a variable is:

TypeOfValue VariableName

As done in some languages like Pascal or Basic, we will start with the name.

The Name of a Variable

When you want the compiler to reserve an area of memory for some values used in your program, you must set a name, also called an identifier, that will allow you to refer to that area of memory. The name can be anything of your choice but there are rules you must follow:

  • The name of a variable can be made of one letter (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, or Z) only
  • The name of a variable can start with a letter, an underscore “_”, or the dollar sign $. The name cannot start with a digit. If the name starts with an underscore, the second character must be an alphabetical letter
  • After the first character, the name of the variable can include letters, digits (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9), or underscores in any combination
  • The name of a variable cannot be one of the words that the Java languages has reserved for its own use. A reserved word is also called a keyword. This means that you cannot use one of the following keywords to name your variable:
abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends final finally float
for goto if implements import
instanceof int interface long native
new package private protected public
return short static strictfp super
switch synchronized this throw throws
transient try void volatile while

Beyond these rules as a foundation, you can add yours. For example, we will follow suggested standards of the Java documentation. The rules will follow are:

  • A name will start with a letter in lowercase. Examples are age, f4, name, g_14, country
  • When a name is a combination of words, only the first name will start in lowercase. Examples are firstName, dateOfBirth, pi_314159
  • When the name is an abbreviation, we will use uppercase on all characters. Examples are EAU, UN, CIA, NSA

Variable Initialization

After declaring a variable, you can store a value in the memory reserved for it. When you have just declared a variable, it may not hold a significant value. To know the value it has, you should put an initial value into that memory space. Putting an initial value is referred to as initializing the variable.

To initialize a variable, on the right side of its name, type the assignment operator, followed by the value you want to put in the reserved memory. As we will see in the next few sections, you cannot and should not put just any type of value in a variable. We will see that there are different types used for different variables.

After declaring a variable and once it has a value, to display that value, you can provide the name of the variable to the parentheses (in future lessons, we will learn that this is referred to as passing) of the System.out.print() method.

Source : functionx.com

Iklan

Satu pemikiran pada “Variables on Java

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s