Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review and Java Questions 13 June 2009. Classes, objects, attributes, methods In Java, by convention a class name begins with Uppercase: Coffee, String.

Similar presentations


Presentation on theme: "Review and Java Questions 13 June 2009. Classes, objects, attributes, methods In Java, by convention a class name begins with Uppercase: Coffee, String."— Presentation transcript:

1 Review and Java Questions 13 June 2009

2 Classes, objects, attributes, methods In Java, by convention a class name begins with Uppercase: Coffee, String a method name uses camelCase: getMoreCoffee( ) a method has ( ) variable names also use camelCase: myCoffee constants use UPPER_CASE and _: MAX_COFFEE package names are all lowercase (but not always) primitive types are all lowercase: boolean, int, double

3

4 Identify each of these Is it a package class primitive type attribute ("field") (static or instance) method (static or instance) constant interface (more advanced) ??? Date System.out System.out.println( ) System.nanoTime( ) double Double java.lang.Double.MAX_VALUE java.lang.BigInteger java.lang.Comparable java.text java.util.ArrayList java.util.List

5 Packages  Java uses packages to organize classes.  Packages reduce size of name space and avoid name collisions (like Date in java.util and java.sql ). Q: Which package contain these classes? Java language core classes (Object, String, System,...). You never have to "import" this. Classes for input and output, like InputStream Date classes, collections (List, ArrayList), utilities Scanner, Arrays Java Graphics frameworks (2 packages)

6 Resolving Ambiguity  There is a java.util.Date class and java.sql.Date  In this code, which Date class will Java use? 1. java.util.Date (because it was imported first) 2. java.sql.Date (because it was imported last) 3. depends on Java implementation 4. neither -- compiler error if ambiquous import java.util.*; import java.sql.*; class Ambiguous { Date today = new Date( );... }

7 Resolving Ambiguity (2)  How can you resolve this problem (2 solutions)? Solution 1: Solution 2: import java.util.*; import java.sql.*; import java.util.Date; // (1) class Ambiguous { java.util.Date today = new java.util.Date( ); // (2)... }

8 How to convert number to String  How can you convert n to a String (many solutions)? int n = 100; String s = n; // error: must convert to string s1 = Integer.toString( n ); s2 = "" + n; s3 = String.valueOf( n ); s4 = String.format( "%d", n );

9 Java Primitive Data Types  Java has 8 primitive data types  These are not classes or objects. No properties. NameValuesExamples booleantrue falsetrue, false charcharacter'a', 'A', '1', ' ก ', ' ค ', ' โ ', '\t' byte 8-bit integer -127,..., -1, 0, 1,..., 127 short16-bit integer-32768... 0... 32767 int int32-bit integer-400 47 20000000 long64-bit integer-1234567890L 0L 888L floatdecimal3.14159F 0.0F -2.5E-8F double64-bit decimal3.14159265358979E234

10 How to Convert Primitive to Object? A List (like ArrayList) can only contain objects. How can we convert a primitive type to an object? List mylist = new ArrayList( ); int id = 51651111; mylist.add( id ); // works in Java 5+, but something is happening

11 Wrapper Classes PrimitiveWrapper booleanBoolean charCharacter byteByte shortShort intInteger longLong floatFloat doubleDouble double root = Math.sqrt( 2.0 ); Double d1 = new Double( root ); // same thing: automatic boxing Double d2 = root; // print as a string out.println( d2.toString( ) ); // static method to make a string out.println( Integer.toString( 2 ) );

12 Wrapper to convert to/from String int n = 2*3*5*7*11*13*17*19*23*29*31; // convert n to a String String product = Integer.toString(n); String s = "2.5"; // convert s to a double?

13 Numeric wrappers: range constants  What is the largest "int" value?  What is the smallest "long" value?  What is the range (smallest, biggest) of double? int biggest = long smallest = double minsize = double maxsize =

14 What happens if you go beyond? int n = Integer.MAX_VALUE; n = n + 1; System.out.println( n ); double d = Double.MAX_VALUE; d = d + 1; System.out.println( d ); d = d * 1.000001; System.out.println( d );

15 C# is different  "int", "float", "double" are struct types. // This is C# int n = int.MaxValue; String s = "The biggest integer is " + n.ToString( ) ; // range checking is enforced n = n + 1; System.OverflowException: Arithmetic operation resulted in an overflow.


Download ppt "Review and Java Questions 13 June 2009. Classes, objects, attributes, methods In Java, by convention a class name begins with Uppercase: Coffee, String."

Similar presentations


Ads by Google