lesson1
What is java:
Java is a platform independent, object-oriented programming language. If you are new to the programming scene, this may mean nothing to you. The gist of it is that java is used to build applications (and applets) that can be used on all operating systems ( windows, linux, osX). It uses an object-oriented approach instead of the old top-down programming (like in Pascal). Objects will not be covered in this lesson.
What do we need to make a java program
a JRE (java runtime environment): Downloadable for free on the java website.
a IDE (integrated development environment): There is more than one way to write and compile your java files. You can use netbeans which has a drag-drop system for your visual components and generates a load of code for you. Or you if you prefer to know what you're doing (like me), you can use eclipse. I'm sure there are more IDE's out there, so if neither of these work for you, search the interwebs, I'm sure there is a java-match for you.
note: I am an eclipse-user. Some things are different between IDE's. I'll try explaining what i'm doing as best as I can, but if I'm giving menu-sequences, they won't work for you.
hello world
Great, we have everything we need to start building our own applications. Start you IDE and create a new project(File->new->java project). Call it java101. In this project start a new Package(right click on the project->new->package). Call this package examples. Finally in this package create a new class(right click on the package->new->class. Call it HelloWorld.
What follows is the code of the HelloWorld-application. explication of the line can be found after the '//'
- Code: Select all
package examples; //the package we're in
public class HelloWorld { //start of our class
public static void main(String args[]) { //start of our main-method
System.out.println("Hello world"); //print "Hello world" in the console
} //end of our main-method
} //end of our class
I know you're wondering why i called my method "public static void main(String args[])". This is where the application starts. Thus every java-application must have it (if you want to be able to run it).
variables
If you want to learn about variables in java you should be made aware that, unlike in php, java isn't loose typed. Meaning every variable is java must have a type. Luckily for us the java-language has provided us with some primitive types to work with:
- byte: a numerical value between -128 and 127. Uses 8 bit.
- short: a numerical value between -32,768 and 32,767. Uses 16 bit.
- int: a numerical value between --2147483648 and 2147483647. Uses 32 bit.
- long: a numerical value between -9223372036854775808 and a maximum value of 9223372036854775807. Uses 64 bit.
- float: use this for floating point values. Uses 32 bit.
- double: use this for floating point values. Uses 64 bit.
- char: a unicode character. Uses 16 bit.
- boolean:true or false
What follows is the HelloWorld-application using variables.
- Code: Select all
package examples;
public class HelloWorld {
public static void main(String args[]) {
String variable = "Hello world"; //declaring and initializing a variable of the type String
System.out.println(variable);
}
}
Declaring and initializing a variable of a primitive type is easy in java. First you say what type you want, then you name the variable. Finally you give it a value. some examples:
int i = 1;
boolean flag = true;
char value = 'a';
String text ="hey, this is easy";
As you can see, for all the mathematical values, you can put the desired number. However, if you need a char, then you need to use single quotes. Double quotes are used for Strings.
go with the flow
1: conditions
Often in programming you'll find that you want something to happen only if a condition is met. Have no fear, java has, once more, got a solution for you. The if-statement.
- Code: Select all
if (condition) {
things to do if the condition is met.
}
Then there are those times that you want to do one thing if a condition is met and something entirely else if it isn't.
- Code: Select all
if (condition) {
things to do if the condition is met.
} else {
things to do if the condition is not met.
}
finally there are those times that there are numerous outcomes possible. For those times java has the switch statement.
- Code: Select all
Switch (variable) {
case (value1):
things to do if (variable) = (value1);
break;
case (value2):
things to do if (variable) = (value2);
break;
case (valueN):
things to do if (variable) =(valueN);
break;
default:
things to do if none of the previous values where correct;
}
2:loops
Sometimes we want to do a set of statements more than once. Java provides us with 3 ways to do this. The first type of loop is the for-loop. We use this loop if we know how many times we want to go through our loop.
The following code shows how to go ten times through a loop and write the value of i in the console.
- Code: Select all
for(int i=0; i<10; i++) {
System.out.println(i);
}
This is the first time we used ++. It is a simplified way of writing i=i+1. There is also i-- (i=i-1).
Alas we don't always know how many times we want to go through our loop. For those times java provides us with the while-loop and the do-while-loop. The difference between these two is that the while loops checks if a condition is met before it goes through it's first loop. The do-while-loop goes through its first loop and then checks if it has to go through it again. Therefore a do-while-loop is executed at least once.
- Code: Select all
//the while loop
while(condition) {
things to do;
}
- Code: Select all
//the do-while-loop
do {
things to do;
} while (condition);
Be aware that you should always (at some point in your loop) change the value of your condition so that it is met. Otherwise you will write an endless loop and your program will not function beyond that loop.
operators
1: assignment operator
- assign = (int number = 12;)
- plus +
- minus –
- multiply *
- divide /
- mdulo %
- bigger than >
- smaller than <
- bigger than or equal >=
- smaller than or equal >=
- equal ==
- not equal !=
- and &&
- or ||
- not !
- exclusive or ^
- and (*) &
- or (**) |
(**)only evaluates the second part of a statement if the first part is false
5: bit-wise operators
- and &
- or |
- exclusive or ^
- complement ~
- left shift <<
- right shift >>
- unsigned right shift >>>
If interested I'll continue this.
If you found mistakes, send me a pm. I'll adjust and mention your name somewhere






