review and java questions 13 june 2009. classes, objects, attributes, methods in java, by convention...

15
Review and Java Questions 13 June 2009

Upload: brittany-lester

Post on 17-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

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

Review and Java Questions

13 June 2009

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

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

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

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

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

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)

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

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( );

...

}

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

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)

...

}

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

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 );

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

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

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

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

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

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 ) );

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

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?

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

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 =

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

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 );

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

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.