Java - Variables and Arrays
From Global Programming Syntax
In this page you will be able to see how Java variables and arrays are defined and used. There are many simularities to the method of defining variables and arrays in Java but the main difference is that when defining arrays, you will see the [] brackets which tells the computer to define a array instead of a variable. Just a side note, if you choose to design the applet for a web browser then the following html code will be required to open the applet:
<applet code="filename.class" width="200" height="200"></applet>The html code will be required as a browser cannot directly open the applet without html code.
Java Variables
Defining variables in Java is very simular to defining variables in other complex non-auto-define language. There are many ways of defining a variable and to define number variables they are:
short myvariable; // a number between around -32768 and 32767. int myvariable; //a number variable. long myvariable; // defines a number that is too long the be defined by a int or short. double myvariable; // a number stored with double accuracy (less changes of been rounded off). float myvariable; // a number stored in the standard 32 bit form.
|
As you can see, there are many different types of ways to define a number variable which brings the question of which is the best one to use. Well that depends on the size of the number you are trying to use. A short integer (variable) stores numbers between -32768 and 32767 while the int define will store numbers between -2,147,483,648 and 2,147,484,647. For storing any numbers within a variable that are not between -2,147,483,648 and 2,147,484,647 you will need to use the long interger. You then may wonder what double and float are for. Well double doubles the accuracy of decimal points so if you have a long decimal point (eg. 3.1415926535898) while float is still accurate enough for shorter decimal points and probably one of the most commonly used with int. | ||||||||||||||||||||
While numbers are all fun with mathematics and all, there are also what are called strings. A string is basically just a group of letters and words. Below is a sample of different methods of defining a string in Java:
byte myvariable; //a single letter
char myvariable; //a single letter/number/character
String myvariable; //a string variable or set of characters
boolean myvariable //true or false variable
In the above example, byte is just a single US English letter while char is a single character (letter, number, symbol). Also the string method is a set of characters. This just leaves the boolean variable and a boolean variable can only have 2 possible values. The values are 'true' and 'false'.
Below is an example of a script using all of the above variables and placing them on screen:
class filename
{
public static void main()
{
short vara;
int varb;
long varc;
double vard;
float vare;
byte varf;
char varg;
String varh;
boolean vari;
//change the values from null
vara=1;
varb=127349634;
varc=40000000000;
vard=3.629004812964;
vare=95.589;
varf='i';
varg='/';
varh='this is a string';
vari=true;
System.out.printIn("short="+vara);
System.out.printIn("int="+varb);
System.out.printIn("long="+varc);
System.out.printIn("double="+vard);
System.out.printIn("float="+vare);
System.out.printIn("byte="+varf);
System.out.printIn("char="+varg);
System.out.printIn("string="+varh);
System.out.printIn("boolean="+vari);
}
}
Note that the above example will not work inside the browser java viewer as it uses a system output. To make the above Java applet/application work in the browser, you will need to use the following:
// required when you create an applet
import java.applet.*;
// required to paint on screen
import java.awt.*;
//the below name starts the applet with the files name called 'filename'.
public class filename extends Applet
{
//define the variables for all below methods.
short vara;
int varb;
long varc;
double vard;
float vare;
byte varf;
char varg;
String varh;
boolean vari;
public void init()
{
//in here is the general calculations
vara=1;
varb=127349634;
varc=2000000000;
vard=3.57;
vare=95;
varf='i';
varg='/';
varh="this is a string";
vari=true;
}
// This method gets called when the applet is terminated
// That's when the user goes to another page or exits the browser.
public void stop()
{
// meant be be empty.
}
// The standard method that you have to use to paint things on screen
public void paint(Graphics g)
{
//in here is the paint data.
g.drawString("short="+vara,10,20);
g.drawString("int="+varb,10,40);
g.drawString("long="+varc,10,60);
g.drawString("double="+vard,10,80);
g.drawString("float="+vare,10,100);
g.drawString("byte="+varf,10,120);
g.drawString("char="+varg,10,140);
g.drawString("String="+varh,10,160);
g.drawString("boolean="+vari,10,180);
}
}
