COS 212 Resources

COS 212 Resources

  • Docs

›Java Crash Course

Getting Started

  • Introduction
  • Installing Linux
  • Development Environment

Java Crash Course

  • Hello World
  • Compiling and Running
  • Makefile
  • Comments
  • Variables and Types
  • Operators
  • Slides

Tools

  • Version Control
  • Hello World with Git

Notes

  • Self Organising Lists

Hello World

The structure of this crash course was borrowed from learnjavaonline.org

The Java Crash Course slides cover more areas but in less detail.

A good place to start and test the Java Installation is with the classic Hello World.

Main.java

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Compiling and running the above code will output Hello, World!.

Lets analyse the parts of the code.

public class Main {...
  • Defines a class with the name Main.
  • The name of source file has to match the name of the class defined in it. In this case it is Main.java.
  • Only one class can be defined per source file.
  • Methods can only be defined in a class.
  • Statements can only be written in a class definition.
  • Unlike C++, there are no header files. One source file will contain your class and method definitions.
  • public indicates the class is accessible from other Java Packages. In the context of COS 212 only public class will be used.
public static void main(String[] args) {...
  • This is the entry point for our program. The signature has to match exactly as above.
  • The Access Level Modifier public specifies that the method is visible to all classes.
    • private specifies that is is accessible only within the class.
  • static specifies that the method can be called without instantiating an instance of the class.
    • i.e Main.main()
  • void specifies that the method returns no value.
  • String[] is the type for an array of Strings. We will go over the different types soon.
  • args will contain the command line arguments used to start the program.
System.out.println("Hello, World!");
  • System is a Java class providing useful class fields and methods.
  • out is a static member variable that represents the "standard" output stream.
  • println is a method of out that prints the given string along with a new line character.
Last updated on 2/11/2019 by Evert Geldenhuys
← Development EnvironmentCompiling and Running →
  • Main.java
COS 212 Resources
Docs
Getting Started
GitHubStar
Facebook Open Source
Copyright © 2019 Evert Geldenhuys