advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Articles : JavaBoutique's Introduction to Java :
Data Types and Operators :

Contents
Comments
Printing to Standard Output
Variables
- Data Types
- Declaring, Assigning, and Casting
- Variable Type Helper Objects
Strings
Arrays
Operators
Flow Control

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

How to Add Java Applets to Your Site

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.

 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 33
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

The Pitfalls of Open Source Litigation
LiMo Open to Working With Google on Mobile
Google Gadgets Under Attack at Black Hat
IBM, Linux and the Microsoft-Free PC
Opengear's Open Source Odyssey
Sun Moves JavaFX Closer to Primetime
Will LSB 4 Standardize Linux?
Making a Case for an Android-Symbian Merger
Ubuntu Launchpad Opens Up Development
BT Buys Voice Software Startup

An Introduction to F# for Functional Programming
The Basics of REALbasic, Cross-platform RAD Tool in the Mold of VB
Silverlight Streaming: Free Video Hosting for All
What's New in ASP.NET 3.5?
Putting SharePoint to Work for You
Putting Intel® Threading Building Blocks to Work
Achieve the Best of Two Worlds with Behavior-Driven Development
Understanding Windows Mobile Security
RODCs Transform Branch Office Security
SQL Server 2008 Express: The Free Database from Microsoft

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers