Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1/27/2018
John Roberts
2
Overview
• Notes on Java
• Default Constructor
• Reference Semantics
• Exception Handling
3
Notes on the Java Language
• Based on C
3
4
Notes on the Java Language
5
Notes on the Java Language
6
Notes on the Java Language
6
7
Overview
• Notes on Java
• Default Constructor
• Reference Semantics
• Exception Handling
8
Write Once, Run Anywhere
Java Source Code
Program.java
Output
8
9
Java Virtual Machine (JVM)
9
10
Sandboxes
10
11 Talk a little bit about good programming practice with a compilation example
Overview
• Notes on Java
• 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?
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?
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?
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?
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?
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?
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
• 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
21
Comments on Code Style
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
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
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
26
Executing
(master ✔) ᐅ java Hello folks
• java Hello folks Hello folks
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
• Default Constructor
• Reference Semantics
• Exception Handling
27
28
The Default Constructor
29
The Default Constructor
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
• 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 }
• Github: ReferenceSemantics.java
32
33
Output
ᐅ java ReferenceSemantics
a's i is 7
aa's i is 7
33
34
Reference Semantics
35
Reference Semantics
35
36
Overview
• Notes on Java
• 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