Lecture 2 COMP1406/1006 (the Java course) Fall 2013 M. Jason Hinek Carleton University
today s agenda a quick look back (last Thursday) assignment 0 is posted and is due this Friday at 2pm Java compiling and running a Java program primitive data types (and objects) casting arrays static methods (procedural programming) what s next? 2
last time... /* hello world */ public class HelloWorld{ public static void main(string[] args){ System.out.println("hello, world!"); } } access modifier public top level access modifier specifies who can see HelloWorld member level access modifier specifies who can access main (non access) modifier static allows a method to be called without an instance of the class return type void it is a Java keyword that tells us that a method returns nothing it is not an actual Java type System.out System is a class with three attributes/fields in, out and err out is a PrintStream object, it is standard output println is a method of out 3
last time... /* hello world */ public class HelloWorld{ public static void main(string[] args){ System.out.println("hello, world!"); } } access modifier public top level access modifier specifies who can see HelloWorld member level access modifier specifies who can access main (non access) modifier static allows a method to be called without an instance of the class return type void it is a Java keyword that tells us that a method returns nothing it is not an actual Java type System.out System is a class with three attributes/fields in, out and err out is a PrintStream object, it is standard output println is a method of out 3
last time... /* hello world */ public class HelloWorld{ public static void main(string[] args){ System.out.println("hello, world!"); } } access modifier public top level access modifier specifies who can see HelloWorld member level access modifier specifies who can access main (non access) modifier static allows a method to be called without an instance of the class return type void it is a Java keyword that tells us that a method returns nothing it is not an actual Java type System.out System is a class with three attributes/fields in, out and err out is a PrintStream object, it is standard output println is a method of out 3
last time... /* hello world */ public class HelloWorld{ public static void main(string[] args){ System.out.println("hello, world!"); } } access modifier public top level access modifier specifies who can see HelloWorld member level access modifier specifies who can access main (non access) modifier static allows a method to be called without an instance of the class return type void it is a Java keyword that tells us that a method returns nothing it is not an actual Java type System.out System is a class with three attributes/fields in, out and err out is a PrintStream object, it is standard output println is a method of out 3
last time... /* hello world */ public class HelloWorld{ public static void main(string[] args){ System.out.println("hello, world!"); } } access modifier public top level access modifier specifies who can see HelloWorld member level access modifier specifies who can access main (non access) modifier static allows a method to be called without an instance of the class return type void it is a Java keyword that tells us that a method returns nothing it is not an actual Java type System.out System is a class with three attributes/fields in, out and err out is a PrintStream object, it is standard output println is a method of out 3
last time... /* hello world */ public class HelloWorld{ public static void main(string[] args){ System.out.println("hello, world!"); } } what do we know about System.out.println()? 4
last time... /* hello world */ public class HelloWorld{ public static void main(string[] args){ System.out.println("hello, world!"); } } what do we know about System.out.println()? we can look at the API for the System class <link> API - application programming interface specifies how to use a given class 4
assignment 0 academic regulations coding style for COMP1406/1006 basic Java programs using just a main method using command line arguments assignment 1 user input procedural programming testing writing methods 5
assignment 0 academic regulations coding style for COMP1406/1006 basic Java programs using just a main method using command line arguments assignment 1 user input procedural programming testing writing methods 5
running hello world /* Java hello world */ public class HelloWorld{ public static void main(string[] args){ System.out.println("hello, world!"); } } Java convention is that class name is capitalized (use camel case if more than one word) class XXX must be in the file XXX.java so HellowWorld must be in the file HelloWorld.java first we need to compile the source code into Java bytecode IDE will have a compile button javac HelloWorld.java from console window (shell) this creates HelloWorld.class, which is the Java bytecode next, we run the bytecode in the JVM (Java virtual machine) java HelloWorld from the console window runs out program! the JVM executes the main method of our program 6
running hello world /* Java hello world */ public class HelloWorld { public static void main(string[] args){ System.out.println("hello, world!"); } } Java convention is that class name is capitalized (use camel case if more than one word) class XXX must be in the file XXX.java so HellowWorld must be in the file HelloWorld.java first we need to compile the source code into Java bytecode IDE will have a compile button javac HelloWorld.java from console window (shell) this creates HelloWorld.class, which is the Java bytecode next, we run the bytecode in the JVM (Java virtual machine) java HelloWorld from the console window runs out program! the JVM executes the main method of our program 6
running hello world /* Java hello world */ public class HelloWorld{ public static void main(string[] args){ System.out.println("hello, world!"); } } Java convention is that class name is capitalized (use camel case if more than one word) class XXX must be in the file XXX.java so HellowWorld must be in the file HelloWorld.java first we need to compile the source code into Java bytecode IDE will have a compile button javac HelloWorld.java from console window (shell) this creates HelloWorld.class, which is the Java bytecode next, we run the bytecode in the JVM (Java virtual machine) java HelloWorld from the console window runs out program! the JVM executes the main method of our program 6
running hello world /* Java hello world */ public class HelloWorld{ public static void main(string[] args){ System.out.println("hello, world!"); } } Java convention is that class name is capitalized (use camel case if more than one word) class XXX must be in the file XXX.java so HellowWorld must be in the file HelloWorld.java first we need to compile the source code into Java bytecode IDE will have a compile button javac HelloWorld.java from console window (shell) this creates HelloWorld.class, which is the Java bytecode next, we run the bytecode in the JVM (Java virtual machine) java HelloWorld from the console window runs out program! the JVM executes the main method of our program 6
running hello world /* Java hello world */ public class HelloWorld{ public static void main(string[] args){ System.out.println("hello, world!"); } } Java convention is that class name is capitalized (use camel case if more than one word) class XXX must be in the file XXX.java so HellowWorld must be in the file HelloWorld.java first we need to compile the source code into Java bytecode IDE will have a compile button javac HelloWorld.java from console window (shell) this creates HelloWorld.class, which is the Java bytecode next, we run the bytecode in the JVM (Java virtual machine) java HelloWorld from the console window runs out program! the JVM executes the main method of our program 6
running Java programs XXX.java 7
running Java programs XXX.java 7
running Java programs XXX.java javac XXX.java 7
running Java programs XXX.java javac XXX.java 7
running Java programs XXX.java javac XXX.java java.class 7
running Java programs XXX.java javac XXX.java java.class 7
running Java programs XXX.java javac XXX.java java.class java XXX 7
running Java programs XXX.java javac XXX.java java.class java XXX 7
running Java programs XXX.java javac XXX.java java.class java XXX!!??!! 7
command line arguments vs user input both are ways of providing some information to your program command line arguments input is entered before program runs input is passed as parameters to main method (args) (not practical for many user inputs) user input input is entered while program is running standard input is the keyboard (very flexible and useful) 8
let s take a break... for 3 minutes 9
Java date types in Java, everything is an object 10
Java date types in Java, everything is an object except for the 8 things that aren t! 10
Java date types in Java, everything is an object except for the 8 things that aren t! Java has eight primitive data types byte, short, int, long float, double boolean char (integers) (approximate real numbers) (logical true/false) (unicode characters) 10
Java date types there are four primitive data types for exact integers byte 8-bit signed integers 128 127 short 16-bit signed integers 32, 768 32, 767 int 32-bit signed integers 2,147,483,648 2,147,483,647 long 64-bit signed integers 9,223,372,036,854,775,808 9,223,372,036,854,775,807 11
Java date types there are two primitive data types for approximate decimal numbers (approximation to real numbers) float 32-bit IEEE 754 floating point 1 bit for sign, 8 bits for exponent, 23 bits for fraction accuracy about 7 decimal digits of accuracy double 64-bit IEEE 754 floating point 1 bit for sign, 11 bits for exponent, 52 bits for fraction accuracy about 15-16 decimal digits of accuracy 12
Java date types and two more... boolean true or false char 16-bit Unicode character chars enclosed in single quotes x, 3, Q, etc from \u0000 (zero) to \uffff (65,635) unlike other languages, char is not equivalent to a byte (8-bits) 13
type conversion automatic type conversion JVM will do some conversions for you explicit type conversion cast use a method 14
type conversion automatic type conversion JVM will do some conversions for you explicit type conversion cast use a method expression cat + 22 Integer.parseInt( 32 ) (int) 4.99999 Math.round(2.1) 11 * 0.3 (int) 11 * 0.3 11 * (int) 0.3 (int) (11*0.3) expression value 14
type conversion automatic type conversion JVM will do some conversions for you explicit type conversion cast use a method expression expression value cat + 22 cat22 String Integer.parseInt( 32 ) (int) 4.99999 Math.round(2.1) 11 * 0.3 (int) 11 * 0.3 11 * (int) 0.3 (int) (11*0.3) 14
type conversion automatic type conversion JVM will do some conversions for you explicit type conversion cast use a method expression expression value cat + 22 cat22 String Integer.parseInt( 32 ) 32 int (int) 4.99999 Math.round(2.1) 11 * 0.3 (int) 11 * 0.3 11 * (int) 0.3 (int) (11*0.3) 14
type conversion automatic type conversion JVM will do some conversions for you explicit type conversion cast use a method expression expression value cat + 22 cat22 String Integer.parseInt( 32 ) 32 int (int) 4.99999 4 int Math.round(2.1) 11 * 0.3 (int) 11 * 0.3 11 * (int) 0.3 (int) (11*0.3) 14
type conversion automatic type conversion JVM will do some conversions for you explicit type conversion cast use a method expression expression value cat + 22 cat22 String Integer.parseInt( 32 ) 32 int (int) 4.99999 4 int Math.round(2.1) 2 int 11 * 0.3 (int) 11 * 0.3 11 * (int) 0.3 (int) (11*0.3) 14
type conversion automatic type conversion JVM will do some conversions for you explicit type conversion cast use a method expression expression value cat + 22 cat22 String Integer.parseInt( 32 ) 32 int (int) 4.99999 4 int Math.round(2.1) 2 int 11 * 0.3 3.3 float (int) 11 * 0.3 11 * (int) 0.3 (int) (11*0.3) 14
type conversion automatic type conversion JVM will do some conversions for you explicit type conversion cast use a method expression expression value cat + 22 cat22 String Integer.parseInt( 32 ) 32 int (int) 4.99999 4 int Math.round(2.1) 2 int 11 * 0.3 3.3 float (int) 11 * 0.3 3.3 float 11 * (int) 0.3 (int) (11*0.3) 14
type conversion automatic type conversion JVM will do some conversions for you explicit type conversion cast use a method expression expression value cat + 22 cat22 String Integer.parseInt( 32 ) 32 int (int) 4.99999 4 int Math.round(2.1) 2 int 11 * 0.3 3.3 float (int) 11 * 0.3 3.3 float 11 * (int) 0.3 0 int (int) (11*0.3) 14
type conversion automatic type conversion JVM will do some conversions for you explicit type conversion cast use a method expression expression value cat + 22 cat22 String Integer.parseInt( 32 ) 32 int (int) 4.99999 4 int Math.round(2.1) 2 int 11 * 0.3 3.3 float (int) 11 * 0.3 3.3 float 11 * (int) 0.3 0 int (int) (11*0.3) 3 int 14
operator precedence order matters! 15
operator precedence order matters! BEDMAS 15
operator precedence order matters! BEDMAS elementary school... brackets exponents division and multiplication addition and subtraction 15
operator precedence 15
arrays and memory models let s look at arrays in Java... and lets draw pictures... 16
what s next? tutorial this week creating, compiling and running Java programs command line arguments/user input arrays next class towards OOP testing your code blackbox testing whitebox testing errors 17