Tuesday, July 30, 2013
Monday, June 3, 2013
Anatomy of a simple Java file
When we write java code, we will save our code files with the .java file extension. These .java files are written using the syntax, rules and conventions of the Java programming language and are human readable.
Java ‘code’ must be transformed into a form of code which is readable or executable by the machine. This is achieved by compiling the Java code in the .java file to transform it into bytecode which is stored in .class files. Bytecode is the language which is understood by machines that have the Java Virtual Machine (Java) installed.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
HelloWorld.java
The code above shows a simple example of a .java file. It contains the definition for a class called HelloWorld, a single method called main, and within that, a single statement which prints the words “Hello World!” to the console.
Java is case sensitive
Java is case sensitive! This means that main is different to Main.
All Java code is contained within a class
All Java code is contained within a class. The class begins with a class declaration. The .java file must have the same name as the class. The class declaration includes a set of curly braces, which contain all other code in the file.
public class HelloWorld {
}
You cannot omit either of the curly braces and you cannot place code outside of them either.
All Java apps contain exactly one main method
All Java applications contain exactly one main method. Think of the main method as the front door of the application. From the main method, program execution begins.
public static void main(String[] args) {
}
Additionally, the main method is able to accept a String array parameter. A String is any collection of alphanumeric characters (such as a file path) and an array is like a list of items.
(String[] args)
In this case we could say that the main method is able to accept a list of Strings. More on this later.
All methods are enclosed in a set of curly braces
All methods are enclosed in a set of curly braces (such as the main method above). There will be many sets of curly braces in your .java files. It is important that sets remain sets (don’t accidently delete a brace).
All method names are followed by parenthesis ()
All method names are followed by parenthesis - open and close round brackets. The parentheses contain parameters used by the method. Parameters are sometimes also called arguments. Not all methods use parameters but some methods may use one or more.
The example below shows an empty method (no code within the curly braces) called logout. The parenthesis following the world logout are empty. This method does not use any parameters.
public static void logout() {
}
The example below shows an empty method called login. The parenthesis following the world login contain two parameters; each is separated by a comma. This method uses the parameters to complete it’s task.
public static void login(String username, String password) {
}
All statements are terminated using the semi-colon ;
All statements are terminated using the semi-colon character. A statement is an instruction such as...
System.out.println("Hello World!");
Methods often contain many statements. The semi-colon ensures that all statements are separate.
// is used to comment the code
The double slash // is used to comment the code. Code commenting is useful as it provides internal documentation which allows yourself and others to read a description of the purpose of the code.
public class HelloWorld {
// This is the main method.
public static void main(String[] args) {
// Prints Hello World! to the console.
System.out.println("Hello World!");
} // Closes the main method.
} // Closes the class.
Comments are ignored by the compiler.
Tuesday, May 28, 2013
JPanel
"How to Use Panels (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)" ( http://bit.ly/ZaWraM )
"JPanel (Java Platform SE 6)" ( http://bit.ly/Rn8WMx )
Monday, May 27, 2013
Wednesday, May 15, 2013
Object Reflection
"Retrieving Class Objects (The Java™ Tutorials > The Reflection API > Classes)" ( http://bit.ly/10WYmfe )
"Trail: The Reflection API (The Java™ Tutorials)" ( http://bit.ly/16dIKgn )
Wednesday, May 1, 2013
Wednesday, April 24, 2013
Abstraction. A definition.
From http://programmers.stackexchange.com/questions/16070/what-is-abstraction
"Abstraction is the process of moving from a specific idea to a more general one. For example, take a look at your mouse. Is it wireless? What kind of sensor does it have? How many buttons? Is it ergonomic? How big is it? The answers to all of these questions can precisely describe your mouse, but regardless of what the answers are, it's still a mouse, because it's a pointing device with buttons. That's all it takes to be a mouse. "Silver Logitech MX518" is a concrete, specific item, and "mouse" is an abstraction of that. An important thing to think about is that there's no such concrete object as a "mouse", it's just an idea. The mouse on your desk is always something more specific - it's an Apple Magic Mouse or a Dell Optical Mouse or a Microsoft IntelliMouse - "mouse" is just an abstract concept."
Here's a cat...
"Abstraction is the process of moving from a specific idea to a more general one. For example, take a look at your mouse. Is it wireless? What kind of sensor does it have? How many buttons? Is it ergonomic? How big is it? The answers to all of these questions can precisely describe your mouse, but regardless of what the answers are, it's still a mouse, because it's a pointing device with buttons. That's all it takes to be a mouse. "Silver Logitech MX518" is a concrete, specific item, and "mouse" is an abstraction of that. An important thing to think about is that there's no such concrete object as a "mouse", it's just an idea. The mouse on your desk is always something more specific - it's an Apple Magic Mouse or a Dell Optical Mouse or a Microsoft IntelliMouse - "mouse" is just an abstract concept."
Here's a cat...
Subscribe to:
Posts (Atom)