Download presentation
Presentation is loading. Please wait.
Published byDennis Harrington Modified over 8 years ago
1
2-1 Types of data Java has two types of data primitive types that store one value char boolean byte int long float double reference types to store many values with one name any of the hundreds of classes that come with Java String BufferedReader URLAddress JButton String BufferedReader URLAddress JButton any domain types for specific applications BankAccount Student Jukebox CD BankAccount Student Jukebox CD Classes that provide utility in a wide variety of applications Scanner String arrays JFrame Scanner String arrays JFrame
2
2-2 What's the difference Primitive type variables that store the actual value of the variable int n = 0; // n stores 0 int n = 0; // n stores 0 Reference type variables store a special value for finding the object at runtime the object is a bunch of bits on the heap (special part of memory that gets allocated at runtime)
3
2-3 Methods Abstraction: allows programmer to concentrate on essentials how does a String's substring? How does System.out.println send output to the console? how does a JButton know it's been clicked? What algorithm does the Math.sqrt method use? However, programmers often create new methods We will need to work out the details and know who to write the methods
4
2-4 Methods A Java class typically contains many methods, each of which has a method heading Method headings specify the number and types of arguments required There are two major components to a method: the method heading the block (a set of curly braces with the code that completes the method's responsibility)
5
2-5 Method Headings Sun uses method headings (and other things) to document methods so we know how to use them public String substring( int beginIndex, int endIndex ) Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex-1. Thus the length of the substring is endIndex-beginIndex. Examples: "hamburger".substring(4, 8) returns "urge" "smiles".substring(1, 5) returns "mile" Parameters: beginIndex - the beginning index, inclusive. endIndex - the ending index, exclusive. Returns: the specified substring.
6
2-6 What method headings tell us Method headings provide the information needed to use it they show us how to send a message to instances of the class public String substring(int beginIndex, int endIndex) public String substring(int beginIndex, int endIndex) What does the method evaluate to? String What is the method name? substring How many arguments are required? 2 What type of arguments are required? int Example message String fullName = "Murphy, John"; String fullName = "Murphy, John"; String lastName = fullName.substring( 0, 6 ); String lastName = fullName.substring( 0, 6 );
7
2-7 Parameters = Arguments The substring method requires two arguments in order to specify the portion of the string to return. When the message is sent, the argument 0 is assigned to the parameter named beginIndex and the argument 6 to the parameter name endIndex fullName.substring( 0, 6 ); fullName.substring( 0, 6 ); public String substring( int beginIndex, int endIndex ) public String substring( int beginIndex, int endIndex ) Implementation of the method is not shown here Implementation of the method is not shown here
8
2-8 Arguments / Parameters When a message is sent The first argument is assigned to the first parameter, second argument gets assigned to the second parameter,... If you do not supply the correct number and type of arguments, you get compiletime errors Examples of errors fullName.substring( "wrong type" ); fullName.substring( "wrong type" ); fullName.substring( 0, 6, fullName.length() ); fullName.substring( 0, 6, fullName.length() ); fullName.substring( ); fullName.substring( ); fullName.substring( 0.0, 6.0 ); fullName.substring( 0.0, 6.0 );
9
2-9 Method Headings General form of a Java method heading public return-type method-name ( parameter-1, parameter-2, parameter-n,... ) parameter-2, parameter-n,... ) public says a method is known where objects are constructed return-type may be any primitive type, any class, or void A void method returns nothing therefore, a void method can not be assigned to anything also, you can not place a call to a void method in println parameters, which are optional, specify the number and type of arguments required in the message
10
2-10 Parameters General form of a parameter between ( and ) in method headings class-name identifier class-name identifier-or- primitive-type identifier primitive-type identifier Example method headings: public int length( ) // class String public int length( ) // class String public int equals( String anotherString ) // class String public int equals( String anotherString ) // class String public void withdraw( double amount ) // class BankAccount public void withdraw( double amount ) // class BankAccount public double readDouble( ) // class TextReader public double readDouble( ) // class TextReader public void move( int spaces ) // class Grid public void move( int spaces ) // class Grid
11
2-11 The Method Body The method body is Java code enclosed within a block { } Curly braces contain the same things you saw in main variable declarations and initializations: int creditOne = 0; input and output messages: readInt, System.out.println construct objects: Grid g= new Grid(8,6,1,1,Grid.EAST) messages: g.turnLeft() Method bodies have access to the state of the object The "state" of an object is implemented as variables declared inside the class rather than a method body.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.