• Tidak ada hasil yang ditemukan

CompSci 230 Software Construction

N/A
N/A
Protected

Academic year: 2023

Membagikan "CompSci 230 Software Construction"

Copied!
17
0
0

Teks penuh

(1)

CompSci 230 Software Construction

Lecture Slides #2: Hello World! S1 2016

(2)

Agenda

COMPSCI 230: OOD 2

Topics:

Java Basics

Getting started – our first .java file

“Hello world!” in Java and Python

Backward and forward compatibility

Syntax and semantics

(3)

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

E.g., if our class is called HelloWorld, we’ll put it into a file called HelloWorld.java

We can edit the .java file with any text editor

In 230, we will be using an integrated development environment (Eclipse) to do this

(4)

Java Basics - compilation

COMPSCI 230: OOD 4

We compile the class with the Java compiler javac:

On the command line: javac HelloWorld.java

Compilation results in a bytecode file with the extension .class (if we don’t have any syntax errors in the source code)

E.g., HelloWorld.class

Note: In Eclipse, we can invoke javac differently – more on this shortly

(5)

Java Basics – running a program

Once we have the .class file, we can run the program using the Java VM, e.g.:

java HelloWorld

Note: We omit the .class – the Java VM knows what to look for

In Eclipse, we just hit the “Play” button (green circle with white triangle)

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.

(6)

Java Basics – application structure

COMPSCI 230: OOD 6

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

(7)

“Hello World!” in Java

HelloWorld.java (Java source code):

public class HelloWorld {

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

} }

What does all this mean?

(8)

“Hello World!” in Java

COMPSCI 230: OOD 8

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)

It also allows our program to compile (in this case)

(9)

“Hello World!” in Java

HelloWorld.java (Java source code):

public class HelloWorld {

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

} }

class HelloWorld means that what follows is a class declaration of a class named “HelloWorld”

In that respect, Java’s no different from Python

(10)

“Hello World!” in Java

COMPSCI 230: OOD 10

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.

An opening curly brace marks the beginning of a block of code

A closing curly brace marks the end of a block of code

Curly braces must match, i.e., for every opening brace, there must be a closing brace

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!

(11)

“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

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

(12)

“Hello World!” in Java

COMPSCI 230: OOD 12

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

This parameter contains an array of arguments passed in from the command line (if any)

(13)

“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

Note again the curly braces that enclose the block with the code of the method

(14)

“Hello World!” in Java

COMPSCI 230: OOD 14

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

Guess what this does!

See http://stackoverflow.com/questions/3406703/whats-the-meaning-of- system-out-println-in-java for the gory details

(15)

In Eclipse

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!");

(16)

In Eclipse

COMPSCI 230: OOD 16

(17)

Review questions

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?

Referensi

Dokumen terkait

l Consider the following class stored in a file called MyApplication.java: public class MyApplication { public static void mainString[] args { MyProgram p = new MyProgram;

Object Instantiation COMPSCI 230: IOOD 10  When a constructor method is called, a new instance is created..  If a class definition doesn’t include a constructor method, the Java

public class Test{ public static void mainString args[]{ //tambahkan nasabah pada bank dengan nama Andi Sucipto dengan konstruktor tambahNasabahString,String, besar tabungan

3 package A; public class MainA { public static void mainString args[]{ } } package A; public class TestA { private int varPrivateA ; int varDefaultA ; protected int

public static void mainString args[] { B obj = new B; obj.methodA; //calling super class method obj.methodB; //calling local method } } 2 Multiple Inheritance “Multiple

Listing 13-7: Make the Calling Method Handle the Exception package com.allmycode.naptime; class GoodNightsSleepB { public static void mainString args[] { System.out.println“Excuse

Java Application Menampilkan Tulisan //Welcome1.java Public class Welcome1 { //main method memulai eksekusi aplikasi java public static void main String args[] {

import java.util.Scanner; public class FinalExamB{ public static void mainString []args{ Scanner s=new ScannerSystem.in; System.out.println"Choose one of the