Declaring, Assigning, and Casting Variables
Declaring Variables
As we just said, if you want to use a variable, you must specifically declare its type.
To declare a variable's type you simply use the type followed by the variable name.
Consider the following examples:
byte b;
short age;
long nationalDebt;
boolean isMale;
You can also declare multiple variables of one type in one expression such as in the following example:
int age, yrsEmployed, numChildren;
Variable Assignment and Initialization
Once you have declared the type of a variable, you are free to initialize it and assign to it some value.
Assignment and initialization is simple.
You simply use variable name = some value.
For example, consider the following code:
int age;
age = 28;
Of course you can also declare variables and assign values to them at the same time using the following syntax:
int age = 28;
Casting (Changing from one type to another)
What if you want to multiply 2 x 1.5?
Or more generically, int x double?
Will the result be an int or a double or something else?
Well, when in doubt, Java will convert to the less restrictive type to be safe.
Thus, in the above example, the result will be a double since double is less restrictive.
The following chart shows how types will be caste if possible.
byte --> short --> int --> long --> float
--> double
However, what if you are going the other way?
Suppose you have two doubles and you want to make an int out of the product
To do this type of caste, you simply perform an assignment using the type to be casted to in parentheses before the value to be casted.
Consider the following example in which we caste from a double to an int:
double d = 123.456;
int i = (int) d;
In this case, it will be assigned a value of "123".
NEXT
Selena Sol contributes to the JavaBoutique's Introduction to Java. Selena curently works for Barclays Capital in London, one of the leading global investment banks in Europe and has worked as a software developer for the National Center for Human Genome research, Microline Software, Neuron Data, and Electric Eye in Singapore. Selena is perhaps best-known for creating the Public Domain Web Script Archive (Extropia) and writing several books on Web Programming (Perl, CGI, Java).
Email: selena@extropia.com
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|