SE251:StudentClarifications:Lec2

From Marks Wiki
Jump to navigation Jump to search

Lecture 2 - Java Basics

Lecture slides

Classes and Objects

A class is just a type of thing. The class Person would describe the various attributes and abilities (variables and methods respectively) that make up a person. For example, it might describe the fact that people have names and ages, and the ability to wave. An object, on the other hand, would be a person called Bob who's 23 years old and happens to be waving to us. Hi Bob! People sometimes give a slightly misleading explanation of objects, by saying the class is Car, and the object is Porsche 911 or something to that effect. Porsche 911 is still a class, because it's a type of car, not a particular car. The object is the black Porsche 911 outside the engineering building. To put this into familiar (for most of you) programming terms, a class is very similar to a structure (struct) in C.

/* This is like a class */
typedef struct { 
   int age;
   char* name;
} Person;

/* These are objects */
Person bob;
Person fred;
bob.age = 23;
bob.name = "Bob";

Java classes are, however, much more advanced than C structures. For starters, they can include functions (functions within classes are called methods). Then there is inheritance and so on, which we haven't covered yet.

Here is a phrase to remember, if this sort of thing works for you: Galloper was the best horse is his class.

Hello World

import javax.swing.JApplet;
import java.awt.Graphics;

/*
 * An applet that displays the message “Hello World!”.
 */
public class HelloWorldApplet extends JApplet {
    public void paint( Graphics g ) {
      // Send a message to the Graphics object.
      g.drawString( “Hello World!”, 25, 25 );
    }
}

I'd just like to explain this piece by piece. The imports up the top just say what other code is needed to run this java program. In this case the JApplet class and the Graphics class are needed. javax, swing and awt are packages (groups of classes). Public just means that the class can be accessed from anywhere in the program, it's not important yet. extends JApplet means that HelloWorldApplet is a type of JApplet, so it automatically gets all the functionality (methods and members) that JApplet has. This is inheritance. HelloWorldApplet inherits all the functionality from it's "parent", JApplet. The paint function is common to all applets, and is called by the browser when the applet needs to be drawn.

Data Types

Most of the basic data types are similar to C. One notable difference is that char and byte are different in Java, where in C if you wanted a byte you would use char. In Java, char is 16 bits instead of 8. This is for internationalisation. 256 values is enough for english, but if you want your program to work in more languages you clearly need a larger data type for characters.

Arrays in Java are very similar to C, with the addition of the .length attribute.

Pass by value/reference

In C terms, you pass by value when you don't use a pointer. It's the normal way of calling a function that has parameters, where changing the parameter inside the function will not change it's value outside. I consider it misleading to say that java always passes by value. Where simple data types are concerned (int, float) it passes by value. Arrays, as has been mentioned, are effectively a pointer, so changing one inside a function will change it outside the function too. But classes are the most confusing, as I understand it. You can have a function which takes a Person and an integer in exactly the same way:

public void nothing(Person p, int a) {
}

If, inside the function, you say a = 5, then the variable you passed as input a will not become 5, it will stay whatever it was. If instead you say p.age = 5, then the persons age will change not only in the function, but outside too! Why? Because Person p is effectively a pointer. You don't have to say it's a pointer, the fact that Person is not built in like int and double is enough. Java "pointers" are interesting in that they don't have the confusing * and & syntax, and you never have to delete the memory they point to (it's done automatically if nothing points to it). You do still have to allocate the memory though, which is done like: p = new Person();

Strings

Firstly, thanks to whoever it was that corrected the lecturer on string assignment. Here it is again:

Strings are immutable, which means that once they have been created, they cannot be changed. However, since string is not as built in as int, what was mentioned earlier about pointers applies. So if you create a string car containing "Dodge", then assign "Toyota" to it like: car = "Toyota", car will then point to the new string "Toyota" and the old one will be deleted because nothing points to it any more.

If you want a string which is modifiable (usually due to performance problems with creating and destroying large strings all the time) use a StringBuffer.