CompSci 230 Software Construction

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Your First Java Program: HelloWorld.java
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
OOP & JAVA. HelloWorld.java /** * The HelloWorld class is an application that * displays "Hello World!" to the standard output. */ public class HelloWorld.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Slide 1 of 40. Lecture A The Java Programming Language Invented 1995 by James Gosling at Sun Microsystems. Based on previous languages: C, C++, Objective-C,
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
POS 406 Java Technology And Beginning Java Code
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
CSE 131 Computer Science 1 Module 1: (basics of Java)
Compiling and the Java Virtual Machine (JVM). The syntax of Pseudocode is pretty loose –visual validation encourages a permissive approach –emphasized.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
CIT 590 Intro to Programming First lecture on Java.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Introduction to programming in the Java programming language.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
CSI 3125, Preliminaries, page 1 Compiling the Program.
CS 106 Introduction to Computer Science I 01 / 22 / 2007 Instructor: Michael Eckmann.
Creating a Java Application and Applet
OOP Basics Classes & Methods (c) IDMS/SQL News
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
CS 177 Recitation Week 1 – Intro to Java. Questions?
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
Terms: Software  Set of instructions  aka programs  Operating System:  Special software whose job it is to translateinstructions into hardware instructions.
CS-140 Dick Steflik Lecture 3. Java C++ Interpreted optimized for the internet Runs on virtual ized machine Derived from C++ Good object model Widely.
Java Methods and Applications CSIS 3701: Advanced Object Oriented Programming.
Computer Programming Your First Java Program: HelloWorld.java.
Information and Computer Sciences University of Hawaii, Manoa
Eclipse.
CompSci 230 Programming Techniques Lecture 1 Semester
The eclipse IDE IDE = “Integrated Development Environment”
Dept of Computer Science University of Maryland College Park
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Intro to ETEC Java.
Chapter 3 GC 101 Java Fundamentals.
Lecture 5: Some more Java!
Introduction to.
Java Primer 1: Types, Classes and Operators
USING ECLIPSE TO CREATE HELLO WORLD
CompSci 230 Software Construction
Intro to Java.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Programming Vocabulary.
String Output ICS 111: Introduction to Computer Science I
How to Run a Java Program
An Introduction to Java – Part I, language basics
Chapter 3 Classes and Objects
Java Intro.
Anatomy of a Java Program
Introduction to Java Brief history of Java Sample Java Program
IB Computer Science II Paul Bui
Java Looking at our first console application in Eclipse
Corresponds with Chapter 5
How to Run a Java Program
Presentation transcript:

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 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 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 COMPSCI 230: OOD

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 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.: 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. 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 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!"); } 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) 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!"); } 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. 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! 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 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 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 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 Guess what this does! See http://stackoverflow.com/questions/3406703/whats-the-meaning-of- system-out-println-in-java for the gory details COMPSCI 230: OOD

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!"); COMPSCI 230: OOD

In Eclipse COMPSCI 230: OOD

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? COMPSCI 230: OOD