From OO Concepts to Java Programming
package fr.tbr.exercises;
public class JavaSyntaxDemo {
private String demoVersion = "1.0_DEV";
//Constructor
public JavaSyntaxDemo() {
}
public String getDemoVersion(){
return this.demoVersion;
}
}
// This is a line comment
/* This is a block comment
it can contain several lines*/
/** This is a piece of javadoc, we will see this in detail after*/
... ; //In Java, each statement must be finished by a semicolon
package fr.tbr.exercises;
// It provides a unique namespace to the class, and helps to locate the class
Identity identity; // you should place the type name before the field
//Constructor declaration
Identity(){
}
void test(){
Identity identity = new Identity(); //constructor call thanks to the "new" operator
}
String
double
String
and double
instantiations
String message = "Hello World";
double amount = 10.2;
In Java, you can meet two "kinds of type"
Object
class. Your own objects
belong to that category
Type | Description | Range |
---|---|---|
byte
|
a signed byte |
-128 to 127 |
short
|
This is a two-bytes signed integer, it defaults to 0 if not initialized |
-32,768 to 32,767 |
int
|
This is a signed integer, it defaults to 0 if not initialized |
-2,147,483,648 to 2,147,483,647 (inclusive) |
Type | Description | Range |
---|---|---|
long
|
The long is a signed integer with a wider range. The declaration of a long is a bit
different, you should do it as described below
|
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Type | Description | Range |
---|---|---|
float
|
The |
1.40129846432481707e-45 to 3.40282346638528860e+38 |
double
|
The |
4.94065645841246544e-324d to 1.79769313486231570e+308d |
Other |
Type | Description | Range |
---|---|---|
boolean
|
the boolean in Java can have two values |
true or false |
char
|
2 bytes, unsigned, Unicode, |
0 to 65,535 |
int i = 0;
i = i + 1;
int i = 10;
i = i - 1;
int i = 10 * 2;
int i = 10 / 2;
int i = 25 % 2; // i equals 1
int i = 0;
i++; //i equals 1
int i = 10;
i--; //i equals 9
computeInterest()
in the class SavingAccount
, that calculates the
interest on one year, depending on the current amount at the computation timewithDraw()
on the same class, that takes one parameter