Sei sulla pagina 1di 13

1

CSC 413 - Software Development


3. Java Refresher

1/27/2018

John Roberts

2
Overview
• Notes on Java

• Write Once, Run Anywhere

• Compiling and Executing

• Default Constructor

• Reference Semantics

• Exception Handling

3
Notes on the Java Language

• Developed at Sun Microsystems in 1995

• An Object Oriented Programming Language

• Based on C

• Evolved from C++ and Smalltalk

3
4
Notes on the Java Language

• Goal was to provide a ubiquitous programming


environment for web-based programming

• Completely portable system within a distributed


programming environment on heterogeneous
architectures

5
Notes on the Java Language

• Simple - no multiple inheritance like C++

• OO - Everything is an object (except primitives)

• Network savvy - Libraries provided to interact with URLs,


sockets, etc.

• Robust - Built in exception handling, garbage collection

6
Notes on the Java Language

• Architecture neutral - Has its own VM (more on the VM in


minute)

• Portable - Its byte codes don’t depend on the host


machine, just on the VM. Machine dependent details are
fixed (e.g. size of int, float)

• Multithreaded - Parallel execution, client-server


computing

• Dynamic - Pluggable code and classes

6
7
Overview
• Notes on Java

• Write Once, Run Anywhere

• Compiling and Executing

• Default Constructor

• Reference Semantics

• Exception Handling

8
Write Once, Run Anywhere
Java Source Code
Program.java

Analyzes source program and generates Java machine


Java Compiler
javac Program.java code (byte code) - for the JVM. Puts byte code in the
file Program.class

Java VM Uses the JVM to execute the byte codes in


java Program Program.class

Output
8

9
Java Virtual Machine (JVM)

• A program that emulates the Java machine by


interpreting the byte codes

• Java machine is a specification, implemented as a


program (different program on different architectures)

• Scans byte codes and performs the indicated operations


on the virtual machine

• Since the JVM is a program, instances of it can execute


the same byte codes on whatever machine it runs on

9
10
Sandboxes

• The VM interprets byte codes and doesn’t allow codes


that will access or modify the client’s files

• Java programs allowed to “play” in special sandboxes,


where it can’t do any harm

10

11 Talk a little bit about good programming practice with a compilation example
Overview
• Notes on Java

• Write Once, Run Anywhere

• Compiling and Executing

• Default Constructor

• Reference Semantics

• Exception Handling

11

12
An example program

• Github: Hello.java

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }
12
13
What does this program do?

• Line 1 - import components from the standard library

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }

14
What does this program do?

• Line 3 - One public class allowed per file, where the


class name is used as the file name Hello.java

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }

15
What does this program do?

• Line 4 - Main program that takes an array of String


arguments from the user

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }
16
What does this program do?

• Line 5 - length is a property of the array object,


accessed with the dot operator

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }

17
What does this program do?

• Line 6 - Using the + to concatenate Strings

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }

18
What does this program do?

• Line 6, 8 - System.out.println used to print a


String on a line followed by a carriage return/line feed

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }
19
Comments on Code Style
• Use of curly braces in control flow blocks, even though only one statement

• Guards against future errors

• Easier to maintain

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }

20 You are entitled to an opinion on tabs vs. spaces, but it’s wrong if it doesn’t
Comments on Code Style agree with mine in code submitted for this class.
• Consistent indentation WITH SPACES

• Tabs are displayed differently in different editors, sometimes making code


much more difficult to read

• Spaces displayed consistently EVERYWHERE


1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }

21
Comments on Code Style

• Consistent alignment of braces and blocks of code

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }
22
Comments on Code Style

• Submitting code with tabs, inconsistent alignment of


braces or code blocks, etc. will result in loss of points!

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }

23
Compiling

• javac Hello.java

• Results in a .class file, containing byte codes

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }

24
Compiling

• javac Hello.java

• Results in a .class file, containing byte codes

[11:32:30] ~/workspace/CSC-413/Lecture-3 (master ✔) ᐅ xxd Hello.class


00000000: cafe babe 0000 0034 002b 0a00 0b00 1509 .......4.+......
00000010: 0016 0017 0700 180a 0003 0015 0800 190a ................
00000020: 0003 001a 0a00 0300 1b0a 001c 001d 0800 ................
00000030: 1e07 001f 0700 2001 0006 3c69 6e69 743e ...... ...<init>
00000040: 0100 0328 2956 0100 0443 6f64 6501 000f ...()V...Code...
00000050: 4c69 6e65 4e75 6d62 6572 5461 626c 6501 LineNumberTable.
25
Executing
(master ✔) ᐅ java Hello
• java Hello Hello everybody!

• Note the absence of the .class extension when


executing!
1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }

26
Executing
(master ✔) ᐅ java Hello folks
• java Hello folks Hello folks

• We can pass in command line arguments

1 import java.lang.*;
2
3 public class Hello {
4 public static void main ( String args[] ) {
5 if ( args.length > 0 ) {
6 System.out.println( "Hello " + args[0] );
7 } else {
8 System.out.println( "Hello everybody!" );
9 }
10 }
11 }

27
Overview
• Notes on Java

• Write Once, Run Anywhere

• Compiling and Executing

• Default Constructor

• Reference Semantics

• Exception Handling

27
28
The Default Constructor

• Recall that if the programmer does not provide ANY


constructors for a class, then the compiler will include a
default, public, no-argument constructor

• In this example, the compiler provides



public A() {}
1 public class A {
2 int i;
3
4 void p() {
5 System.out.println( i );
6 }
7 }
28

29
The Default Constructor

• In this example, no default constructor is provided - you


must always instantiate B with an int argument

1 public class B {
2 int i;
3
4 public B( int j ) {
5 i = j;
6 }
7
8 void p() {
9 System.out.println( i );
10 }
11 }
29

30
Overview
• Notes on Java

• Write Once, Run Anywhere

• Compiling and Executing

• Default Constructor

• Reference Semantics

• Exception Handling

30
31 What do we expect the output will be?
A code example

• Github: ReferenceSemantics.java
16 class A {
17 private int i = 0;
18
19 public void incI( B bb ) {
20 i = bb.inc();
21 }
22
23 public int getI() {
24 return this.i;
25 }
26 }
27
28 class B {
29 private int i = 0;
30
31 B( int i ) {
32 this.i = i;
33 }
34
35 public int inc() {
36 return ++i;
37 } 31
38 }

32 What do we expect the output will be?


A code example

• Github: ReferenceSemantics.java

1 public class ReferenceSemantics {


2 public static void main( String[] args ) {
3 A a = new A();
4 A aa;
5 aa = a;
6
7 B b = new B( 5 );
8 aa.incI( b );
9 a.incI( b );
10
11 System.out.format( " a's i is %d\n", a.getI() );
12 System.out.format( "aa's i is %d\n", aa.getI() );
13 }
14 }
15

32

33
Output

ᐅ java ReferenceSemantics
a's i is 7
aa's i is 7

33
34
Reference Semantics

• Unlike C++, in Java we can only refer to objects (except


primitives)

• Line 3: a refers to an instance of A

• Line 5: a and aa refer to the same object


1 public class ReferenceSemantics {
2 public static void main( String[] args ) {
3 A a = new A();
4 A aa;
5 aa = a;
/* Snip */
13 }
14 }
15
34

35
Reference Semantics

• Lines 8, 9 - We’re setting i in the same object


8 aa.incI( b );
9 a.incI( b );
10
16 class A {
17 private int i = 0;
18
19 public void incI( B bb ) {
20 i = bb.inc();
21 }
22
23 public int getI() {
24 return this.i;
25 }
26 }

35

36
Overview
• Notes on Java

• Write Once, Run Anywhere

• Compiling and Executing

• Default Constructor

• Reference Semantics

• Exception Handling

36
37 Note we may have many different types of exceptions occur within a try that
Exception Handling we may want to handle depending on exception type
• Exception are out of the ordinary - not necessarily errors

• Syntax:

try {

<statements that may throw exception>

} catch ( ExceptionType exceptionOne ) {

<statements to handle exception>

}

…

} catch ( ExceptionType exceptionN ) {

<statements to handle exception>

} finally {

<statements that will always be executed, with or
without exception>

}

37

38
Raising Exception Without Catching Properly
1 public class ExceptionTestOne {
2 static int numbers[] = { 0, -2, 5, 27, 3};
3
4 public static void main( String[] args ) {
5 int sum = 0;
6
7 for ( int i = 0; i <= 5; i++ ) {
8 sum += numbers[ i ];
9 // attempt to access numbers[5] which is outside the array bounds
10 }
11 }
12 }

ᐅ java ExceptionTestOne
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 5
at ExceptionTestOne.main(ExceptionTestOne.java:8)
38

39
Properly Catching a Raised Exception
1 public class ExceptionTestTwo {
2 static int numbers[] = { 0, -2, 5, 27, 3};
3
4 public static void main( String[] args ) {
5 int sum = 0;
6
7 try {
8 for ( int i = 0; i <= 5; i++ ) {
9 sum += numbers[ i ];
10 }
11 } catch ( ArrayIndexOutOfBoundsException e ) {
12 System.out.println( "*** Array index out of bounds" );
13 }
14
15 System.out.println( "Sum: " + sum );
16 }
17 }

ᐅ java ExceptionTestTwo
*** Array index out of bounds
Sum: 33 39

Potrebbero piacerti anche