These are types of the variables we declare. We distinguish:
boolean, which is 1-bit long can take values of true or false
boolean isRaining = false;
char, which is a 16-bit long and therefore can store 2 to the16th = 65,536 different characters.
char firstLetter = ‘A’;
int, which is 32-bit long defined integer (whole) numbers
int pixel = 25;
double, which is 64-bits long defined fractional numbers
double pi = 3.14;
There are also the data types short, long, and float which are used in special cases (discussed later when needed). One more data type is:
String, which is a collection of characters used for words and sentences
String myName = “Kostas”;
Be aware that a string is initialized with text within quote (different from chars)
When we declare a variable (which is a name we make up) we also need to tell what is its type and (sometimes) to give it an initial value.
type aName = value;
Variables are names that we choose and they have to make sense. If we will declare a variable for the sum of two numbers we should called it sum. Variables usually start with lower case and when composite use upper case for the next word. For example:
sum or mySum or theSumOfTwoNumbers
For Booleans we usually start with the word “is”. For example:
isRaining or isRainingToday
For example, you may want to declare information about a person you can use the following types, variables and initializations.
string hisName = “John”;
int hisAge = 35;
double hisMoneyToday = 243.99;
boolean isHeMarried = false;
All the basic mathematical operations are available in Java:
Addition, subtraction, multiplication, and division.
For example, to get the sum of two numbers we can write:
int sum; // not initialized cause we do not know how much
sum = 5 + 6; // now sum is 11
Notice the two slashes. They represent comments. Anything after // is ignored by java until the end of the line. Therefore // is for one line comments. If you want to write multi line comments use the /* to start and the */ to end. For example:
/* this statement is ignored
by java even though I change
lines */
// this is ignored until the end of the line
The multiplication symbol is * and the division is /. For example:
Double result;
result = 0.5 * 35.2 / 29.1;
One extra operation of interest is the modulo (%) operation. That is the remainder of the division of two numbers:
int moduloResult;
moduloResult = 10 % 2; //the result is 0
moduloResult = 9 % 2; //the result is 1
Logical (or Boolean) operations define the truthfulness of something. For example the word “if” represents a guess that needs to be tested. In Java if - statements have the following format:
if( condition ) {
…;
}
else {
…;
}
The conditions can be one of the following: equal, not equal, greater, and smaller.
These four conditions are represented by the symbols (a and b)
if(a==b) // if a is equal to b
if(a!=b) // if a is not equal to b
if(a>b) // if a is greater than to b
if(a>=b) // if a is greater than or equal to b
if(a<b) // if a is less than b
if(a<=b) // if a is less than or equal to b
To combine condition we use the AND and OR operators represented by && and || symbols. For example
if(a>b && a >c) //if a is greater than b and a is greater than c
if(a>b || a >c) //if a is greater than b or a is greater than c
Here is an example of an if condition:
String userNname = “Kostas”;
boolean itsMe;
if( username == “Kostas”) {
itsMe = true;
}
else {
itsMe = false;
}
A loop is a repetition statement that allows one to declare a start, end, and step. The syntax is as follows:
for(start condition; end condition; increment step){
….
}
The start condition is the initial number to start counting
The end condition is the number to end the counting
The increment step is the pace of repetition
For example a loop from 0 to 99 is:
for(int i=0; i<100; i++){
…
}
The starting condition is int i=0;
The end condition is i<100;
The step is i++;
(i++ means add one every time. i— means subtract 1 every time. These two statement can also be written as i = i +1; and i =i-1; )
An array is an ordered set of data. We can have arrays of boolean, integers, etc. We define an array by using the [] symbol. For example:
int[] evenNumbers = new int[50];
String[] listOfNames = new String[45];
The above arrays define 50 and 45 element respectively. The word new is there to create and initialize the arrays. Once we created an array we can fill it with data and then access them. For example:
for(int i=0; i<50 i++){
evenNumbers = i * 2;
}
To access the 31st element of the array:
int thirtyFirstElement = evenNumbers[31];
That will return the 31st element (which should be 62).
If we have a 2-dimensional array we initialize it as:
int twoDArray[][] = new int[5][100];
and we access the same way:
int someElement = twoDArray[2][18];
When we write code in Java we occasionally want to group statements that do something useful and then call a command that will execute the group of statements. For example, we may want to group statements that count to 100 and then call a command (or a method-command) that will execute the grouped statements. Here is how it can be done
int countTo(int until){
int myResult;
for(int i=0; i<until; i++){
myResult = i;
}
return myResult;
}
In the above example, we have declared a method call countTo and we pass it an integer value called until. After the method does the looping and assigns the values to myResult it then returns the result.
To invoke (call) the method from another part of the code we write:
int x = countTo(100);
And method countTo will do the rest.
This can be very useful in organizing code, through statements and commands that call one another. If you want the square root you can call
double myNumber = squareRoot(25);
And squareRoot will do the rest (with or without your knowledge/supervision).
Sometimes methods can be very complex such as morph(object a, object b) or very simple such as countTo(int until).
A class is a collection of methods and data in a way that it forms a complete entity.
It looks like this:
class x {
data;
method( ..){
…
}
}
For example, a pixel can be a class that has as data a color and an x and y screen position. It also can have a setColor and getColor methods.
class Pixel {
int x, y; //Data
Color c; //Data
void setColor(Color aColor){ // method 1
c = aColor;
}
Color getColor() { // method 2
return c;
}
}
The setColor method takes void in the beginning because it does not return any value. In contrast getColor returns a Color. In the setColor method we pass a color aColor which is assigned to c and in the getColor method we do not pass anything.
A class should be in a file with the same name (in this case pixel.java). To call the methods of pixel from another part of the code we first initialize a pixel class and then we call the methods using a dot (.). For example:
Pixel myPixel = new Pixel();
myPixel.setColor(Color.red);
Color myColor = myPixel.getColor();