CHAPTER 4
|
Type | Storage | Range |
---|---|---|
int | 4 bytes | -2,147483,648 to 2,147,483,647 |
short | 2 bytes | -32,768 to 32,767 |
long | 8 bytes | |9x1018| |
byte | 1 byte | -128 to 127 |
float | 4 bytes | |3x1038| 6 sig digits |
double | 8 bytes | |10308| 15 sig digits |
char | 2 bytes | Unicode a denoted by 'a' |
boolean | . | true false |
Type | Name | Example |
---|---|---|
+ | addition | 2+3 is 5 |
- | subtration | 3-2 is 1 |
* | multiplication | 3*4 is 12 |
/ | division | 12/3 is 4 |
% | modular arithmetic | 13 % 5 is 3 |
+= | increment by | x += 4 sets x to x + 4 |
Math.pow(,) | import java.lang.Math must be used | Math.pow(x,3) computes x cubed |
Type | Name | Example |
---|---|---|
= = | is equal to | (2= =3) is false (2= =1+1) is true |
!= | not equal | (2!=3) is true (2!=1+1) is false |
< | less than | (3<4) is true (3<3) is false |
> | greater than | (3>4) is false (4>3) is true |
int i=1;
do{ // begin do loop
i++;
i=2*i;
}while(i<100); //end do loop
This loop will change i to 2 then multiply by 2 to get 4.
The second execution will
change i to 5 then multiply by 2 to get
10 and so forth until i is 100 or greater.
int i=1;
while(i<100){ // begin while i is less than 100 is true
i++;
i=2*i;
} // end while
j=1;
for(int i=1; i<100; i++){ // begin for i
j=j+2;
} //end for i
if(j==1){j=2;} //simple if
if(j==2){ // begin if j is 2
    j=j+2;
  } //end if j is 2
  else if(j==3){ //begin if j is 3
      j=j+3;
    } //end else if
else { j=100; i=200;} //end else - end block
switch(count){ \\begin case checks
      case 1: note="one"; break;
      case 2: note="two"; break;
      case 3: note="three";break;
      default: note="na";break;
             } //end case checks
if(event.target==square_button){ //begin square draw
        Graphics g=this.getGraphics();
        g.setColor(current_color);
        g.drawRect(10,10,40,40);
        return true;
        } // end square draw
One might say that the minmal characteristics of the square are the upper left hand corner. and the length of a side. We could the define the class
public class Square{
  public int x;
  public int y;
  public int side;
    }
We could then obtain an instance of this class object with the declaration.
    public Square sq=new Square();
We can now store data and retrieve data from our Square class.
sq.x=10;
sq.y=10;
sq.side=30;
if(event.target==square_button){ //begin square draw
        Graphics g=this.getGraphics();
        g.setColor(current_color);
        g.drawRect(sq.x,sq.y,sq.x+sq.side,sq.y+sq.side);
        return true;
        } // end square draw
The problem is that this will only allow us to use one set of data. That is any new instance of Square will point to the same data. To implement more usable data classes we must instruct the compiler about the method of cloning or replicating the data structure.
public class Square implements Cloneable{
  public int x;
  public int y;
  public int side;
        public Object clone(){ //begin clone
        try{ //begin try
        return super.clone();
        } //end try
        catch(CloneNotSupportedException e){ // begin catch
        return null; //if there is a catch
        } //end catch
        } //end cloning structure for point
  } // end def of Square
Note that the definition must say -implements Cloneable-. This allows java to invoke the super.clone method. Now we can define two different instances of Squares.
    public Square sq1=new Square();
    public Square sq2=new Square();
If the data structure is more complicated the super.clone method will not suffice. Consider the following example:
public class point implements Cloneable{
public int x;
public int y;
public Object clone(){ //begin clone
try{ //begin try
return super.clone();
} //end try
catch(CloneNotSupportedException e){ // begin catch
return null; //if there is a catch
} //end catch
} //end cloning structure for point
} //end point def
public class Vcirc implements Cloneable{
public point c=new point();
public point v=new point();
public int s;
public boolean sel=false;
public Color cl=Color.black;
public Object clone(){ //begin clone
try{ //begin try
Vcirc vc=(Vcirc)super.clone();
vc.c=(point)c.clone();
vc.v=(point)v.clone();
return vc;
} catch(CloneNotSupportedException e){
return null;
}//end catch
} //end cloning structure for virtual circle c is center, v is vector
} //end virtual circle def
-
Java Language Specifications
-
TOC
TABLE OF CONTENTS