int
is a built-in type (some classes are predefined in Java).
struct
you would write in a C program.
class Fruit { // fields (data members) int grams; int cals_per_gram; //a method (i.e. member function) int total_calories() { return grams * cals_per_gram; } }
struct
in C).
malloc
a pointer to
the struct
in C.
Fruit
is a class.
Fruit apple;
apple
is called an instance of the
class Fruit
, or an object.
int i; i = 0; i++; // all fine
apple
above, does not give you an
object of class type Fruit
.
apple
that can reference
an object of Fruit
- after you have filled in the reference so
it points to such an object.
Fruit
object for apple
or
you can make it point to an existing object.
apple = new Fruit();
Fruit apple = new Fruit();
classname (parameterlist) { ... }
void
.
class Fruit { int grams; int cals_per_gram; // constructor Fruit () { grams = 55; cals_per_gram = 0; } // another constructor Fruit (int g, int c) { grams = g; cals_per_gram = c; } } ... Fruit melon = new Fruit(4,5), banana = new Fruit();
/opt/jdk
, but CUNIX has had softlinks set to go
through /opt/local
.
/opt/local/bin
.
CLASSPATH=/opt/local/lib/classes.zip:.
.class
files it needs.
.class
files
in them, and also zip files containing classes.
CLASSPATH=/usr/lib/java/classes.zip:/home/vicky/Java/classes:.
.java
files into .class
files.
% javac [options] filename.java
-classpath path
This overrides the CLASSPATH enviornment variable setting with
path
-directory d
Specifies the root directory of the class file hierarchy. In other words,
this is essentially a destination directory for compiled classes. If you leave
this unspecified, the .class
file will end up next to the
.java
file.
java
. To start a stand
alone application with it, you specify the initial class to be loaded (a
.class
file).
% java [interpreter options] classname [program arguments]
main
method.
.class
at the end of it.
% java HelloWorld
import java.lang.*; public class HelloWorld { public static void main (String args[]) { System.out.println("Hello, World!"); } }
import
- like #include
in C. This specifies
which packages and classes this program is using. This line means "include
all classes from the java.lang package." All java.lang classes are already
automatically included, so this line can be omitted.
class
- everything is a class in Java, even the simplest
program.
public
- this class is visible to all other classes in the
VM, and so is the main
method.
void
- Java has a void type just like C/C++.
main
- a class needs a main()
method if it is to
be a runnable program.
String
- Java has a built-in, first-class support for
strings.
args
- like argc, argv
constructs in C; except
we only need an array of String
s as the incoming command-line
arguments. No need for argc
because you can easily get any
array's length (in this case args.length
).
HelloWorld.java
.
% javac HelloWorld.java
HelloWorld.class
in your
directory.
% java HelloWorld
.class
at the end of
HelloWorld
.
public
class with
a public static void main (String args[])
method in it. It needs
that main
method as a starting point, like a C programs main
function.
main
method when you call
java
.
args
, but it should
be.
args[0]
is the first parameter, not the program
name (like in C).
java
interpreter.
Type Definition
boolean true or false char 16-bit Unicode character byte 8-bit signed integer short 16-bit signed integer int 32-bit signed integer long 64-bit signed integer float 32-bit IEEE 754 floating-point value double 64-bit IEEE 754 floating-point value
0
,
booleans are false
, chars are set to '\0
', etc.
public class Circle { int radius; // automatically initialized to 0 public setRadius (int x) { radius = x; } }
public int returnOneMoreThan (int x) { int y = 0; // must be initialized y = x + 1; return y; }
Just a couple of words about these books. "Java - How to Program" is a comprehensive book on how to program in Java. "Just Java" is only recommended for those of you who are already familiar with C or C++. It is at a level that allows you to pick up Java quickly, but it lacks some in depth details and assumes some prior Object Oriented programming experience.
This is a good book to use as a Java reference (you should always have a reference book at hand to remember syntax and library functions). It is too in depth to be used to learn Java from scratch.