Title: Static, main, math, Strings
1Static, main, math, Strings
2What helps me learn Java the most
- Going to the Mac lab
- Writing Java on the Board
- Doing the Homework
- Watching the Instructor code in BlueJ
- Doing the Programming Assignments
- clickers
3Review reading javadoc and methods
- java.lang.Math
- static double sqrt(double a) Returns
the correctly rounded positive square root of a
double value. - All methods in java.lang.Math are static
4- To call sqrt, do the following
- int x Math.sqrt(4)
- int x sqrt(4)
- Math.sqrt(4)
- int x new Math( ) x.Math.sqrt(4)
- int x new Math( ) x.sqrt(4)
5static
- static means "class level", not "object level"
- public int x // note public just for demo
- // instance variables should be private
- public static int y
exists at class level before any objects are
created
object is created on object bench
6Using static variables/methodsFor each line ok?
A. YES B. NO
- class MyClass public int x
- public
static int y - MyClass c new MyClass( ) // ok?
- c.x 4 // ok?
- c.y 8 // ok?
- MyClass d new MyClass( ) //ok?
- // c.x is A. 0 B. 4 C. neither D. undefined
- // c.y is A. 0 B. 8 C. neither D. undefined
7More on using static
- class MyClass public int x
- public
static int y - MyClass c new MyClass( )
- c.y 8 // ok? A. YES B. NO
- MyClass.y 8 // ok? A. YES B. NO
- MyClass.x 4 // ok? A. YES B. NO
- For static variables/names, use name of class,
not object name. can use either - For object variables, use name of object, not
class name. canNOT use either
8Why have static?
- If instance variables are not needed
- Math.PI, Math.sqrt, JOptionPane.showMessageDialog
- when in doubt, do NOT use static
9static methods
- static methods must use static everything since
they exist even if an object doesn't - SO
- static methods can only call static methods
- static methods cannot modify instance variables
10main
- main is the first method that is called when a
program is run - main must be static in Java because it is created
before an object is created - because main is static, all methods it calls must
be static - moral main should create an object, and that
object should do the work of the program
11program with main
- public class MyClass
-
- // do stuff with methods and constructors
- public static void main(String args)
-
- new MyClass( ) // call constructor
12String args
- You can specify arguments at the command line
13- public static void main(String args)
-
- for (String mystring args)
- // Note NO references to the array in foreach
- System.out.println("You entered "
mystring) - / the same loop with a for would be
- for (int i0iltargs.lengthi)
- System.out.println("You entered " argsi)
- /
-
14Wrapper Classes
Wrapper Type Primitive Type
Boolean boolean
Integer int
Double double
Character char
- Sometimes, primitives need to act like objects
and the reverse - Each primitive has a "wrapper" class that makes
it look like an object
primitives are all lowercase
Wrappers are instances of Classes, so are capital
15this refers to the classor class's instance
variable
- public class Member
-
- private String name
- private String motto
- privateltMembergt friends new
ArrayListltMembergt ( ) - public void addMyBestFriend( )
-
- // want to say
- // friends.add(me)
- friends.add(this) // will add the current
Member as a friend -
16More practice using javadoc String(expand S.o.p
to System.out.println)
- int compareTo(String anotherString)
Compares two strings
lexicographically. - Returns the value 0 if the argument string is
equal to this string a value less than 0 if this
string is lexicographically less than the string
argument and a value greater than 0 if this
string is lexicographically greater than the
string argument. - Call to compareTo
- String s1, s2 if (s1 lt s2) S.o.p("s1 lt s2")
- String s1, s2 if (s1.compareTo(s2) ) S.o.p ("s1
lt s2") - String s1, s2 if (compareTo(s1, s2)) S.o.p("s1 lt
s2") - String s1, s2 if (s1.compareTo(s2) lt 0)
S.o.p("s1 lt s2") - String s1, s2 int x 0
- if (x s1.compareTo(s2) )
S.o.p("s1 lt s2")