Basic Java Language: Difference between revisions
wikademia>Crossbow9 |
wikademia>Crossbow9 No edit summary |
||
| Line 54: | Line 54: | ||
You can set the values of variables while declaring them, like this: | You can set the values of variables while declaring them, like this: | ||
<pre>int age = 100;</pre> | <pre>int age = 100;</pre> | ||
You can declare multiple variables on one line, if you wish: | |||
<pre> boolean a, b;</pre> | |||
===Identifiers=== | ===Identifiers=== | ||
There are several rules for the naming of your variables. Identifiers can be named using all the letters from A through Z (capital or lowercase, you choose), numbers 0-9 and underscore _ and the dollar sign $. The first character of the identifier cannot be a number, but it can be a letter, _ or $. | There are several rules for the naming of your variables. Identifiers can be named using all the letters from A through Z (capital or lowercase, you choose), numbers 0-9 and underscore _ and the dollar sign $. The first character of the identifier cannot be a number, but it can be a letter, _ or $. | ||
You can't create two variables with the same identifier, all identifiers must be unique. You also can't use an identifier that is a Java keyword. Here is a list of all the [http://en.wikipedia.org/wiki/Java_keywords Java Keywords] | You can't create two variables with the same identifier, all identifiers must be unique. You also can't use an identifier that is a Java keyword. Here is a list of all the [http://en.wikipedia.org/wiki/Java_keywords Java Keywords] | ||
==Statements== | |||
A statement in Java is generally a line of code that does something. Statements are ended by a semicolon ;. An example of a statement is the | A statement in Java is generally a line of code that does something. Statements are ended by a semicolon ;. An example of a statement is the declaration of a variable like we saw before. Another example you will recall from the "Hello World" program we made in Lesson 1. | ||
<pre>System.out.println("Hello World");</pre> | <pre>System.out.println("Hello World");</pre> | ||
This line of code prints ''Hello World'' onto the console. | This line of code prints ''Hello World'' onto the console. | ||
===Operators=== | |||
==Equality Operator = == | |||
The equality operator sets the variable on its left equal to the value on its right. This code creates the variable ''a'' and sets it equal to 5 | |||
<pre>int a; | |||
a = 5;</pre> | |||
Fairly simple, right? Here is where it can get a little more tricky. | |||
<pre>int a; | |||
int b; | |||
b = 6; | |||
a = b;</pre> | |||
At first glance, you might think that the last statement is setting ''a'' equal to the letter "b", but instead it is setting ''a'' equal to the ''value'' of ''b'', which is 6. | |||
[[Topic:Java|Back to Java Main Page]] | [[Topic:Java|Back to Java Main Page]] | ||