Java - University of Technology - 9

How are operations on array elements of type Cow different from operations on a variable of type Cow? We use the (.) operator as usual, but since array elements do not have variable names, we use array element notation instead. For example, with the Cow class defined as in Figure 4.8, we use array references to operate on the Cow array elements as in Figure 4.9.



Figure 4.8: Cow.java

Maybe you are interested!


@ (% A

Java - University of Technology - 9

"EF"

@ ( (2 # ( @ ( &

ABC2-2 DEE28743

%,FF '28' G HHH

F2'$ I' %234"' J2K8 72"'8 '28' G HHH

J2L8 '28' G HHH

J2K8 '28' G HHH

(27 &

(27 # 8K P 8&


@ (EF @ ( # ( @ (E5F&

@ ( E3F # ( @ ( &

@ ( E2F # ( @ ( &

@ ( E1F # (2&


@ ( E3F7 # 8A 8&

@ ( E2F7 # 8K 8&


7 7 8 ($ 8 &

7 7 @ ( E1F7 &


# 3&

( - @ ( 7 "

@ ( E F7 &

# )2&


K* LMM

2 . N % *


Figure 4.9: CowArrayTest.java


We often use for loops to iterate over the elements of an array. For example, the code that iterates over the array myCows and prints the names of each cow in it could be written as follows:

In addition to the common syntax above, the for loop also has a shorter form, the for-each loop. For example, we can rewrite the above for loop as follows:


In it, we declare the running variable aCow to be a variable of type Cow, the running variable will run from the beginning to the end of the array myCows, each time taking a value equal to the value of the current element in the array (in this example, that value is a reference to a Cow object).

The for-each loop can be applied to arrays of reference data types as well as primitive types, and can also be used for the collection structures of the Java library, which we will discuss in Chapter 13.

Exercise

1. Fill in the blanks in each of the following sentences with the appropriate word:

a) Instance variables of types char, byte, short, int, long, float, and double all have default values ​​of.

b) The boolean entity variable has a default value of.

c) Data types in Java are divided into two categories: types and non-types..

2. Are the following statements true or false?

a) It is possible to call a method from a base type variable.

b) The created objects exist in heap memory until the program ends.

c) Any time an object is available, it must have a reference to it.

d) Floating-point values ​​in source code are by default understood as direct floating-point values ​​of type float.

3. What are entity variables used for?

4. The parameter of the main() method is a String array. This array is a list of command line parameters when we run the program. For example, when we run the command java CowArrayDemo foo bar from the command prompt. The array args[] will contain the character strings foo and bar. Write a program to print all the command line parameters received.

5. Find and fix errors in the following programs (each part is a complete source code file).

a)


b)



6. Given the following program, list the HeapQuiz objects created; ask up to the paragraph

//do stuff then array elements hq[0] to hq[4] refer to which objects.


7. Dan asked Ti and Suu to help him quickly write a code to process phone contacts for mobile phones. Whoever came up with the best solution would be paid a bag of popcorn. After listening to Dan's description, Suu wrote the following code on the board:


Ti looked over and laughed, "Your cell phone has such a small memory, but you're so wasteful!"

Speaking of which, Ti wrote:


After finishing writing, Ti happily said, "The popcorn is mine!". Dan smiled, "It saves more memory, but you have to eat with Suu."

Why did Dan decide like that?

Chapter 5. Object Behavior


State influences behavior, and behavior influences state. We have seen that objects have state and behavior, which are represented by instance variables and methods. We have also seen that each instance of a class (each object of a class) has its own values ​​for instance variables. For example, this Cow object has the name "Lady" and weighs 80 kg, while another Cow object is named "Daisy" and weighs 150 kg. Do the two objects implement the moo() method differently? Maybe, because each object has a behavior that depends on its state. In other words, the method called by an object will use the values ​​of the instance variables of that object. This chapter will examine this interrelationship.


5.1. OBJECT METHODS AND STATES


Recall that a class is a template for creating objects of that class. When we write a class, we describe how to construct an object of that class. We know that the values ​​of the same instance variables can be different for different objects. But what about the methods? Do they behave differently? Well, sort of. Each instance of a class has the same methods, but these methods can behave differently depending on the specific values ​​of the instance variables.

For example, the PhoneBookEntry class has two instance variables, name and phone. The display() method displays the contents of the PhoneBookEntry object, specifically the values ​​of its name and phone. Different objects have different values ​​for those two variables, so the contents displayed by display() for those objects are also different.

tom.display() Name: Tom the Cat Phone: 84208594


jerry.display() Name: Jerry the Mouse Phone: 98768065

Looking back at the example in Figure 3.3, we will see that the display() method calls from tom and jerry show different results on the screen, even though the source code for display() for tom and jerry is the same:

In fact, the above content of display() is equivalent to writing it like this:


In this, the keyword this means a special reference to the owner of the current method. For example, for the call tom.display(), this has the value of the tom reference; for the call jerry.display(), this has the value of jerry. It can be said that when calling a method on an object, the reference to that object is passed into the method via an implicit parameter: the this reference.

The this reference can be used to access an instance variable or call a method on the current object. Typically, this is useful only when the instance variable name is the same as a local variable or method parameter. For example, suppose the setName() method of the PhoneBookEntry class takes a name parameter of type String that has the same name as the name instance variable of the class. From within the setName() method, if the name 'name' is used, the compiler will understand that we are talking about the name parameter. To call the name instance variable, the only way is to use the this reference to call it explicitly. For example:


5.2. PASSING PARAMETERS AND RETURN VALUES


Just like in other programming languages, we can pass values ​​into methods. For example, we want to instruct a Cow object how many times it should roar by calling the method like this:

c.moo(3);

We call an argument what we pass into a method. In Java, an argument is a value, such as 3 in the call above, or "Hello" as in System.out.println("Hello"), or the value of a reference to a Cow object. When the method call is executed, that argument value is copied into a parameter. A parameter is essentially just a local variable to the method.

– a variable has a name and a data type, it can be used inside the body of a method.



0.5 1 2 3 4 / IQ

= ! # 2 3 @

@o( c # ( @o(();

======



o moo( ) ( ( , 3)

0O5 ! # P2

GCK


0 5 GCK

mo . (8Noo...8);

# Q 2;

( L # 2 3

2 3 4


Figure 5.1: Arguments and parameters.


Important thing to remember: If a method requires a parameter, we must pass it a value, and that value must be of the parameter's declared type.

A method can have multiple parameters. When declaring them, we use commas to separate them. And when calling a function, we must pass the arguments of the correct data type and in the order they were declared.


Figure 5.2: A method can have multiple parameters.


Methods can return values. Each method is declared with a return type, but so far our example methods have used the return type void, meaning they don't return anything.

void doSomething() {

}

We can declare a method to return a specific type of value to its caller, for example:

int giveSecret() { return 3;

}

A declared method must return a value of the data type it declares to return. (Or a value of a type compatible with the declared type. We will discuss this in detail when we talk about polymorphism in Chapter 5.)

Comment


Agree Privacy Policy *