JavaHyperText
This webpage contains definitions of Java concepts and constructs. It is not complete, but you can find most of what you want here. It also cannot give complete, rigorous definitions of everything ---for that, you have to read the Java language specification. But it covers the most usual cases and gives you a good conceptual view of objects and classes. Use it as your main resource for information on Java.
We call it a HyperText because: (1) it is a text, an online text, and (2) it is indeed hypertext, that is, it is an electronic document with each entry containing appropriate links to other entries. Hopefuly, this hypertext format will let you find what you want with just 1 or 2 clicks. See this pdf file for a bit of history of the word hypertext.
Many of the explanations are given in pdf files. They are never more than two pages long, and most are one page. This should make it easy to read about and digest any topic.
Type a word into the Search field and only entries that have that word in it will be displayed!
The first video on this page is a 3.5-minute tutorial that explains abstract classes and methods.
The first video on this page is a 3.5-minute tutorial that explains abstract classes and methods.
modifier | class | package | subclass | world |
---|---|---|---|---|
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
no modifier | Y | Y | N | N |
private | Y | N | N | N |
Annotations in a JUnit 4 testing class: See this pdf file.
The specification of the API for Java version 1.8 appears here: docs.oracle.com/javase/8/docs/api/. Visit this site often to learn about some class (like String, or JFrame) and its methods.
public static void main(String[] pars) {...}
An application can be executed without using an IDE. One creates a jar-file that contains all the classes of the program and uses it as a stand-alone program.
min(x+y, y+w)
The number of arguments must equal the number of parameters of the method that is being called, and the type of each argument must be the same as or narrower than the type of the corresponding parameter.
This pdf-file explains how to create ragged/jagged multidimensional arrays, in which each row can have a different number of elements.
To execute it, evaluate the <boolean-expression>; if true, do nothing more; if false, throw an AssertionError, which will generally cause the program to abort.
The assignment statement has the form <variable> = <expression> ;
To be syntactically correct, the
type of the expression must be the same as or narrower than the type of the variable. To execute the assignment statement, evaluate the <expression> and store its value in the <variable>. For more information see the 2.5-minute video on this webpage.
See this pdf file for the compound assignment operators += -= *= /= %=.
This pdf file explains casting between types char and int.
This pdf file explains casting among the number types byte, short, int, long, float, and double.
This pdf file explains casting with classes, called class casts.
This webpage contains a 3.75-minute video on casting with interfaces.
If only one of b and c are of type String, then b + c still denotes catenation, and the non-String operand is changed into its String representation before the catenation is done. If the non-String operand is of some class-type, its toString method is called to obtain its String representation.
The simplest way to get a String representation of any expression is to catenate it with the empty String. For example, "" + (68 + 2) evaluates to a String that contains "70".
The class definition also defines the static variables (class variables) and static methods (class methods).
A class can be used as a type. If C is (the name of) a class, then C v; declares a variable v that contains either null or the name of (or pointer to) an object of class C.
A constructor should truthify the class invariant. Each method body can assume that the class invariant is true and should terminate with the class invariant true.
Also called reference type because a value ot the type is a pointer or reference to an object.
5 < 4 ? 1+6 : 3 evaluates to 3 since the boolean-expression is false
5 > 4 ? 1+6 : 3 evaluates to 7 since the boolean-expression is true
The purpose of a constructor is to initialize the fields of a new object of class <class-name> so that the class invariant is true when the object is created. See this pdf file for:
(1) Rule: initialize superclass fields before subclass fields.
(2) Syntactic rule: a constructor that you write must start with a call on another constructor; what call is inserted if it is not present.
(3) How to call another constructor using super or this.
(4) What constructor is inserted if a class does not have a constructor.
1. In a new-expression. It has the form <class-name> ( <arguments> )
2. As a call of another constructor in the same class: this(<arguments>);.
3. As a call of a constructor of the superclass: super(<arguments>);.
public <class-name>() {}
This two-page pdf file shows you how to add fields and methods to an enum class.
\n —new-line character
\\ —backslash character
\" —double quote character
\' —single quote character (you can use ' in a String, but as a character, write '\''
For information on increment/decrement expressions ++ and --, see this pdf file.
Look here for constant expressions.
public static final double PI= 3.141592653589793;
A class that is declared with keyword final cannot be extended ---cannot have a subclass.
You can enable the use of the foreach statement on your own collection class by implementing interfaces Iterator and Iterable. This tutorial shows you how in less than 15 minutes of video: webpage.
(1) the name of the method,
(2) a program counter, which indicates which statement to execute next,
(3) a scope box, which contains the name of the class or object in which the method resides,
(4) the parameters of the method, and
(5) the local variables of the method.
1. for a static function: <class-name> . <function-name> ( <arguments> )
2. for a non-static function: <expression> . <function-name> ( <arguments> )
where the <expression> evaluates to the name of an object that contains the function to be called. Both "<class-name>." and "<expression>." can be omitted if the call appears in a place in which the function can be called without them.
Introduction to generic classes: pdf file.
Introduction to generic methods: pdf file.
Why ArrayList<Integer> is not a subclass of ArrayList<Object>: pdf file.
Introduction to wildcards: pdf file.
Introduction to bounded wildcards: pdf file.
Examples of upper-bounded types: pdf file.
Examples of lower-bounded types: pdf file.
Multiple upper bounds: pdf file.
Restrictions on the use of generic types: pdf file.
Discussion of raw types.
See this pdf file for a full explanation.
See this pdf file for an example where instanceof and not getClass() should be used.
Static components are not overridden but are hidden. This is explained in this pdf file.
import <path>.<class-name>; // import a particular class
import <path>.*; // import all classes in the package
where the <path> is a path of the API package (e.g. javax.swing) or a path to a directory on your hard drive that represents a package of classes. It is placed as the first statement of a file that contains a class definition in order to indicate that the class may refer to the classes in the directory given by the <path>.
The Java spec uses "inheritance" slightly differently: private fields are not inherited. However, the private fields DO appear in each object of the subclass, so we think the Java use of "inherited" is wrong and will continue to say that ALL components are inherited.
Here's an analogy. Suppose a 16-year-old son's parent's die. He inherits their money. But the money is put in a Trust, and, he cannot touch the money until he is 18. That's like saying the private field appears in each object of the subclass but it is private.
This pdf file explains why nested classes are useful.
Study this example of the use of a static nested class before reading the next example: pdf file.
Here is a simple but realistic example of the use of an inner class: pdf file.
(1) A bit about the Java IO packages; intro to paths of files and directories: pdf file.
(2) Interface Path and classes Files and Paths: pdf file.
(3) Read a text file using a BufferedReader: pdf file.
(4) Write a text file using a BufferedWriter and a PrintWriter: pdf file.
(5) Read a webpage —class URL: pdf file.
(6) Use a JFileChooser to obtain a Path: pdf file.
See this pdf file for a full explanation.
See this pdf file for an explanation.
See this pdf file for an explanation of char.
See this pdf file for an explanation of byte, short, int, and long.
The second video on this page is a 2.4-minute tutorial that explains interfaces.
See this page to learn how to write a JUnit testing class in Eclipse, for example, to test the methods in a class.
This pdf file explains how to test whether an assert statement is correct.
This pdf file explains ow to test whether a method call throws a particular exception.
JUnit 4 annotations: pdf file.
See this pdf file for literals of the number types (like int and float).
See this pdf file for literals of type char
Principle: Place a local variable as close to its first use as possible. Do not simply declare all local variables at the beginning of a method. FOLLOW THIS PRINCIPLE.
for-loop, while-loop, do-loop, foreach loop.
You can enable the use of the foreach statement on your own collection class by implementing interfaces Iterator and Iterable. This tutorial shows you how in less than 15 minutes of video: webpage.
1. Push a frame for the call onto the call stack;
2. Assign argument values to the parameters;
3. Execute the method body;
4. Pop the frame for the call from the call stack (and, if a function, put the function value onto the call stack).
the signature of method <T, E> m1(T p, E q) {...} would be <T1, T2> m1(T1, T2)
the signature of method <E1, E2> m2(E1 x, E2) {...} would be <T1, T2> m2(T1, T2)
so they are the same except for the name of the method.
byte --> short --> int --> long --> float --> double
char --> int --> long --> float --> double
subclass --> superclass
See cast to find complete information on casting.
This pdf file explains why nested classes are useful.
Here is a simple but realistic example of the use of a static nested class: pdf file.
Here is a simple but realistic example of the use of an inner class: pdf file.
See this pdf file for an explanation of char.
See this pdf file for an explanation of byte, short, int, long, float, and double.
Fields are not overridden but are hidden. This is explained in this one-page pdf file.
Static components are not overridden but are hidden. This is explained in this one-page pdf file.
As a convention, we generally write a precondition in a specification as "Precondition: ..." to make it as clear as possible.
(2) An assertion placed (as a comment) before a statement in a program to indicate what is true at that place during execution.
See this pdf file for an explanation of primitive type char.
See this pdf file for an explanation of the other primitive number types: byte, short, int, long, float, and double.
See this pdf file for an explanation of type boolean.
1. for a static procedure: <class-name> . <function-name> ( <arguments> ) ;
2. for a non-static procedure: <expression> . <function-name> ( <arguments> ) ;
where the <expression> evaluates to the name of an object that contains the procedure to be called. Both "<class-name>." and "<expression>." can be omitted if the call appears in a place in which the procedure can be called without them.
results in unexpected (and wrong) results. See this pdf file.
But if you are not familiar with arrays in Java, read this first: pdf file.
See this pdf file for an intro to Java's reflection capabilities. Demo_code.zip
This pdf file explains why nested classes are useful.
Here is a simple but realistic example of the use of a static nested class: pdf file.
This pdf file talks about how to study in this course. Please take it seriously.
This pdf file talks about the process of completing a programming assignment. Please take it seriously.
(1) super.m(...) calls method m within the object in which it appears. To determine which version of m is called, use the bottom-up (overriding) rule, but start looking in the partition above the one in which the method call appears.
(2) super(...); can appear as the first statement in a constructor; it calls a constructor of the superclass ---which one is called depends on the types of the arguments within the parentheses..
<variable> = <expression> ;
See JUnit testing to find out how to write and run repeatable testing procedures.
(1) this is an expression; it evaluates to the name of (pointer to) the object in which it appears. For example, this.x is a reference to a field x of the object.
(2) "this(...);" can appear as the first statement in a constructor; it calls another constructor in this class ---which one is called depends on the types of the arguments within the parentheses.
Learn about race conditions (pdf file 2) and deadlock (pdf file 3).
Creating and starting threads. pdf file 4.
Some methods of class Thread. pdf file 5.
Intro to synchronized statements and methods. pdf file 6.
Intro to wait() and notifyAll(). pdf file 7.
Implementing a bounded queue in an array and a bounded buffer. pdf file 8.
Java classes that provide for concurrency. pdf file 9.
Warning: Synchronize all accesses to an object. pdf file 10.
Warning: Do not use notify() unless you know what you are doing. pdf file 11.
What's a daemon thread? pdf file 12.
Watch a 3.2-minute video on throwable objects here.
Watch a 3.2-minute video on throwable objects here.
Watch a 5-minute video on interpreting the output of an uncaught thrown object here.
Watch a video on the throw-statement and a video on catching and throwing the exception further here.
try <statement>
catch (<class-name1 e1>) <catch-statement1>
...
catch <class-namen en>) <catch-statementn>
where each of the <class-names> is a throwable class. There can also be a "finally clause", which we don't discuss. The try-statement is explained, with videos, here and also in this pdf flle.
Java has primitive types and class types.
- (6+2), 6! . Unary prefix operators have highest precedence and are evaluated from right to left. E.g. - + - 8 + 5 is evaluated as (-(+(- 8))) + 5. See also ternary operator and binary operator.