Slides0128 - -Example: bar = stdin.nextInt(); Overloading...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Slides 1/28 COP 3330
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Topics Classes and methods
Background image of page 2
Garbage Collection In Java, when an object is no longer referenced, its memory is reclaimed automatically This is a big difference from C
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Methods Non-static methods act on objects A method is laid out as follows: modifiers return-type identifier ( parameters ) { body } A method is essentially the same thing as a function in C
Background image of page 4
Methods The return type of a method can be any type Inside the body of a method, you MUST always reach a return statement that returns a value of the same type as the return type If the method does not return anything, the return type is void , in which case it doesn't need to have a return statement
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Methods Variables accessible from within a method - Instance variables - Formal parameters - Local variables
Background image of page 6
Methods Use object . methodName ( parameters ) to call the method on an object
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: -Example: bar = stdin.nextInt(); Overloading Multiple methods with the same name can exist in the same class The formal parameters MUST be different Why do this?-Use different types of information to construct an object (e.g. Scanner constructors)-Perform similar operations (e.g. print() method in PrintStream) Static Ordinarily, methods and fields are associated with objects The static modifier makes them associate with the class Example-See the class Math in java.lang Static To access a static member, use the class name instead of the object name-Example: foo = Integer.parseInt(5); Packages Packages group related files Put a package statement at the top of the source file Put the files in directory with a name matching the package...
View Full Document

Page1 / 11

Slides0128 - -Example: bar = stdin.nextInt(); Overloading...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online