Jump to content

Basic Java Language: Difference between revisions

From IdeaWazaWiki
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===
==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 declarationg of a variable like we saw before. Another example you will recall from the "Hello World" program we made in Lesson 1.  
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]]

Revision as of 04:49, 25 September 2006

This lesson discusses the basics of the Java language, including variables, primitives, operators, statements and Java writing conventions.

Variables

Variables are used to store data that can change. There are two main categories of variables, reference variables and primitive variables. Reference variables are place holders for objects (More on objects in Lesson 4, Java Objects and Classes.) Primitive variables are given a value of a primitive, a basic value. Variables are all given a type when they are declared, such as "int" or "string".

Primitive Data Types

Primitives are the simplest type of data. Primitives consist of numbers, letters and strings of letters. The following table shows all the types of primitives.

Primitive Description Values
byte A brief, 8-bit integer Whole numbers -128 through 127
short A short, 16-bit integer Whole numbers -32,768 through 32,767
int A 32 bit integer Whole numbers -2,147,483,648 through 2,147,483,647
long A long, 64 bit integer Whole numbers -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807
float Single-percision floating poing (32-bit IEEE 754) Smallest positive non-zero: 14e-45, Largest positive non-zero: 3.4028234e38
double Double-percision floating point (64-bit IEEE 754) Smallest positive non-zero: 4.9e-324, Largest positive non-zero: 1.797693157e308
char A single character All Unicode characters
boolean A Boolean value true or false

Declaring Variables

In a nutshell, declaring a variable has this syntax:

 [variable type] [identifier];

This works for both reference variables and primitive variables. [variable type] is the type of variable, like int or char. [identifier] is the name for the variable you wish to create, for example, password or number_of_fish. If you wanted to create a int variable called age, you would type this:

int age;

Notice the semicolon at the end of the statement. All statements in Java must end in this, otherwise you will get a compiler error.

You can set the values of variables while declaring them, like this:

int age = 100;

You can declare multiple variables on one line, if you wish:

 boolean a, b;

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 $.

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 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 declaration of a variable like we saw before. Another example you will recall from the "Hello World" program we made in Lesson 1.

System.out.println("Hello World");

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

int a;
a = 5;

Fairly simple, right? Here is where it can get a little more tricky.

int a;
int b;
b = 6;
a = b;

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.

Back to Java Main Page