9
CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4

CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4

Embed Size (px)

Citation preview

Page 1: CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4

CSC 243 - Java Programming, Fall, 2008

Week 2: Java Data Types, Control Constructs, and their C++

counterparts, September 4

Page 2: CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4

References• GNU make http://www.gnu.org/software/make/manual/ • http://java.sun.com/javase/downloads/index.jsp docs.• http://java.sun.com/javase/6/docs/ has on-line docs.• http://java.sun.com/javase/6/docs/api/index.html class lib.• /export/home/faculty/parson/JavaLang on bill.kutztown.edu• Make sure that /usr/jdk/jdk1.6.0_02/bin is near the front of UNIX PATH.

• Follow instructions in the JavaShellSetup.pdf document (accessible via my home page at http://faculty.kutztown.edu/parson) to set up and verify your Java compilation environment.

Page 3: CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4

First example Java program

• CountArgString counts distinct, non-overlapping occurrences of a string in a file

• /export/home/faculty/parson/JavaLang/week1/lecture1

• Make sure you “cp –pr ~parson/JavaLang ~/JavaLang to get JavaLang into your home directory

• Make sure CLASSPATH include your $HOME/JavaLang• makefile drives execution of compile and test steps

• gmake build to compile Java files• gmake test to run test cases• gmake clean to clean up test output and compiled files

Page 4: CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4

Summary of Java built-in types

• Primitive type variables are not class objects• byte is 8-bit signed, -128 through 127 (page 32 in text)• short is 16-bit signed, -32768 through 32767• int is 32-bit signed, long is 64-bit signed• float is 32-bit type and double is 64-bit type• boolean is 1-bit true or false, char is UNICODE

• Some class types are built into the language• java.lang.String a class built into the language• Wrapper classes in java.lang wrap native types as

classes. Examples are Integer, Float, Long, Double.

Page 5: CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4

Summary of Java control constructs

• boolean (true or false) is the basis for control flow• /export/home/faculty/parson/JavaLang/week1/bool• for, while and do-while loops, just like C++• if .. else if .. else selection, just like C++• switch statement (text p. 86) also like C++• break exits the innermost loop or switch• continue goes to the top of innermost loop• There are no stand-alone functions! A method always exists

inside a class.• A non-static method uses a class object.• A static method uses only class-static data.

Page 6: CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4

Java Methods

• A method is equivalent to a C++ member function. In Java all methods resides in classes.

• A static method does not require an object.• int intvar = java.lang.Integer.parseInt(“2”);• System.out.println(“integer: “ + intvar);• Statics method use static class variables.

• A regular method needs an object reference.• Integer intObject = new Integer(“3”);• System.out.println(“integer: “ + intObject.intValue());

Page 7: CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4

Java storage of class and object data

• Only one copy of a static data element exists for its class. Both static methods and non-static methods (object methods) can use it.

• static java.lang.String oneStringPerClass = new String();

• There is one copy of each non-static data element in each object. Only non-static methods can use it.

• String oneStringPerObject = new String(“my value”);

Page 8: CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4

Java access to class and object methods and data

• Public methods and data can be used by any code that imports the class.

• Protected methods and data can be used only by the defining class and derived classes.

• Private methods and data can be used only by the defining class.

• If there is no explicit access restriction, then package methods and data can be used by any class in the package.

Page 9: CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4

Boolean example code

• /export/home/faculty/parson/JavaLang/week1/bool• Make sure you have a copy of this.

• cp –pr ~parson/JavaLang/week1/bool ~/JavaLang/week1/bool

• Program verifies valid command line args.• 1. Program reads command line into an array of

integers, raising an Exception if any of the command line arguments is not an integer.

• 2. Verify that array is in ascending order using &&.• 3. Verify that array is in ascending order using ||.