JAVA INTRODUCTION - uwoece-se2205b-2017.github.io€¦ · Java provides several desktop toolkits...

Preview:

Citation preview

SE2205B - DATA STRUCTURES AND ALGORITHMS

JAVA INTRODUCTIONKevin Brightwell

Thursday January 5th, 2017

Acknowledgements:Dr. Quazi Rahman

1 / 24

LECTURE OUTLINE

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

2 / 24

WHAT IS JAVA?

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

3 / 24

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

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

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

LECTURE OUTLINE

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

7 / 24

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

9 / 24

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

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

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

LECTURE OUTLINE

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

13 / 24

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

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

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

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

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

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

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

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

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

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

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

familiar with Java/IntelliJ

24 / 24

Recommended