Operators and Methods
Introducting the Operators - Con't
The comparison operators and the logical operators are as follows:
| Operator |
Meaning |
Example |
| == |
equal |
if(a==b)c=d |
| != |
not equal |
if(a!=b)c=0 |
| < |
less than |
if(a<b)c=d |
| <= |
less or equal than |
if(a<=b)c=d |
| > |
greater than |
if(a>b)c=d |
| >= |
greater or equal than |
if(a>=b)c=d |
| ! |
not (logical) |
if(!a)c=d |
| && |
and (logical) |
if(a==1&&b==2)c=d |
| || |
or (logical) |
if(a<10||b==0)c=d |
Provided that "a" is a boolean variable, you can write: if(a)b=c instead of if(a==true)b=c, and if(!a)b=c instead of if(a==false)b=c, this is shorter and quite common. Remember that the operators * and / go before + and -, so you will often have to put combined expression inside brackets.
The following example demonstrates some of the basic operators, both comparison and boolean (see the sourcecode to understand what is going on here):
|
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project5E extends Applet
{
public void paint(Graphics g)
{
int a=3;
int b=3;
int c=2;
int d=5;
boolean x=false;
g.setFont(new Font("Helvetica",Font.PLAIN,18));
if(a==b)x=true;
g.drawString("x = "+x, 110,30);
if(a==c)x=true;
else x=false;
g.drawString("x = "+x, 110,50);
if(a>c)x=true;
else x=false;
g.drawString("x = "+x, 110,70);
if(b>=c)x=true;
else x=false;
g.drawString("x = "+x, 110,90);
if(a==b&&c>d)x=true;
else x=false;
g.drawString("x = "+x, 110,110);
if(a==b||c>d)x=true;
else x=false;
g.drawString("x = "+x, 110,130);
}
}
|
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.
|