review and java questions 13 june 2009. classes, objects, attributes, methods in java, by convention...
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
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
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. 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( );
...
}
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 Values Examples
boolean true false true, false
char character 'a', 'A', '1', 'ก', 'ค', 'โ', '\t'byte 8-bit integer -127, ..., -1, 0, 1, ..., 127
short 16-bit integer -32768 ... 0 ... 32767
intint 32-bit integer -400 47 20000000
long 64-bit integer -1234567890L 0L 888L
float decimal 3.14159F 0.0F -2.5E-8F
double 64-bit decimal 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 Wrapper
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
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 ) );
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.