Download Lecture 2: Hello World

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CompSci 230
Software Construction
Lecture Slides #2: Hello World! S1 2016
Agenda

Topics:





2
Java Basics
Getting started – our first .java file
“Hello world!” in Java and Python
Backward and forward compatibility
Syntax and semantics
COMPSCI 230: OOD
Java Basics –source code


In Java, each program is a class.
In source code, each class sits in a test file that has the same name
as the class and the extension .java


We can edit the .java file with any text editor

3
E.g., if our class is called HelloWorld, we’ll put it into a file called
HelloWorld.java
In 230, we will be using an integrated development environment (Eclipse)
to do this
COMPSCI 230: OOD
Java Basics - compilation

We compile the class with the Java compiler javac:


Compilation results in a bytecode file with the extension .class
(if we don’t have any syntax errors in the source code)


4
On the command line: javac HelloWorld.java
E.g., HelloWorld.class
Note: In Eclipse, we can invoke javac differently – more on this
shortly
COMPSCI 230: OOD
Java Basics – running a program

Once we have the .class file, we can run the program using the
Java VM, e.g.:



In Eclipse, we just hit the “Play” button (green circle with white
triangle)


5
java HelloWorld
Note: We omit the .class – the Java VM knows what to look for
This takes care of compilation AND running of the program in the VM
That’s provided we’re building our program as part of an Eclipse project,
in which case we get a little extra complexity thrown in.
COMPSCI 230: OOD
Java Basics – application structure

A program may use additional classes (we’ll get to that later), which
also sit in their own files (except in the case of nested classes,
which we’ll discuss much later)

The class that “starts” the program must have a class method (also
called static method) which returns nothing (void).

OK, and now it’s time to look at our HelloWorld.java
6
COMPSCI 230: OOD
“Hello World!” in Java

HelloWorld.java (Java source code):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

7
What does all this mean?
COMPSCI 230: OOD
“Hello World!” in Java

HelloWorld.java (Java source code):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}



public is our first nod to “development in the large”
It’s a so-called access modifier. public means that code from
outside the application can see this class (and use it)
This allows the developers of other classes to use the
HelloWorld class in their code (if they want to)

8
It also allows our program to compile (in this case)
COMPSCI 230: OOD
“Hello World!” in Java

HelloWorld.java (Java source code):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}


9
class HelloWorld means that what follows is a class
declaration of a class named “HelloWorld”
In that respect, Java’s no different from Python
COMPSCI 230: OOD
“Hello World!” in Java

HelloWorld.java (Java source code):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

Curly braces in Java have the same purpose as whitespace indentation in Python.



Curly braces must match, i.e., for every opening brace, there must be a closing brace



10
An opening curly brace marks the beginning of a block of code
A closing curly brace marks the end of a block of code
The concept is very similar to parentheses in mathematical formulas
Here, the curly braces marked by the boxes indicate the block that contains the class
declaration
Note: javac ignores whitespace indentation completely – we use it only to make
the code easier to read for humans!
COMPSCI 230: OOD
“Hello World!” in Java

HelloWorld.java (Java source code):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

This is the declaration of our main() method




11
It’s also public, meaning code from outside the class can invoke the
method
The static means that we can invoke the method without having to
build an object from the blueprint that the HelloWorld class represents
The void means that the method returns nothing
Note there’s no def as you have it in Python
COMPSCI 230: OOD
“Hello World!” in Java

HelloWorld.java (Java source code):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

In the declaration of our main() method




The method signature is very similar to a Python method (method name followed
by parentheses with a parameter list)
New here: the String[] in front of the parameter args
Remember that all variables (and function return values) in Java need a type (unlike
in Python, where the Python interpreter figures this out at runtime)
Here, the type of the parameter args is an array (“[]”) whose elements are of
type String

12
This parameter contains an array of arguments passed in from the command line (if any)
COMPSCI 230: OOD
“Hello World!” in Java

HelloWorld.java (Java source code):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

In the declaration of our main() method

13
Note again the curly braces that enclose the block with the code of the
method
COMPSCI 230: OOD
“Hello World!” in Java

HelloWorld.java (Java source code):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

In the declaration of our main() method


14
Guess what this does!
See http://stackoverflow.com/questions/3406703/whats-the-meaning-ofsystem-out-println-in-java for the gory details
COMPSCI 230: OOD
In Eclipse





15
File -> New -> Java Project
Enter HelloWorld as the project name and save
You’ll see HelloWorld pop up in the Package Explorer on the left
Add a class (File -> New -> Class) named HelloWorld and Eclipse
will create almost everything else for you.
All you need to do is add the line with the code:
System.out.println("Hello World!");
COMPSCI 230: OOD
In Eclipse
16
COMPSCI 230: OOD
Review questions










17
Which form does a program take in Java?
What are the essential elements of any Java program?
How do we store the source code for classes in Java?
How do we name Java class files?
What does the modifier “public” do?
What is the purpose of curly braces in Java?
Can you give a few examples of types in Java?
What does the “static” mean in front of a method declaration?
How do we compile a Java program, and how do we run it?
What is a key difference in the way we specify method parameters
in Java and in Python?
COMPSCI 230: OOD