Static, main, math, Strings - PowerPoint PPT Presentation

About This Presentation
Title:

Static, main, math, Strings

Description:

Static, main, math, Strings – PowerPoint PPT presentation

Number of Views:115
Avg rating:3.0/5.0
Slides: 17
Provided by: LynnL151
Category:
Tags: java | main | math | spring | static | strings

less

Transcript and Presenter's Notes

Title: Static, main, math, Strings


1
Static, main, math, Strings
2
What helps me learn Java the most
  1. Going to the Mac lab
  2. Writing Java on the Board
  3. Doing the Homework
  4. Watching the Instructor code in BlueJ
  5. Doing the Programming Assignments
  6. clickers

3
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

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)

5
static
  • 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
6
Using 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

7
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

8
Why have static?
  • If instance variables are not needed
  • Math.PI, Math.sqrt, JOptionPane.showMessageDialog
  • when in doubt, do NOT use static

9
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

10
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

11
program with main
  • public class MyClass
  • // do stuff with methods and constructors
  • public static void main(String args)
  • new MyClass( ) // call constructor

12
String 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)
  • /

14
Wrapper 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
15
this 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

16
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
  • 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")
Write a Comment
User Comments (0)
About PowerShow.com