How to declare a variable in Java? The variable is declared in the following format: <type> <variable_name>;
Let's look at the following example(Variables1.java):
class Variables1 {
public static void main(String[] args)
{
int age; // <-- These are the variables:
String name; // variable age of type integer
// and variable name of type string
age = 20;
name = "John Doe";
System.out.println("Hi, my name is "+name);
System.out.println("I am "+age+" years old.");
}
}
You can examine there are two variables: age and name.
Of course you can declare more than two variables. If you want to declare
two or more variables of the same type, you can combine the declaration
as follows:
int var1, var2, var3;Well, that declaration says that we declare three variables: var1, var2, and var3. Be careful and keep in mind that Java is case sensitive, so pay attention to capital letters as well. Otherwise your program may not work.
To use variable, i.e. to make the variable store a value, simply put the equal sign after the variable name like the line age = 20; above. Of course, you will be able to use variables ONLY AFTER you declare them. However, it is not necessarily to have the variable declaration on the top of your program like the example above. You can declare them anywhere as long as before you using them.
There are some basic data types you should know. Note that this is not the basic in the technical sense, however, I just point out several data types that is necessary for you to know first.
{
: // Scope A
: // Suppose variable i is declared here
:
{
:
: // Scope B
: // Suppose variable j is declared here
{
: // Scope C
: // Suppose variable k is declared here
}
:
: // Scope B continued
: // Suppose variable l is declared here
{
: // Scope D
: // Suppose variable m is declared here
}
: // Scope B continued
}
:
: // Scope A continued
: // Suppose variable n is declared here
{
: // Scope F
}
}
If you create the variable on the scope A, it will visible on all the scope
below it. Like in the case of variable i above, it is visible
through all scopes. In the case of variable j, it is only visible
for scope C, D, and the rest of scope B, but
not scope A and F. In the case of variable k,
only scope C can use it, not even scope D. The same case
is variable m, which can only be used by scope D. For
the case of variable l in scope B, it can only be used
by the rest of scope B and scope D. Scope C
cannot use it because the variable is declared below scope C.
Same case is variable n, which can only be used by the rest of
scope A and scope F, but not scope B since the
variable is declared below B.
This scope concept is very important to keep in mind. I'd rather to explain it to you pretty early so that you won't find much difficulties later, especially in dealing with methods.
long E; int m, c; m = 5; c = 3000; E = m * c * c;Hey, that's the famous E = mc2 formula! You bet! :-) Of course you can do more complex equation with Java. These kind of equations will later be called expressions.
Keep in mind that Java doesn't have the ^ operator like BASIC. The power sign for Java will be discussed later.
Oh yes, in case you wonder this line:
System.out.println("Hi, my name is "+name);
System.out.println("I am "+age+" years old.");
Well, to print out variables onto the screen, simply put the plus sign
after the text quotes you want to display, like the first lane in the above
example. In the second line, suppose you want to continue with some more
texts, you may add the plus sign, and again put some texts. Don't forget
to wrap up the text with double quotes as well. If you just want to print
out the variables, then you can do like this:
System.out.println(myvariable);Hmm... pretty simple, eh?
Quick Links:
Have Java Problem
Do you have
a Java Question?
Java Books
Java Certification, Programming,
JavaBean and Object Oriented Reference Books
Best Regards,
Java Programming Hints and Tips
All the site contents are Copyright © www.sap-img.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.sap-img.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.sap-img.com or the content authors.