1 java basics. 2 compiling a “compiler” is a program that translates from one language to...

33
1 Java Basics

Post on 19-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

1

Java Basics

Page 2: 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

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 C or Assembly to machine code Java must be (explicitly) compiled before it is

run The Java compiler turns Java source code

(.java) into Java bytecode (.class)

Page 3: 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

3

The Java Platform

The Java Virtual Machine (JVM) is responsible for running bytecode

The idea: bytecode can be interpreted quickly The same bytecode can be interpreted on

any architecture: write once, run anywhere Code (C,C++) compiled to machine code

must be compiled to a specific system

Page 4: 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

4

The Java Language

Created by Sun Microsystems Introduced in 1995, initial popularity grew due

to Internet applications Excitement surrounding Java applets Confusion with Javascript

Steady rise in popularity has continued for “better” programming reasons

Page 5: 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

5

A Historical Interlude: The Java Team Java originally intended to be used on “smart”

consumer electronics Bill Joy

Founded Sun, 1982 Intelligent robots will replace humanity in the near future…

James Gosling (“the father of Java”) University of Calgary grad First JVM, compiler, interpreter also developed Emacs

Patrick Naughton Arrested in late 90s on child predator charges Not mentioned so much as a founding father anymore

Page 6: 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

6

The Java Language (cont’d)

… is a high-level programming language … is very object oriented … is similar to C++ and C … typically compiled to Java bytecode … is often confused with the Java Platform,

but these are two different aspects of “Java”

Page 7: 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

7

Syntax and Semantics

The syntax rules of a language define how we can combine reserved words, symbols, and identifiers

The semantics of a program statement define what the statement means Problem with program syntax = “error” Problem with program semantics = “bug”

Page 8: 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

8

Java Program Structure

A Java program consists of: One or more classes A class contains one or more methods A method contains program statements

We will explore these terms in detail

Page 9: 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

9

Java Program Structure

public class MyProgram

{

}

// comments about the class

class header

class body

Comments can be placed almost anywhere

Page 10: 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

10

Java Program Structure

public class MyProgram

{

}

// comments about the class

public static void main (String[] args)

{

}

// comments about the method

method headermethod body

Page 11: 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

11

Hello World

public class HelloWorld

{

}

// HelloWorld.java

public static void main (String[] args){

}

System.out.println(“Hello World!”);

Page 12: 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

12

Hello World

public class HelloWorld

{

}

// HelloWorld.java

public static void main (String[] args){

}

System.out.println(“Hello World!”);

Creates a “class” called HelloWorld Compiled to HelloWorld.class Classes used to define objects… later

Page 13: 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

13

Hello World

public class HelloWorld

{

}

// HelloWorld.java

public static void main (String[] args){

}

System.out.println(“Hello World!”);

The “main” method is where it starts to run Ignore “public static void” and “String[] args” for

now

Page 14: 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

14

Hello World

public class HelloWorld

{

}

// HelloWorld.java

public static void main (String[] args){

}

System.out.println(“Hello World!”);

Contains one “statement” The System.out.println function comes from

the Java “class library” Ends with a semicolon (all statements do)

Page 15: 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

15

Compiling and Running

Create the file HelloWorld.java in a text editor Compile:

javac HelloWorld.java Run:

java HelloWorld Output:

Hello World!

Page 16: 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

16

Comments

Three kinds of comments:

To simplify: comments are good

// a one-line comment/* a multi-line comment *//** a javadoc comment */

Page 17: 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

17

Reserved Words and Identifiers Reserved words are specified by the

language All Java reserved words are in the text

Identifiers are specified by a programmer Maybe you: e.g. HelloWorld Maybe someone else: e.g. println

Page 18: 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

18

Restrictions and Conventions

Restriction Identifiers can not start with a digit

Conventions Title case for class names: HelloWorld Uppercase for constants: MAX

Page 19: 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

19

White Space Conventions

Idea: make programs easy to read Use consistent indentation Use blank lines and comments to visually

separate methods The fact that it compiles doesn’t make it

right…

Page 20: 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

20

Strong Typing

Java is a “strongly typed” language All variables and values have a specific type Type is known when the program is

compiled…. before it is run So all variables and values must be declared

with a type before being used

Page 21: 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

21

Declaring Variables

Syntax:

Examples: int count1, int count 2; int count = 0; String course1 = “CMPT 126”;

<variable declaration> ::= <type> <declarator>, …. ;<declarator> ::= <identifier><declarator> ::= <identifier> = <expression>

Page 22: 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

22

Assignment

We use the = operator for variable assignment Initialization is a special case

When a value is assigned, the old value is overwritten

In Java, we use the final modifier to declare a variable constant final int MAX_HEIGHT = 6;

Page 23: 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

23

Primitive Data Types in Java

Four integer types: byte, short, int, long

Two floating point types float, double

One of them is for characters char

One of them is for boolean values boolean

Page 24: 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

24

Expressions and Assignment

An expression is a combination of one or more operators and operands

Arithmetic operators: +, -, *, /, % Use the normal order of operations

e.g. int exp = 2 * 5 +7;

count = count + 1;

count++; Boolean operators: &&, ||

Page 25: 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

25

More Assignment Operators

x += y is equivalent to x = x + y Also:

-= *= /= %=

Page 26: 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

26

Data Conversion

Non-matching types can be converted A widening conversion is automatic

e.g. from short to int A narrowing conversion may lose information

e.g. from float to int Three kinds of conversion:

Assignment Promotion Casting

Page 27: 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

27

Assignment Conversion

final int dollars = 6;

double money;

money = dollars;

Only works for widening conversion

Page 28: 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

28

Promotion

int count = 2;

float mass = 18.342;

mass = mass / count;

Passing count to an operator that expects floating point values

Page 29: 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

29

Casting

float mass = 18.342;

int roundedmass = (int) mass;

Casting works for widening and narrowing In this example, decimal part is just lost Note: this does not actually round

Page 30: 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

30

Object Types

The primitive types aren’t really enough Java also allows object types, or classes

Typically capitalized Object variables hold references to objects

The declaration only creates a reference This is different from primitive types

Variables of primitive type hold a value

Page 31: 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

31

Example: String Objects

We have already seen one object type in Java: String

A String object is a list of characterse.g. “Hello world!” or “My name is Aaron”

Can be passed to print or println Can be concatenated using the (+) operator

e.g. “Hello world! ” + “My name is Aaron”

“I can also append numbers, like “ + 2

Page 32: 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

32

Object Instances

We must create a new “instance” of an object to store something Each object type has a constructor (more later) Create instances using the reserved world new

e.g. course = new String(“CMPT 126”); This creates a new String in memory

It stores the characters “CMPT 126” The assignment sets course to refer to this

instance

Page 33: 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

33

References and Instances

String course;

new String(“CMPT 126”)

course = new String(“CMPT 126”);

course:

CMPT 126

CMPT 126course: