Java Review Session - PowerPoint PPT Presentation

About This Presentation
Title:

Java Review Session

Description:

... fine detail, so for more info check out Java in a Nutshell, which is available ... Bytecode and the JVM run on most platforms. Security. JVM can restrict ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 38
Provided by: matthe51
Category:

less

Transcript and Presenter's Notes

Title: Java Review Session


1
Java Review Session
  • CS 402
  • Melissa Carroll
  • Berk Kapicioglu
  • Most slides by Matt Hibbs

2
First Off
  • Slides available on course website
  • This presentation doesnt get into very fine
    detail, so for more info check out Java in a
    Nutshell, which is available online to Princeton
    students for free. You can search/find it at
  • catalog.princeton.edu

3
Why Java?
  • Cross Platform
  • Bytecode and the JVM run on most platforms
  • Security
  • JVM can restrict access to local machine
  • Safety
  • Garbage Collection (fewer memory leaks)
  • Ease of Coding
  • Many available packages

4
Java Basics
  • http//java.sun.com (SDK)
  • Development Environments
  • Eclipse
  • Emacs
  • Notepad and Command Line

5
Hello World
  • In C
  • int main (int argc, char argv)
  • printf(Hello World!\n)
  • return 0
  • / end main /
  • In Java
  • public class HelloWorld
  • public static void main (String args)
  • System.out.println(Hello World!)
  • / end main /
  • //end HelloWorld
  • Things to notice
  • Similar syntax
  • Classes
  • System.out.println()
  • main is void public static void main

6
How Java Differs from C
  • Exclusively Object-Oriented Language
  • EVERYTHING must live in a class (mostly)
  • No Global Variables
  • No Pointers
  • Also, no -gt or operators
  • Blessing and a Curse
  • Garbage Collection
  • Loss of Power

7
How Java Differs from C
  • No Preprocessor (no include, define, etc.)
  • No goto statement
  • Declare/Define Variables Methods anywhere
    (within a class)
  • No struct, enum, or typedef
  • Cant overload Operators
  • Use new rather than malloc()

8
Java Data Types
  • Primitives
  • boolean, char, byte, short, int, long, float,
    double
  • char is Unicode (16 bits)
  • boolean is true/false (not 1/0)
  • int i 1
  • if (i) //BAD
  • if (i 1) //GOOD

9
Java Data Types
  • Primitives
  • Type conversions
  • Cant convert boolean
  • boolean b false
  • int i b //BAD
  • int j b ? 1 0 //GOOD
  • Converting up is automatic
  • Converting down requires a cast
  • double f 37.5 // it used to be float but //
    gave compiler error
  • int i f //BAD
  • int j (int) f //GOOD (truncates)

10
Java Data Types
  • References (by reference vs. by value)
  • Everything thats not primitive (classes and
    arrays)
  • Think of these as hidden pointers
  • Foo a new Foo()
  • Foo b a
  • a and b now reference the same object

11
Java Arrays
  • Mostly what you would expect
  • String textLines
  • String textLines2 new String15
  • textLines23 Hi, how are you today?
  • int fib 1, 1, 2, 3, 5, 8, 13, 21
  • System.out.print(fib5) // 8
  • textLines is just a Reference, no array was
    actually made
  • textLines2 is an un-initialized array
  • fib is an initialized array

12
Java Development
13
Java Platform
  • Lots of built-in objects and functions for
    various purposes
  • Graphics (java.awt)
  • Math Functions (java.math)
  • Networking (java.net)
  • Databases (java.sql)
  • Well mostly use the platform for data structures
    (java.util) and I/O (java.io)

14
Java Packages
  • Java organizes classes into larger groups called
    packages
  • Both the Java Platform and any classes that you
    write are organized into packages
  • For code that you write, dont worry so much
    about what package youre in (the default is fine)

15
Using the Java Platform
  • http//java.sun.com/j2se/1.4.2/docs/api/index.html
  • You can always use the full package path name to
    access classes
  • java.io.FileReader fr new java.io.FileReader
    (test.txt)
  • But thats pretty annoying, so you can use import
    statements at the beginning of your code to avoid
    this
  • import java.io.FileReader
  • //...
  • FileReader fr new FileReader(test.txt)
  • //...

16
Object Orientation
17
Java - Classes
  • Similar to C classes in many ways
  • Heres a simple Java class that well break down
  • public class Circle
  • private float radius
  • public static final double PI 3.14159
  • public Circle(float r)
  • radius r
  • public float area()
  • return PI radius radius

18
Java - Classes
  • Naming
  • public class Circle
  • The basic form is
  • ltmodifiersgt class ltnamegt
  • Typically need an access modifier
  • public, protected, private
  • Can have additional modifiers
  • static, final, abstract
  • If a Class is public, it MUST be the only public
    class in its file, and this file MUST be called
    ltnamegt.java

19
Java Access Modifiers
  • Access modifiers control who has access to a
    variable or class
  • public anything can access it
  • protected only classes in same package can
    access (not important for 402)
  • private only accessible within the class that
    defines it

20
Java - Classes
  • Instance Field
  • private float radius
  • Any variable declared in a class is a field
  • Typically have an access modifier
  • In this case the field is uninitialized, so it
    will get the default value (0).

21
Java - Classes
  • Class fields and methods
  • Use static modifier
  • Are instance-independent
  • Refer to using class name e.g. Circle.PI
  • No need to include class name if using within
    class
  • If only used static methods, would not be OO

22
Java - Classes
  • Constant class field
  • public static final double PI 3.14159
  • Again, typically have an access modifier
  • If you want to have a constant variable, also use
    the static and final modifiers
  • static means that there is only one PI no matter
    how many instances of Circle we make
  • final means that PI cannot be changed

23
Java - Classes
  • Constructors
  • public Circle(float r) radius r
  • This is what gets called when an instance of your
    class is created
  • Typically used to just initialize fields
  • If you dont write one, you get a default
    constructor for free, which does nothing
  • Refer to other constructors with this()

Name of the class
24
Java - Classes
  • Methods
  • public float area()
  • return PI radius radius
  • Again, need an access modifier
  • Need a return type (void is a valid choice)
  • Can optionally pass in arguments

25
Java - Classes
  • Heres the whole thing again
  • public class Circle
  • private float radius
  • public static final double PI 3.14159
  • public Circle(float r)
  • radius r
  • public float area()
  • return PI radius radius

26
Java - Objects
  • So, weve made a class, now what?
  • We can start making circles
  • public Circle c1
  • public Circle c2 new Circle()
  • public Circle c3 new Circle(2.5)
  • c1 didnt actually make a circle, it just made a
    circle reference
  • c2 used the default constructor, and has radius 0
  • c3 used our constructor, and has radius 2.5

27
Java - Objects
  • Now that we have circles, we can call methods on
    them
  • public float f c3.area()
  • System.out.print(f) //19.6349

28
Java - Interfaces
  • Interfaces are one of the ways that Java allows
    for Inheritance (the other is with subclasses)
  • For example, we could make an interface called
    Shape that our Circle class can implement
  • public interface Shape
  • public float area ()
  • // end Shape
  • public class Circle implements Shape
  • //...
  • //end Circle
  • Now, since we know the Circle implements Shape,
    we know that Circle must contain an area() method
  • Classes can implement one interface, no
    interfaces, or multiple interfaces (many do)

29
Java Reference Oddities
  • Since we only deal with references to objects,
    some behavior may seem odd
  • Circle hulaHoop new Circle (3)
  • Circle CD hulaHoop
  • CD.radius 1 //we cant actually do this (lets
    pretend)
  • System.out.print(hulaHoop.radius) // 1

30
Packages to Look At
  • Have a look at java.util, especially
    java.util.HashMap and java.util.LinkedList
  • Both are what they sound like and have fairly
    straightforward functions
  • These may or may not be useful in upcoming
    assignments

31
What to Focus on for 402
  • Do focus on
  • Basic syntax like control flow, declarations
  • Syntax for declaring classes and methods,
    instantiating objects
  • Writing output to stdout, stderr (for debugging)
  • Basic OO like interfaces
  • Java.util
  • Dont worry so much about
  • Reading input from stdin and files
  • Misc packages like java.security

32
Example Demo
33
Suggestions
  • Play around with Java
  • Find an IDE your comfortable with, and just write
    some programs to get a feel for it.
  • Use the Java API specification at
    http//java.sun.com/j2se/1.4.2/docs/api/index.html
  • If you have any problems, e-mail Berk
    (bkapicio_at_cs) or Melissa (mkc_at_cs) or feel free to
    ask your friends and/or classmates for help

34
Suggested Programs
  • A rcenet rsesercah sduty fnuod taht it deosnt
    mttaer in waht oredr the ltteers of a wrod are,
    so lnog as the frist and lsat letetrs are in the
    rgiht pclae. The rset can be a toatl mses and you
    can siltl raed it whtuoit a porbelm.

35
Suggested Programs
  • A recent research study found that it doesnt
    matter in what order the letters of a word are,
    so long as the first and last letters are in the
    right place. The rest can be a total mess and you
    can still read it without a problem.
  • As a toy, write a program that garbles text like
    this it would get you familiar with I/O and
    text manipulation.

36
Suggested Programs
  • Using the built in HashMap, write a tester as
    though you had written this class yourself
    (possibly in CS 226)
  • Maybe you could help out the TAs by writing a
    program that uses student names as a key and maps
    them to student ID numbers (You dont really need
    to do this, we can just look it up)

37
Questions?
Write a Comment
User Comments (0)
About PowerShow.com