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

Operators

Java also provides several operators that you can use to manipulate your variables.

Mathematical Operators

As in all languages, Java allows you to perform math functions. The following table outlines the available functions:


Operator Description Example
+ Performs addition int x = 1;
int y = 2;
int z = x + y // z = 3
- Performs subtraction int x = 2;
int y = 1;
int z = x - y // z = 1
* Performs multiplication int x = 2;
int y = 2;
int z = x * y // z = 4
/ Performs division int x = 4;
int y = 2;
int z = x / y // z = 2
% Gets the remainder (modulus) int x = 6;
int y = 4;
int z = x % y // z = 2

One thing to remember about mathematics in Java is that since variables are typed, you should be careful to match types or expect automatic casting! In other words, Java will return the result in a type that fits in the result regardless of what was used in the equation.

Assignment Operators

As was alluded to in the last section, variables utilize assignment operators. However, if you are working with a numerical type, you have more assignment operators than simply "=". The following table outlines the other available assignment operators:

Operator Description
= Equality. Note, you can chain assignments such as:
x=y=x=0
+= Addition (x=1;x+=2; //x=3
-= Subtraction
/= Division. Don't forget about automatic casting! If your division returns a decimal number, it will be chopped to fit its type.
*= Multiplication
%= Modulus
^= Bitwise XOR
&= Bitwise AND
|= Bitwise OR
<<= Left Shift
>>= Right Shift
>>>= Zero Fill Right Shift

Incrementation and Decrementation

Java also provides the standard increment and decrement operators with prefix and postfix notation. Remember that the prefix operator modifies the variable before is it acted upon whereas the postfix operator acts afterwards. This is best seen by example:

int x = 3;
int y = 3;
int sum1 = 2 * x++; // sum1 is 6 and x is 4
int sum2 = 2 * ++y  // sum2 is 8 and y is 4
int sum3 = 2 * x--; // sum3 is 6 and x is 2
int sum4 = 2 * --y  // sum4 is 4 and y is 2

Precedence

Finally, Java maintains the usual operator hierarchy. The precedence table is shown below:

Precedence Operator Associativity
1 ++, --, ~!, +, -, (unary), (type cast) R
2 * / % L
3 +, -, + (concatination) L
4 >>>, >>, << L
5 <, <=, >, >= L
6 ==, != L
7 & L
8 ^ L
9 | L
10 && L
11 || L
12 ?: R
13 =, *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |= R

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

 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%.

IBM Gives a Mobile Voice to Developers
Inadequate Tools Send Software Down the Drain
USB 3.0 One Step Closer to Reality
Would-Be Linux Contributors May Get a Leg Up
SAP, Oracle Holding Out on Ubuntu?
GIPS Technology to Voice-Enable iPhone Apps
Citrix CTO Eyes the Future of Virtualization
The Pitfalls of Open Source Litigation
LiMo Open to Working With Google on Mobile
Google Gadgets Under Attack at Black Hat

The Guide to E-Mail Archiving and Management
Making XQuery Control Structures Work for You
Simplifying Composite Applications with Service Component Architecture
Overview: C++ Gets an Overhaul
Sharpening Your Axis with Visual Basic 9
Easier C++: An Introduction to Concepts
Simpler Multithreading in C++0x
Software as a Service Perceptions Survey - March 2007
Rackspace® Security
On Demand Media Technology Company Relies on The IT Hosting Leader to Provide Managed Microsoft Exchange

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