eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

Introduction to Web Programming
Operators  

  • As was the case in Perl, Java 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:

OperatorDescriptionExample
+Performs additionint x = 1;
int y = 2;
int z = x + y // z = 3
-Performs subtractionint x = 2;
int y = 1;
int z = x - y // z = 1
*Performs multiplicationint x = 2;
int y = 2;
int z = x * y // z = 4
/Performs divisionint 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 = 5

  • 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:

OperatorDescription
=Equality. Note, you can chain assignments such as:
x=y=x=0
+=Addition
-=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:

PrecedenceOperatorAssociativity
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

Previous Page |Next Page