Start Now !
Start by writing in the top text box
void main() {
}
This is the core inside which we start to program. Every program must go inside a
void main() function which is followed by curly brackets { }
Statements are :
Functions are calls to action , they do something.
eg printLine("this"); // prints out "this"
main() // makes the whole program execute
readInt() // reads in an integer typed in by a user
Variables
Exercises
1. which of these variable names are legal ?
a. result b. Product1 c. a d. textbox e. text1 f. field_2 g. stackDepth h. ball Speed
Declaration
All variables must be declared. That is, they must tell the compiler what type of data they are
(datatype). Datatypes can be a number such as int or double. Or text such as a String.
Or a true/false datatype called a boolean.
These are declarations:
int myAge; double price; String myName; boolean likesJava;
Initialization
Initialization must be done after the declaration. Its purpose is to give a value to the variable.
If given no value, Java assumes that the value is 0, or false for a boolean.
Here are examples of initialization:
myAge = 14; price = 12.95; myName = "Fred Durst"; likesJava = false;
Variables can be declared and initialized in the one line:
int myAge = 14; double price = 12.95; String myName = "Fred Durst"; boolean likesJava = false;
Declaration and initialization should be done at the top of the program before the variables are used.
Number Variables
int - integer or whole numbers such as 3, 9, 32, 456
int age = 9; int year = 2003;
double -
numbers with a decimal point in them such as 1.00 , 34.54, 234.07
double price = 12.45; double pi = 3.412;
Text Variables
String
- a sequence of characters or letters such as "a string of text", "Steve Happ", "GameBoy 3.34".
- Strings are always enclosed inside Quotation marks - " ".
String myName = "Steve Happ"; String gameType = "Shoot 'em up";
Truth Variables
boolean
- is either "true" or "false"
boolean isLearningJava = true; boolean likesIceCreams = false;(Just joking ! )
Color Variables
Color
Color shirtColor = red; Color myCatColor = yellow;
void main() {
// declaration and initialization of variables
int sum = 4 + 3;
double price = 12.34;
String book = "Learning Java";
String name = "Steve Happ";
// do the programming work
printLine("Attention Computer Book Shop");
printLine("I want to buy " + sum + book +
" at the price of $" + price);
printLine("Yours Sincerely");
printLine(name);
}
Notes on program above
// this is a line of comment // and will not be processed // it is used for reminders to oneself // and for structural purposes
printLine("Whatever is between the quotes will be printed") ;
printLine("My name is " + name);
printLine("My name is " + name + ".") ;
printLine("My name is " + name + ", and my age is " + age) ;
These words are reserved--you cannot use any of these words as names of variables in your programs.
| abstract | double | int | strictfp |
| boolean | else | interface | super |
| break | extends | long | switch |
| byte | final | native | synchronized |
| case | finally | new | this |
| catch | float | package | throw |
| char | for | private | throws |
| class | goto | protected | transient |
| const | if | public | try |
| continue | implements | return | void |
| default | import | short | volatile |
| do | instanceof | static | while |