java introduction - uwoece-se2205b-2017.github.io€¦ · java provides several desktop toolkits...

24
SE2205B - DATA STRUCTURES AND ALGORITHMS JAVA INTRODUCTION Kevin Brightwell Thursday January 5th, 2017 Acknowledgements:Dr. Quazi Rahman 1 / 24

Upload: others

Post on 30-Apr-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

SE2205B - DATA STRUCTURES AND ALGORITHMS

JAVA INTRODUCTIONKevin Brightwell

Thursday January 5th, 2017

Acknowledgements:Dr. Quazi Rahman

1 / 24

Page 2: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

LECTURE OUTLINE

What is Java and the PlatformJava Development ToolsAnatomy of a Java Program

2 / 24

Page 3: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

WHAT IS JAVA?

Java is:Object-orientedCross-platformMulti-paradigm-ishMulti-threadedHigh-performanceInterpreted - sorta

3 / 24

Page 4: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

WHAT IS JAVA?JAVA TOOLCHAIN

Workflow from "code" to "running"

1. Compile Java code to Java Byte code (not machine code)2. Java virtual machine interprets the bytecode

4 / 24

Page 5: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

WHAT IS JAVA?

JAVA TOOLCHAIN

1. Bytecode is Platform Independant, able to run on anyJava Virtual Machine

2. Commonly Java is referred to as slow, in fact, due to"Just-in-Time" compilation, it often out performs otherlanguages

5 / 24

Page 6: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

WHAT IS JAVA?JAVA APPLICATIONS

Mobile ApplicationThe vast majority of Android applications are written inJava

Desktop ApplicationsJava provides several desktop toolkits out-of-the-box,e.g. ,

Web ServicesBusiness applications, e.g. much of Google and IBM'sinfrastructure runs on Java

SWT JavaFX

6 / 24

Page 7: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

LECTURE OUTLINE

What is Java and the PlatformJava Development ToolsAnatomy of a Java Program

7 / 24

Page 8: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

JAVA DEVELOPMENT TOOLSJava code is compiled using javac

Do not do this by hand, it is neither instructive or usefulMost Java developers use Integrated DevelopmentEnvironments (IDE)

Eclipse - Free, extremely flexible and advancedenvironment for multiple platformsIntelliJ IDEA - Enterprise but free student licenses are

We will use IntelliJ Idea in this courseavailable

7 / 24

Page 9: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

9 / 24

Page 10: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

JAVA INTRODUCTION

SIMPLE JAVA PROGRAMpackage uwo.se2205.lectures.unit1;

/** * Driver class that simply prints: "Hello, world!" */public class Welcome { public static void main(String[] args) { System.out.println("Hello, world!"); }}

10 / 24

Page 11: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

SIMPLE JAVA PROGRAM - RUNNING

Video: |

Update: I have spliced a clip showing how to create the src/main/java folder, ignore the error

MP4 OGG

11 / 24

Page 12: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

TRACE A PROGRAMJava

C++

Both define the same "entry" method to a programPrint to the console

package uwo.se2205.lectures.unit1;

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

#include <iostream>

int main(int argc, char** args) { std::cout << "Hello, world!" << std::endl;}

12 / 24

Page 13: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

LECTURE OUTLINE

What is Java and the PlatformJava Development ToolsAnatomy of a Java Program

13 / 24

Page 14: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

ANATOMY OF A JAVA PROGRAMCOMMENTS

Line commentsMulti-line commentsJava-doc comments, starts with two ** values

/** * Summary text * @param bar Describes the bar type * @return factored by bar */public double floorTwoPointFiveBar(int bar) { // Line comment double result = Math.floor(bar * 2.5);

/* multi-line * comment */ return result;}

14 / 24

Page 15: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

ANATOMY OF A JAVA PROGRAM

PACKAGE

Java packages allow for organizing code within a programMost packages consist of related classesPackages are "nested", e.g. lectures.unit1, unit1is a "subpackage" of lecturesAll Classes (later) must belong to a packagepackage uwo.se2205.lectures.unit1;

public class Welcome { // ... snip ...}

15 / 24

Page 16: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

ANATOMY OF A JAVA PROGRAM

MODIFIERS

Modifiers are reserved words that specify the properties ofdata, methods, and classes and how they can be used.Examplespublicprotectedprivatestaticfinalabstracttransient

16 / 24

Page 17: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

ANATOMY OF A JAVA PROGRAM

STATEMENT

Statements represent an action or sequence of actions.All statements end with a ;// Comment about it! // <-- Not a statementSystem.out.println("Hello!"); // <-- Statement

17 / 24

Page 18: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

ANATOMY OF A JAVA PROGRAM

BLOCKS

Identical to blocks in C++Scoping rules apply

int foo = 1;for (int i = 0; i < 20; i++) { System.out.println("i = " + i);`}System.out.println("foo = " + foo);// System.out.println("i = " + i); // <- i does not exist here

18 / 24

Page 19: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

ANATOMY OF A JAVA PROGRAM

CLASSES

Designates a blueprint for an ObjectClasses are the most important concept in JavaClasses live in their own Java file (e.g. Welcome.java)Classes must be in a package

Java

C++

class Welcome {} // <- No semicolon

class Welcome {};

19 / 24

Page 20: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

ANATOMY OF A JAVA PROGRAM

METHOD

Collection of statements to execute an "operation"Methods are always attached to a ClassConcrete method implementations are always inline withdeclarationclass Welcome { /** * Summary text * @param bar Describes the bar type * @return factored by bar */ public double floorTwoPointFiveBar(int bar) { return Math.floor(bar * 2.5); }}

20 / 24

Page 21: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

ANATOMY OF A JAVA PROGRAM

METHOD

Methods may be static or instance

Java

C++

class Welcome { public static void staticFoo(int bar) { } public void instanceFoo(int bar) { }}

class Welcome {public: static void staticFoo(int bar); void instanceFoo(int bar);};

21 / 24

Page 22: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

ANATOMY OF A JAVA PROGRAM

MAIN METHOD

The main method provides the flow of a programAs seen previously, the main method is nearly identical toC++A program can not run unless a main method is definedIt must be defined with the signature:

public static void main(String[] args) { // `args` is the same as the values in `argv` in C++

System.exit(0); // exit with no problems}

22 / 24

Page 23: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

ANATOMY OF A JAVA PROGRAM

RETURN TO PACKAGES/CLASSES

Java is very opinionated about where it's files goWithin your source directory

if a class is in package ca.uwo.jsmith2 then it mustlive within the folder ca/uwo/jsmith2If your class name is Welcome and it's package isca.uwo.jsmith2, then it lives atca/uwo/jsmith2/Welcome.javaAdditionally, the fully qualified name of Welcome is:ca.uwo.jsmith2.Welcome

23 / 24

Page 24: JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits out-of-the-box, e.g. , Web Services Business applications, e.g. much of Google and

LAB 0The questions will be basic algorithms, mostly to get you

familiar with Java/IntelliJ

24 / 24