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

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs