Wiki Markup |
---|
{composition-setup |
Below is a short tutorial designed for someone who is familiar with programming (potentially only at the level of MATLAB), and has never learned or long forgotten Java. After explaining the very basics of classes and objects, we skip all of the syntax and control flow and go straight to what you will need to complete the workshop. If your Java is not strong, please take the time to understand this tutorial, otherwise you will most likely be lost during the workshop. It should take about 30 minutes.
Java Basics: Classes, Objects, Fields, Methods, and the Keyword Static
In Java, all code is associated with a class. Classes have fields and methods. Instances of classes are created with Constructors and called Objects. Once an Object has been constructed, its methods can be called and its fields can be accessed. Here is a concrete example with two simple classes:
...
Now you are ready for some more advanced Java!
Types, Primitives, and Generics
TODO... but until then
...
Section | ||
---|---|---|
| ||
Column | Column | |
| ||
Code Block | ||
| 10px | |
Column | ||
| ||
Code Block | title | |
Column |
If you understand this code, particularly how the keywords this
and static
work, and you can predict the output as shown below,
Toggle Cloak | ||
---|---|---|
|
...
visible | false |
---|---|
id | Cat Code Output |
then you probably don't need to expand the section below and can continue.
Toggle Cloak | ||
---|---|---|
|
...
visible | false |
---|---|
id | Cat Code Explained |
First, lets talk about the contents of the files Cat.java
.
- Fields: The class
Cat
has three fields,name
,age
andfriend
. Each field has a type, which ensures that we can only store aString
inname
, anint
inage
and anotherCat
infriend
. The wordprivate
before each field indicates that the field can only be accessed from within the classCat
. - Constructors: The class
Cat
has one constructorpublic Cat(String name, int age){...
}. In the constructor, the fieldsname
andage
are initialized with the values given in the arguments, whilefriend
is simply set tonull
. Because the constructor is public, it can be invoked from other classes. An example of the invoking the constructor can be seen inCatsTheMusical.java
. - Methods: The class
Cat
has four methods,getName(){...
},getFriend(){...
},setFriend(Cat friend){...
} andsayHello(String aboutMe){...
}. The keywordpublic
indicates that the methods can be called outside of the classCat
. The second word in each method is the return type, or the type of the result of the method. Notice thatsetFriend(...)
has a return type ofvoid
, indicating that nothing is returned. The first two methods are examples of getter methods, as they simply retrieve the value of a private field. The third method is a setter method, which allows the fieldfriend
to be changed from outside the classCat
.
Now lets talk about the other file CatsTheMusical.java
.
Now that we have the basic terminology down, lets examine some of the finer points which may be confusing to non-Java programmers:
{column} {column}{column} {section} If you understand this code, particularly how the keywords {{this}} and {{static}} work, and you can predict the output as shown below, {toggle-cloak:id=Cat Code Output} _Cat Code Output_ {cloak:id=Cat Code Output|visible=false} {noformat} Hello World! My name is Mr. Mistoffelees and I am 8 years old. I am magical. Hello World! My name is Garfield and I am 12 years old. I am tired/hungry. Mr. Mistoffelees and Garfield are friends? false Mr. Mistoffelees and Garfield are friends? true {noformat} {cloak} then you probably don't need to expand the section below and can continue. {toggle-cloak:id=Cat Code Explained} _Cat Code Explained_ {cloak:id=Cat Code Explained|visible=false} First, lets talk about the contents of the files {{Cat.java}}. * _Fields_: The class {{Cat}} has three fields, {{name}}, {{age}} and {{friend}}. Each field has a _type_, which ensures that we can only store a {{String}} in {{name}}, an {{int}} in {{age}} and another {{Cat}} in {{friend}}. The word {{private}} before each field indicates that the field can only be accessed from within the class {{Cat}}. * _Constructors_: The class {{Cat}} has one constructor {{public Cat(String name, int age){...\}}}. In the constructor, the fields {{name}} and {{age}} are initialized with the values given in the arguments, while {{friend}} is simply set to {{null}}. Because the constructor is public, it can be invoked from other classes. An example of the invoking the constructor can be seen in {{CatsTheMusical.java}}. * _Methods_: The class {{Cat}} has four methods, {{getName(){...\}}}, {{getFriend(){...\}}}, {{setFriend(Cat friend){...\}}} and {{sayHello(String aboutMe){...\}}}. The keyword {{public}} indicates that the methods can be called outside of the class {{Cat}}. The second word in each method is the _return type_, or the _type_ of the result of the method. Notice that {{setFriend(...)}} has a return type of {{void}}, indicating that nothing is returned. The first two methods are examples of _getter_ methods, as they simply retrieve the value of a private field. The third method is a _setter_ method, which allows the field {{friend}} to be changed from outside the class {{Cat}}. Now lets talk about the other file {{CatsTheMusical.java}}. * _Static Methods_: Methods that are {{static}}, while associated with the class they are written in, are not associated with any particular instance. They can be invoked by using {{\[class-name\].\[method\]}}. For example, see the invocation of {{areFriends(...)}} within {{main(...)}}. * _main(...)_: All Java program must start with the code {{public static void main(String[] args){...\}}}. From here, we will follow the methods and constructors into other class files. The arguments, {{String\[\] args}} can be accessed by calling {{args\[0\]}} etc. and can be set by going to the top menu bar and then selecting _Run → Run Configurations..._, then selecting the _Arguments_ tab in the center and filling out the _Program Arguments_ box. Now that we have the basic terminology down, lets examine some of the finer points which may be confusing to non-Java programmers: * Given an instance of a class (e.g. for {{Cat c = new Cat(...)}}, {{c}} is an instance), non-static methods from the class can be called by \[instance name\].\[method name\] (e.g. {{c.getName()}}). Similarly, non-static fields can be accessed by calling \[instance name\].\[field name\], (e.g. {{c.name}}). However, methods or fields labeled {{private}} can only be called/accessed from code written within their own class file. * Within a constructor, we can directly access the fields of a class by name without reference to any particular instance, as it is implied that the instance is the one that is being built. For example, in the {{Cat}} constructor, we make the assignment {{friend = null}}. However, when there are argument variables with the same name as the field variables, they take precedence and _shadow_ the fields variables. To reference the fields once they have been shadowed, you need to use the keyword {{this}}. In fact, you can use the keyword {{this}} whenever you want to refer to a field or method of the instance of the class being built by the constructor, regardless of whether there is shadowing. The same rules when you are inside a non-static method instead of inside a constructor. * In static methods, we cannot refer to any non-static fields or non static methods of the class without also providing an instance, as static methods are not associated with any instance. Similarly, the keyword {{this}} cannot be applied. {cloak} Now you are ready for some more advanced Java! h1. Types, Primitives, and Generics TODO... but until then * Try [here|http://docs.oracle.com/javase/tutorial/java/generics/] for generics, up to Generics, Inheritance, and Subtypes. * Autoboxing [http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html |
...
] h1. Inheritance, Abstract Classes, Interfaces, and Inner Classes |
...
TODO... but for now |
...
* Things in here are all pretty important [http://docs.oracle.com/javase/tutorial/java/IandI/index.html |
...
Data Structures
] h1. Data Structures TODO... but for now |
...
* The Java Collections documentation [http://docs.oracle.com/javase/tutorial/collections/index.html |
...
] ** Interfaces: Sets, Lists, and Maps |
...
** Implementations: HashSet, ArrayList, HashMap |
...
* [Guava|http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6]
** [BiMap|http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#BiMap]
** [Immutable Collections|http://code.google.com/p/guava-libraries/wiki/ImmutableCollectionsExplained] |