Variables are the key to any program. There are variables called registers inside every CPU (Central Processing Unit). Every program ever written uses some form of variables. Believe it or not, the way you use variables can significantly impact your program. This section is a very simple introduction to what variables are, and how they're used in programs.
boolean t;
byte b;
char c;
int i;
long l;
't'
is declared as boolean
type, and 'b'
as of byte
type, etc.int
type, can be stored in a 32 bit hardware register.public class pSimpleObject{
int i;
public pSimpleObject(){
i = 0;
}
public int get(){
return i;
}
public void set(int n){
i = n;
}
}
public
, this means that it can be visible to other objects outside it's file. We later say that it's a class
, and give it's name, which in this case is: pSimpleObject
. Inside of it, the class contains an integer named 'i'
, and three functions. The first function named pSimpleObject()
, is the constructor. It is called every time an object is created using this class. The set()
and get()
functions set and get the value of 'i'
respectively. One useful terminology is that functions in objects are not called functions, they're called methods. So, to refer to function set()
, you'd say "method set()
." That's all there is to objects!pSimpleObject myObject;
myObject = new pSimpleObject();
pSimpleObject myObject = new pSimpleObject();
myObject
, of class pSimpleObject
, and later instantiate it (a process of actual creation, where it calls the object's constructor method). The second approach illustrates that it all can be done in one line. The object does not get created when you just declare it, it's only created when you do a new
on it.