These notes present the basics of Java in outline form. They only discuss the ways in which Java differs from C++. If something is not discussed, such as for loops, if-then-else statements, or constructors, it is because they are identical in both C++ and Java.
class HelloWorld { public HelloWorld() { System.out.println("Hello World"); } static public void main(String args[]) { new HelloWorld(); } }
javac HelloWorld.java
java HelloWorld
int sum(int x, int y) { return x + y; } int multiply(int x, int y) { return x * y; } int main(int argc, char *argv[]) { sum(3,6); multiply(9,5); }In Java, a comparable program should be written as:
class Arithmetic { public Arithmetic() { sum(3,6); multiply(9,5); } int sum(int x, int y) { return x + y; } int multiply(int x, int y) { return x * y; } public static void main(String args[]) { new Arithmetic(); } }A lot of novice Java programmers think this code is too complicated and try to write it more simply as:
class Arithmetic { int sum(...) int multiply(...) public static void main(String args[]) { sum(3,6); multiply(9,5); } }The java compiler will complain about this code though because sum and multiply have not been declared static as well. If you call a function from a static function, then the called function must also be declared static. The reason is that non-static functions expect a pointer to an object to be passed to them via the this variable. However, static functions like main don't have objects associated with them and hence cannot pass a reference to an object to a non-static function. Trying to keep track of all the function calls made from a static function becomes tiresome, and hence it is best to simply move the code that typically appears in main to a constructor instead, and then invoke that code by having main create a single instance of the class.
int countPositive(int [] a) { int count = 0; int i, j; for (i = 0; i < a.length; i++) { int j = a[i]; if (j > 0) count++; } j = a.length - count; System.out.println("number of negatives = " + j); return count; }
int countPositive(...) { ... for (int i = 0; i < 10; i++) { ... } for (int i = 20; i > 10; i--) { ... } }
int a; double b; b = a; // okay because there is no loss of precision
a = b; // not okay because the fractional portion will be lost when // b's value is assigned to a
a = (int) b; // okay. Java will truncate the fractional part of b's // value and assign it to a
b instanceof Stack b instanceof LinkedList b instanceof Objectare all true. A way of seeing why this makes sense, is that you typically ask if an object is an instance of something because you want to either cast to that type, or because you want to use a certain method. It is always safe to lose "precision" by casting to a superclass object, which is a less precise object.
Example: String a = "Hello"; a = a + " World";In this example, the concatenation operation creates a completely new string to hold "Hello World" and then makes a point to the new string. The old string object remains unaltered with its contents still being "Hello". Since no other variables point to it, it is eventually garbage collected.
String token; String day = "Wednesday"; ... if (token.equals("+")) ... else if (token.equals(day)) ...
e.g., int a []; int [] b; int c [] = new int[6]; int [] d = new int[10];
String weekdays [] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
Person students[] = new Person[10];you do not have an array of 10 Person objects like you would in C++. You have an array of 10 null pointers. If you now write a statement such as:
students[2].name = "Brad";then Java's virtual machine will throw a null pointer exception because students[2] is null, not a Person object. If you want the array to be initialized to 10 Person objects, then you must explicitly assign Person objects to each entry:
for (i = 0; i < students.length; i++) { students[i] = new Person(); }
e.g., System.out.println(c.length) will print 6
e.g., int a [][] = new int[4][5]; int b [][] = new int[4][]; b[0] = new int[5]; b[1] = new int[4]; ...
e.g., int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622 }; int sum = 0; for (int element : arrayOfInts) { sum += element; }You must declare element in the for statement. If you try to declare element outside the for statement, you will get a compiler error message.
class List { public void insertAfter(Object referenceObj, Object objToInsert); public Object getHead(); public Object getTail(); ... }
Stack a; Stack b = new Stack(); b.push(3); a = b; a.push(5); // b's stack now contains both 3 and 5
Example: class Stack { int size = 10; int top = 0; ... }
class RandomValues { int randomNums[] = new int[10]; // Initialization block { for(int i=0; i<randomNums.length; i++) { randomNums[i] = (int)(100.0*Math.random()); } } }
Example: static String color = "red";
class Point { public double x; public double y; }
e.g., class foo { ... public class goo { ... } }foo.goo a = new foo.goo() will fail because a goo can only be created by an instance of a foo
final double SALES_TAX = 0.05;
void swap(int &a, int &b) { int temp = a; a = b; b = temp; } actual call: swap(x, y);
void swap(*int a, int *b) { int temp = *a; *a = *b; *b = temp; } actual call: swap(&x, &y)As you can see, there are many more opportunities to introduce an error when you have to explicitly take the address of arguments and dereference parameters.