Home CPSC 330

Programming in Java

Things that are the Same as C++


Basic Types

Unlike C++, the sizes of numeric types are exactly defined. Also, Java has no unsigned types.


Strings and Objects

Strings in Java are more like the string class than character arrays. The following program uses a few strings.


public class Strings {
  public static void main(String args[]) {
    String first = "Bob";
    String last = "Smith";
    String full = first + " " + last;
    System.out.println("Hello " + full + "!");
    System.out.print("Your name has ");
    System.out.print(first.length() + last.length());
    System.out.println(" letters.");
  }
}

However, comparing strings is a little tricky in Java:


public class Strings2 {
  public static void main(String args[]) {
    String a = new String("hello");
    String b = new String("hello");

    if(a == b) {
      System.out.println("Equal!");
    } else {
      System.out.println("Not Equal!");
    }
  }
}

The == and != operators only compare the objects themselves. a and b are different objects which happen to hold the same data.

The .equals method should be used instead.


Dynamic memory

If the Strings2 program above were C++, there would be a memory leak. However in Java, we never need to use "delete" because Java has garbage collection.

Also, there are no pointers in Java, new returns a reference to an object.


Arrays

Arrays are similar to how they are in C++, but provide some useful functions C++ arrays don't have.

This example shows the basic usage of arrays:


public class Arrays {
  public static void main(String args[]) {
    int[] array = new int[20];

    // fill it up
    for(int i = 0; i < array.length; i++) {
      array[i] = i;
    }
    
    // print them out
    for(int i = 0; i < array.length; i++) {
      System.out.println(array[i]);
    }
  }
}

One big difference between the arrays of Java and C++ is that Java's are bounds-checked:


public class Arrays2 {
  public static void main(String args[]) {
    int[] array = new int[20];

    // fill it up
    for(int i = 0; i <= 20; i++) {
      array[i] = i;
    }
    
    // print them out
    for(int i = 0; i <= 20; i++) {
      System.out.println(array[i]);
    }
  }
}

Parameter Passing

In Java, all parameters are passed by value. Thus assigning into a parameter never has any affect:


public class Passing {
  
  public static void function(int a, String s) {
    // no effect
    a = 3;
    s = "Hello";
  }
  
  public static void main(String args[]) {
    int x= 5;
    String word = "what";
    function(x, word);
    System.out.println(x);
    System.out.println(word);

  }
}

However, all objects in Java are always references, so we are actually passing a reference by value. This means that the variable cannot be reassigned, but can be changed:


public class Passing2 {
  
  public static void function(int array[]) {
    // change!
    array[0] = 0;
  }
  
  public static void main(String args[]) {
    int [] array = new int[5];
    array[0] = 1;
    function(array);
    System.out.println(array[0]);

  }
}

Output

We have already seen the System.out.println function. There is also system.out.print which is identical except it does not add a new line after printing.

There is also the System.out.printf function which works like the printf function from C.


public class Printf {
  public static void main(String args[]) {
    String name = "Bob Smith";
    int age = 31;

    System.out.printf("Hello %s, you are %d years old!\n", name, age);

  }
}

Input

Input with Java is a little more complex. We will use the "Scanner" class. This first must be imported from java.util:


import java.util.Scanner;

This tells Java where to find the Scanner class we will make an object out of. This is more like "using namespace" than it is like "#include".

Next we will create a Scanner object:


Scanner in = new Scanner(System.in)

Passing System.in means we want to use standard input.

We can then use it to input from the user:


import java.util.Scanner;

public class Input {
  public static void main(String args[]) {
    // create a Scanner object
    Scanner in = new Scanner(System.in);
    
    // prompt for input
    System.out.println("Enter your name: ");

    // read in a string
    String name = in.next();

    // Skip the newline
    in.nextLine();

    // prompt for input
    System.out.println("Enter your age: ");
    
    // read in an int
    int age = in.nextInt();

    // greet
    System.out.printf("Hello %s, you are %d years old!\n", name, age);
  }
}

Small Differences


The Java Class Library

A big benefit of Java is the huge class library. It includes:

It also has complete documentation. Here are some pages for classes we have covered:

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.