chapter 2 types variables methods pages 34 43 horstmann l.
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 2 types, variables, methods Pages 34-43 Horstmann PowerPoint Presentation
Download Presentation
Chapter 2 types, variables, methods Pages 34-43 Horstmann

Loading in 2 Seconds...

play fullscreen
1 / 33

Chapter 2 types, variables, methods Pages 34-43 Horstmann - PowerPoint PPT Presentation

  • Uploaded on

Chapter 2 types, variables, methods Pages 34-43 Horstmann. Types and Variables. Every value has a type Variable declaration examples: Variables Store values Can be used in place of the objects they store.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

Chapter 2 types, variables, methods Pages 34-43 Horstmann

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
types and variables
Types and Variables
  • Every value has a type
  • Variable declaration examples:
  • Variables
    • Store values
    • Can be used in place of the objects they store

String greeting = "Hello, World!";PrintStream printer = System.out;int luckyNumber = 13;

int diameter = 20;

Ball A;

‘out’ is a variable in the System class, that holds inside it an object from the PrintStream class.

Objects in PrintStream class contain methods print and println .


Variables are like shaped boxes….

Integer numbers go in integer-shaped variables (labelled int)

Real numbers go in real-number shaped ones (float, double)

Strings (“hello”) can go in string-shaped variables (String)

Variables must be set up ("declared") before use

Each variable is of a certain datatype (int, float, double, String) and each variable has a name (an identifier).

declaring variables
Declaring variables…….

<datatype> <variable name>;

int x; // a box called x that will hold an integer

double radius; /* a box called radius that will hold

a floating point no. */

char ch; // ch can hold a single character

String myName; // myName holds a string of characters

Int, double, float, char all begin with a lower case letter. String begins with a capital letter.

  • Identifier: name of a variable, method, or class
  • Rules for identifiers in Java:
    • Can be made up of letters, digits, and the underscore (_) character
    • Cannot start with a digit
    • Cannot use other symbols such as ? or %
    • Spaces are not permitted inside identifiers
    • You cannot use reserved words
    • They are case sensitive


  • By convention, variable names start with a lowercase letter
  • By convention, class names start with an uppercase letter
self check
Self Check
  • What is the type of the values 0 and "0"?
  • Which of the following are legal identifiers?
  • Define a variable to hold your name.

Greeting1gvoid101dalmatiansHello, World<greeting>

  • intand String
  • Only the first two are legal identifiers

String myName = "John Q. Public";

the assignment operator
The Assignment Operator
  • Assignment operator is =
  • Not used as a statement about equality
  • Used to change the value of a variableint luckyNumber = 13; luckyNumber = 12;

Figure 1:Assigning a New Value to a Variable

uninitialized variables
Uninitialized Variables
  • Error:

int luckyNumber;System.out.println(luckyNumber);   // ERROR - uninitialized variable

Figure 2:An Uninitialized Object Variable

shortcut declare and initialize
Shortcut: declare and initialize

Initialization: giving a variable a value for the first time

We can declare and initialize in the same line (good practice!)

int x = 20;

double d = 3.14;

String myName = “Fintan”;

Variables must be declared before usage!

myName = “Fintan”;

String myName; // NOPE!

self check12
Self Check
  • Is 12 = 12 a valid expression in the Java language?
  • How do you change the value of the greeting variable to "Hello, Nina!"?
  • No, the left-hand side of the = operator must be a variable
  • Note thatis not the right answer–that statement defines a new variable called greeting

greeting = "Hello, Nina!";

String greeting = "Hello, Nina!";

objects and classes
Objects and Classes
  • Object: entity that you can manipulate in your programs (by calling methods)
  • Each object belongs to a class. For example, System.out belongs to the class PrintStream

We can look up the System class in the Java API at

(click on “api specifications”)

To find out what it contains.

Figure 3:Representation of the System.out object

  • Method: Sequence of instructions that accesses the data of an object
  • You manipulate objects by calling its methods
  • Class: Set of objects with the same behavior
  • Class determines legal methods

Objects in the Ball class have methods





  • Public Interface: Specifies what you can do with the objects of a class
  • If a method in an object is declared ‘public’, we can use that method for that object.
  • If a method is declared ‘private’, we cannot use that method.

Ball class methods were declared public, so they

could be used by the BouncingBallApplet class for


representation of two string objects
Representation of Two String Objects

We can look up the String class in the Java API at

( “api specifications”) To see what it contains.

Figure 4:A Representation of Two String Objects

Strings objects are members of the class String. They have various different useful methods.

string methods
String Methods
  • length: counts the number of characters in a string

String greeting = "Hello, World!"; int n = greeting.length(); // sets n to 13


string methods19
String Methods
  • toUpperCase: creates another String object that contains the characters of the original string, with lowercase letters converted to uppercase

String river = "Mississippi"; String bigRiver = river.toUpperCase(); // sets bigRiver to "MISSISSIPPI"

We will see length() and toUpperCase() and other methods in the Java API entry for String ( “api specifications”)


string methods20
String Methods
  • When applying a method to an object, make sure method is defined in the appropriate class

System.out.length(); //This method call is an error

self check21
Self Check
  • How can you compute the length of the string “mississippi"?
  • How can you print out the uppercase version of "Hello, World!"?
  • Is the following legal:

String river = “mississippi”;


Why or why not?

  • It is not legal. The variable river has type String. The println method is not a method of the String class.

river.length() or “mississippi".length()

String greeting = “Hello World”;


implicit and explicit parameters
Implicit and Explicit Parameters
  • Parameter (explicit parameter): Input to a method. Not all methods have explicit parameters.
  • Implicit parameter: The object on which a method is invoked


// greeting is an explicit parameter to printlnn =greeting.length() // length has no explicit parameter

n = greeting.length();



implicit and explicit parameters24
Implicit and Explicit Parameters

Figure 5:Passing a parameter to the println method

return values
Return Values
  • Return value: A result that the method has computed for use by the code that called it

int n = greeting.length(); // return value stored in n

The Java API at

(click on “api specifications”)

Tells us what sort of thing is returned from a given method.


passing return values
Passing Return Values
  • You can also use the return value as a parameter of another method:
  • Not all methods return values. Example:




a more complex call
A More Complex Call
  • replace method carries out a search-and- replace operation
  • As Figure 8 shows, this method call has
    • one implicit parameter: the string "Mississippi"
    • two explicit parameters: the strings "issipp"and "our"
    • a return value: the string "Missouri"

river.replace("issipp", "our")

// constructs a new string ("Missouri")


a more complex call28
A More Complex Call

Figure 8:Calling the replace Method

method definitions
Method Definitions
  • Method definition specifies types of explicit parameters and return value
  • Type of implicit parameter = current class; not mentioned in method definition


method definitions30
Method Definitions
  • Example: Class String defines

public int length() // return type: int // no explicit parameter

public String replace(String target, String replacement) // return type: String; // two explicit parameters of type String

method definitions31
Method Definitions
  • If method returns no value, the return type is declared as void
  • A method name is overloaded if a class has more than one method with the same name (but different parameter types)

public void println(String output) // in class PrintStream

Most methods in the Ball class had a return type of void:

public void move()

public void println(String output)public void println(int output)

self check32
Self Check
  • What are the implicit parameters, explicit parameters, and return values in the method call river.length()?
  • What is the result of the call river.replace("p", "s")?
  • What is the result of the call greeting.replace("World","Dave").length()?
  • How is the toUpperCase method defined in the String class?
  • The implicit parameter is river. There is no explicit parameter. The return value is 11
  • "Missississi"
  • 12
  • As public String toUpperCase(), with no explicit parameter and return type String.