170 likes | 180 Views
CompSci 230 Software Construction. Lecture Slides #2: Hello World! S1 2016. Agenda. Topics: Java Basics Getting started – our first .java file “Hello world!” in Java and Python Backward and forward compatibility Syntax and semantics. Java Basics –source code.
E N D
CompSci 230Software Construction Lecture Slides #2: Hello World! S1 2016
Agenda • Topics: • Java Basics • Getting started – our first .java file • “Hello world!” in Java and Python • Backward and forward compatibility • Syntax and semantics
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
Java Basics - compilation • 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
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.
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
“Hello World!” in Java publicclassHelloWorld { publicstaticvoid main(String[] args) { System.out.println("Hello World!"); } } • What does all this mean? HelloWorld.java (Java source code):
“Hello World!” in Java publicclassHelloWorld { publicstaticvoid 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) HelloWorld.java (Java source code):
“Hello World!” in Java publicclassHelloWorld { publicstaticvoid 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 HelloWorld.java (Java source code):
“Hello World!” in Java publicclassHelloWorld { publicstaticvoid 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! HelloWorld.java (Java source code):
“Hello World!” in Java publicclassHelloWorld { publicstaticvoid 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 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 HelloWorld.java (Java source code):
“Hello World!” in Java publicclassHelloWorld { publicstaticvoid 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) HelloWorld.java (Java source code):
“Hello World!” in Java publicclassHelloWorld { publicstaticvoid 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 HelloWorld.java (Java source code):
“Hello World!” in Java publicclassHelloWorld { publicstaticvoid 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 HelloWorld.java (Java source code):
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!");
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? What is a key difference in the way we specify method parameters in Java and in Python?