All tutorials are implemented in text (here), and also with exercises and multimedia in the Tutorial Player. The main home page has a search engines, and more facilities.
Data TypesJava has eight primitive which are part of the actual language, and they are not objects.
byte short int long float double char boolean
Class is also a type.
Comments are coded with a // or /* */. For example:
// This is a comment /* This is a comment over several lines */
The following are escape codes used in Java:
\n New line \t Tab \\ backslash \" Double quotation mark \' single quotation mark
For example to code
String myString = "Peter said \"Go Home\"";
Java uses the operators shown below:
+ Addition - Subtraction * Multiplication / Division % Moduls
Java uses = for assignment, and allows the following:
thisBalance = oldBalance = finalBalance = 4000;
This sets all balances to 4000.
Java uses the following operators
+= -= *= /=
Examples:
thisBalance += oldBalance; is the same as thisBalance = thisBalance + oldBalance; thisBalance -= oldBalance; is the same as thisBalance = thisBalance - oldBalance; thisBalance *= interestRate; is the same as thisBalance = thisBalance * interestRate; thisBalance *= numberPayments; is the same as thisBalance = thisBalance / numberPayments;
i,j,k are commonly used in loops, and we can use the ++ and -- to increment and decrement. For example:
i++; this adds one to i i--; this subtracts one to i;
If we use an assignment operator, with can do the following;
i=10; j = i++; // This puts i in j, and then adds one to i, therefore j = 10 j = ++i; // This adds one to i, and then makes the assignment, therefore j =11;
The following operators are possible for comparsions:
== equal != not equal < less than > greater than <= less than or equal to >= greater than or equal to
These operators are used in logical statements.
&& is AND - only needed expressions are evaluated || is OR & AND - all bracketed expressions are evaluated | is OR
Let's see this working:
badDebtor = true; balance = 20000; goodCustomer = true;
boolean badCustomer = badDebtor | (balance < 10000); this sets badCustomer to false
If I use
badDebtor = true; balance = 20000; goodCustomer = true;
boolean badCustomer = badDebtor || (balance < 10000);
This sets badCustomer to false, but this time Java does not evaluate the second condition. This is because the badDebtor status makes the customer a bad one, regardless of the balance.
Strings can be added using the + operator:
String customerName = "Joe Harris"; String customerTitle = "Professor"; String customerSalutation = customerTitle + customerName;