Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4
| Lesson 5 | Lesson 6 | Lesson 7 | Lesson 8 | Lesson 9 | Lesson 10
We moved to: http://www.freepgs.com/java
Alright, now this lesson will cover variables. Variables are names that are used to represent numbers and letters.
Variables look like this:
int, String, boolean, char...
Now those are all variable types. Since variables store values, we need to give it some. However before you can
give your variable a value you need to name it.
For example:
int a = 27 ;
int is the variable type, a is the variable name, and 27 is the variable's value. The equal sign tell the computor that you are
assigning the value 27 to the name a. When you assign values you are declaring it.
Type this in:
class hello {
public static void main(String[] args) {
//variable is here
int a = 27;
System.out.println(" Hi, this is a variable value " + a + " . ");
}
}
|
If it compiled right it should of said Hi, this is a variable value 27 .
Ok, remember variable names could only be name with a letter at begining:
int hi = 27 ;
(When you name your variable it must start with a letter or underscore and
must only contain letters numbers and underscores.
You can't have spaces in the variables, and you can not use reserved words like new or int)
Here are some examples of variable names:
peanut (correct)
ham_bone (correct)
x (correct)
y (correct)
emailAdress (correct)
java2 (correct)
MONKEYS (correct)
2car(wrong)
xya!(wrong)
are$(wrong)
my dog(wrong)
The folowing list are all the variable and it's uses
int (use for holding number values int a = 27;)
String (use for holding words, need quotation beetween words String name = "Khoa";)
boolean (true or false only boolean lieksMoney = true;)
char (holds character like letters char d = "d";
long (big numbers 666779685 long c = 6765646736;)
double (for numbers with deciemel points double money = 10.56)
float (really small numbers like 0.00000000002
short (small numbers max value=32768)
byte (-128= max value)
Type this in:
class hello {
public static void main(String[] args) {
//variables are here
int a = 27;
String name = "Khoa";
System.out.println(" Hi, this is a variable value " + a + " . ");
System.out.println(" This is a String value " + name + " . ");
}
}
|
Summary:
Remember variables are meant to hold values, so they can be call easily later. We will explains more on variables later.
|
Copyright ©2004 Java Wave, All rights reserve.
|