 |
Classes
All your coding is held within a class, or multiple classes. A class is like a container for your code. Every program has at least one class. In a particular program, it holds all the code that's needed to fulfil the purpose of the program. A class is defined with an access modifier (public, protected, or private), the word "class" and the name of the class. After this line of definition, brace brackets - "{...}" - enclose your code.
File: SpellingBee.java
public class SpellingBee
{
/* The name of your class should have significance to the purpose of
the program. It'll make your program a lot easier to find, considering that your class name has to be the same as your file name! */
// tasks that spells out words like "honey", or "honeycomb"
}
Note: public is defined as part of the class definition and it means that any other program has access to this class (as opposed to private, in which restrictions apply). See the Methods Tutorial for more information.
A class is very much like a bee, because it can work very well on its own, completing a task, but working with other bees (classes) can often make it much more productive! Also, like a bee, classes do not just do a task, they are entities in themselves as well. This is called object-oriented programming (OOP), and will be discussed in a later tutorial.
Methods
A method holds code that does a specific task. Multiple methods come together to complete a program's purpose. For simple programs, you don't really need to use more than one method: the main() method. If you want to learn more about using multiple methods, read the Methods Tutorial.
main() Method
The main() method is the most important method for a basic program. When you're running a program, only the code in the main method gets executed; that is, the code in the main method starts to run when the program starts.
Outside classes are called from within the main() method to use them.
Example
public class SpellingBee
{
public static void main (String [] args)
{
// code that spells out bee-related words
}
}
Note: Like the class, the main method is also defined as public. It also has the static modifier which means that it is a class method (as opposed to instance method, which will be discussed in a later tutorial).
Statements and Blocks
A statement is an expression (similar to a sentence) followed by a semi-colon.
If a statement is a sentence, a block is a paragraph. Blocks are used to group one or more statements and are enclosed by "{" and "}". They are statements that come together to complete a particular task.
Example
/* this is the beginning of a block */
{
int m=4; //statement 1
int n=6; //statement 2
System.out.println(m+n); //statement 3
} /* this is the end of the block */
Note: This looks very similar to what happens under a class definition or method declaration. Thats because classes and methods need groups of statements on which to function! If the brackets weren't there, the method or class definition would only apply to the first line of the block, the line directly underneath it. Consequently, if you need a block consisting of one line only, the brackets may be left out.
Compiling and Interpreting
When a program is executed, there are two different ways for this to happen. The first is called compiling, when your entire program (called source code) is converted into machine code (binary, made up of 0s and 1s) in a separate file for the computer to read. That new file is then run on the computer.
The downside to compiling to machine code is that, only the platform (Microsoft, Macintosh) on which the program was compiled can actually run the program.
The second method is called interpreting, which does not compile source code into a file. The computer translates one line of your source code at a time into machine language, then runs it immediately. Then the computer translates the next line of code into machine language and runs the next line. Although this provides flexibility, it can also be a lot slower when running your program.
The Java language uses a combination of the two methods, and effectively solves most problems with either one alone. Java solves the system flexibility problem by compiling using bytecode. First, Java compiles the source code into a bytecode file with the extension .class. Bytecode is a simpler language, closer to machine code, but not quite. When bytecode is interpreted, it runs a lot faster than an uncompiled program. The JVM (Java Virtual Machine, refer to Introduction Tutorial), interprets, translates and executes the bytecode.
Packages
Eventually, you will need to use multiple classes or objects for your program. Often, these need to be saved in different file. In Eclipse, a package is one big file that contains all your other files. For example,
in package Unit1; there may be many different files with different topics such as SpellingBee, HoneyComb, and QueenBee. However, all these files generally communicate with one another, and are united with a common purpose (something hive-related).
|
|
 |
|