Terms & Concepts 1

advertisement
BIT 115: Introduction to Programming
TERMS & CONCEPTS 1
Class - A class is a type of blueprint or pattern that contains fields and methods. Objects are created
from classes.
Objects - Objects represent the instantiation of a class.
Method – We communicate with objects by invoking methods on them. Objects usually do something if
we invoke a method, i.e. perform some kind of action.
Parameter – Methods can have parameters to provide additional information for a task.
Type – Parameters have types. The type defines what kinds of values a parameter can take. Examples of
types are int for numbers, boolean for true/false values, string for text characters.
Return Type – The return type of a method specifies what a method call will return.
Void – A method with a void return type does not return a value.
Commands – Methods with void return types represent commands.
Questions – Methods with non-void return types represent questions.
Signature – The specification of a method, which shows its return type, name, and parameters, is called
its signature.
Source Code – The source code of a class determines the structure and behavior (the fields and
methods) of each of the objects of that class.
Compilation – Computers do not understand source code. It needs to be translated into machine code
before it can be executed. This translation of source code into workable machine code is called
compilation or compiling.
Java Keywords
Java keywords are words reserved as core building blocks of the Java language. You may not
use them for names of things you create, like class, field and method identifiers. The Java
compiler recognizes these words and treats them special. The table below contains all of Java's
keywords.
abstract const
finally
int
public
this
boolean continue float
interface
return
throw
break
default
for
long
short
throws
byte
do
goto
native
static
transient
case
double
if
new
strictfp
try
catch
else
implements package
super
void
char
extends
import
private
switch
volatile
class
final
instanceof
protected synchronized while
The Special Method: public static void main(String[ ] args)
When we execute a Java application, we use the "java" command which runs the Java Virtual
Machine (JVM) and we specify a class file for it to execute. But, how does the JVM know where
to start executing?
ANSWER: It starts with a special method you identify as the "main" method of your
application. This convention is a tradition Java inherited from the "C" programming language.
The source code for main( ) always looks something like:
public static void main (String[ ] identifier)
{
<statement>
...
}
The method has to be public so that it can be invoked from the outside.
The method has to be static, because no objects exist when we start off. Initially, we only have
classes, so static methods are all we can call. This static method typically creates the first
object.
The method has a return type of void because it does not return a value.
The parameter is a String array, to allow users to pass in additional argument.
Summary
Java programs are composed of classes. Classes are composed of <member>s. Members are
either fields and methods. Every program needs to have a special method named main. If you
merge the source code I've given you above, you end up with a template for a minimal Java
application.
class <identifier>
{
public static void main(String[ ] identifier)
{
<statement>
...
}
}
with the first identifier is the class' name and the second identifier is args.
NOTE: Remember—it is a defacto standard that all class names start with a capital letter, with
additional words in it also capitalized. As an example, MyClass is proper but myClass and
Myclass are not.
NOTE: Remember—your class identifier must also be the name of the file you enter your source
code into. The file also must have the extension of ".java"
Download