unmigratedcomposition-wiki-markupsetup |
---|
|
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:
Section |
---|
|
minutes.
h1. What are classes and objects?
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:
{section:border=false}
{column}{column}
{column:width=300px}
{code:title=Cat.java}
public class Cat {
private int age;
private String name;
private Cat friend;
public Cat(String name, int age) {
this.name = name;
this.age = age;
friend = null;
}
public String getName() {
return name;
}
public Cat getFriend() {
return friend;
}
public void setFriend(Cat friend) {
this.friend = friend;
}
public String sayHello(String aboutMe) {
return "Hello World! My name is " +
name + " and I am " + age +
" years old." + " I am " + aboutMe + ".";
}
}
|
| {code}
{column:width=10px}{column}
{column:width=500px}
{code:title=CatsTheMusical.java} Column |
---|
| Code Block |
---|
|
public class CatsTheMusical {
public static void main(String[] args) {
Cat mist = new Cat("Mr. Mistoffelees", 8);
System.out.println(mist.sayHello("magical"));
Cat garf = new Cat("Garfield", 12);
System.out.println(garf.sayHello("tired/hungry"));
mist.setFriend(garf);
System.out.println(mist.getName() + " and " + garf.getName()
+ " are friends? " + CatsTheMusical.areFriends(mist, garf));
garf.setFriend(mist);
System.out.println(mist.getName() + " and " + garf.getName()
+ " are friends? " + CatsTheMusical.areFriends(mist, garf));
}
public static boolean areFriends(Cat cat1, Cat cat2) {
if (cat1.getFriend() != null && cat2.getFriend() != null) {
return cat1.getFriend() == cat2 && cat2.getFriend() == cat1;
}
return false;
}
}
|
|
|
If you understand this code, particularly how the keywords this
and static
work, and you can predict the output as shown below,
Cat Code Output Cloak |
---|
visible | false |
---|
id | Cat Code Output |
---|
|
|
then you probably don't need to expand the section below and can continue.
Cat Code Explained Cloak |
---|
visible | false |
---|
id | Cat Code Explained |
---|
|
First, lets talk about the contents of the files Cat.java . {code}
{column}
{column}{column}
{section}
Lets make sure we understand how this works. 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, {{- 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, \}} {{\}} {{...\}} {{...\}} {{}} {{}} _ _ __ {{}} {{}} __ __ {{}} {{}}
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{{. - 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] }} {{}} {}
* __ {{ \}} {{ }} {{}} _ _ __ _ _
Now that we have the basic terminology down, lets examine some of the finer points which may be confusing to non-Java programmers:
* {{ }} {{}} \ \\ \ {{}} \ \\ \ {{}} {{}}
* {{}} {{ }} __ {{}} {{}} Analogous rules apply for fields and methods inside other non-static methods.
* 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.
- 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.
|
Now you are ready for some more advanced Java!
Types, Primitives, and Generics
TODO... but until then
- Try here for generics, up to Generics, Inheritance, and Subtypes.
- Autoboxing http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
Inheritance, Abstract Classes, Interfaces, and Inner Classes
TODO... but for now
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