Static and main
Download
Report
Transcript Static and main
Static, main, math, Strings
What helps me learn Java the
most:
A.
B.
C.
D.
E.
F.
Going to the Mac lab
Writing Java on the Board
Doing the Homework
Watching the Instructor code in BlueJ
Doing the Programming Assignments
clickers
Review 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
To call sqrt, do the following:
A. int x = Math.sqrt(4);
B. int x = sqrt(4);
C. Math.sqrt(4);
D. int x = new Math( ); x.Math.sqrt(4);
E. int x = new Math( ); x.sqrt(4);
static
static means "class level", not "object
level"
public int x; // note: public just for demo
// instance variables should be private
exists at class level
public static int y;
before any objects are
object is created;
on object bench
created
Using static variables/methods
For 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
More 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
Why have static?
If instance variables are not needed
Math.PI, Math.sqrt,
JOptionPane.showMessageDialog
when in doubt, do NOT use static
static 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
main
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
program with main
public class MyClass
{
// do stuff with methods and constructors
public static void main(String[ ] args)
{
new MyClass( ); // call constructor
}
String[ ] args
You can specify arguments at the
command line
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 i=0;i<args.length;i++)
System.out.println("You entered " + args[i]);
*/
}
Wrapper Classes
Sometimes, primitives
need to act like
objects and the
reverse
Each primitive has a
"wrapper" class that
makes it look like an
object:
Wrappers are
instances of Classes,
so are capital
Wrapper
Type
Boolean
Primitive
Type
boolean
Integer
int
Double
double
Character
char
primitives
are all
lowercase
this: refers to the class
or class's instance variable
public class Member
{
private String name;
private String motto;
private<Member> friends = new ArrayList<Member> ( );
public void addMyBestFriend( )
{
// want to say:
// friends.add(me);
friends.add(this); // will add the current Member as a friend
}
}
More 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:
A.
String s1, s2; if (s1 < s2) S.o.p("s1 < s2");
B.
String s1, s2; if (s1.compareTo(s2) ) S.o.p ("s1 < s2");
C.
String s1, s2; if (compareTo(s1, s2)) S.o.p("s1 < s2");
D.
String s1, s2; if (s1.compareTo(s2) < 0) S.o.p("s1 < s2");
E.
String s1, s2; int x = 0;
if (x = s1.compareTo(s2) ) S.o.p("s1 < s2");