Download Review1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Review and Java Questions
13 June 2009
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
Identify each of these
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
Is it a
package
class
primitive type
attribute ("field")
(static or instance)
method
(static or instance)
constant
interface (more advanced)
???
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)
Resolving Ambiguity
 There is a java.util.Date class and java.sql.Date
 In this code, which Date class will Java use?
1.
2.
3.
4.
java.util.Date (because it was imported first)
java.sql.Date (because it was imported last)
depends on Java implementation
neither -- compiler error if ambiquous
import java.util.*;
import java.sql.*;
class Ambiguous {
Date today = new Date( );
...
}
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)
...
}
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 );
Java Primitive Data Types
 Java has 8 primitive data types
 These are not classes or objects. No properties.
Name
boolean
char
Values
true false
character
Examples
true, false
'a', 'A', '1', 'ก', 'ค', 'โ', '\t'
byte
short
8-bit integer
16-bit integer
-127, ..., -1, 0, 1, ..., 127
-32768 ... 0 ... 32767
int
long
float
double
32-bit integer
64-bit integer
decimal
64-bit decimal
-400 47 20000000
-1234567890L 0L 888L
3.14159F 0.0F -2.5E-8F
3.14159265358979E234
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
Wrapper Classes
Primitive
boolean
char
byte
short
int
long
float
double
Wrapper
double root = Math.sqrt( 2.0 );
Boolean
Character
Byte
Short
Integer
Long
Float
Double
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 ) );
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?
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 =
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 );
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.