wszyscy myślą, że to dno. ale na dnie tak nie wieje...

This is accomplished by stating the name of the object handle, followed Chapter 2: Everything is an Object
77
by a period (dot), followed by the name of the member inside the object (objectHandle.member). For example:
d.i = 47;
d.f = 1.1f;
d.b = false;
It is also possible that your object might contain other objects that contain data you’d like to modify. For this, you just keep “connecting the dots.” For example:
myPlane.leftTank.capacity = 100;
The DataOnly class cannot do much of anything except hold data, because it has no member functions (methods). To understand how those work, you must first understand arguments and return values, which will be described shortly.
Default values for primitive members
When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it:
Primitive type
Default
Boolean
false
Char
‘\u0000’ (null)
byte
(byte)0
short
(short)0
int
0
long
0L
float
0.0f
double
0.0d
Note carefully that the default values are what Java guarantees when the variable is used as a member of a class. This ensures that member variables of primitive types will always be initialized (something C++ doesn’t do), reducing a source of bugs.
However, this guarantee doesn’t apply to “local” variables – those that are not fields of a class. Thus, if within a function definition you have:
int x;
Then x will get some random value (as in C and C++); it will not automatically be initialized to zero. You are responsible for assigning an appropriate value before you use x. If you forget, Java definitely improves on C++: you get a compile-time error telling you the variable might not have been initialized. (Many C++ compilers will warn you about uninitialized variables, but in Java these are errors.)
Methods, arguments
and return values
Up until now, the term function has been used to describe a named subroutine. The term that is more commonly used in Java is method, as in “a way to do something.” If you want, you 78
Thinking in Java
www.BruceEckel.com
can continue thinking in terms of functions. It’s really only a syntactic difference, but from now on “method” will be used in this book rather than “function.”
Methods in Java determine the messages an object can receive. In this section you will learn how simple it is to define a method.
The fundamental parts of a method are the name, the arguments, the return type, and the body. Here is the basic form:
returnType methodName( /* argument list */ ) {
/* Method body */
}
The return type is the type of the value that pops out of the method after you call it. The method name, as you might imagine, identifies the method. The argument list gives the types and names for the information you want to pass into the method.
Methods in Java can be created only as part of a class. A method can be called only for an object,3 and that object must be able to perform that method call. If you try to call the wrong method for an object, you’ll get an error message at compile time. You call a method for an object by naming the object followed by a period (dot), followed by the name of the method and its argument list, like this: objectName.methodName(arg1, arg2, arg3). For example, suppose you have a method f( ) that takes no arguments and returns a value of type int. Then, if you have an object called a for which f( ) can be called, you can say this: int x = a.f();
The type of the return value must be compatible with the type of x.
This act of calling a method is commonly referred to as sending a message to an object. In the above example, the message is f( ) and the object is a. Object-oriented programming is often summarized as simply “sending messages to objects.”
The argument list
The method argument list specifies what information you pass into the method. As you might guess, this information – like everything else in Java – takes the form of objects. So, what you must specify in the argument list are the types of the objects to pass in and the name to use for each one. As in any situation in Java where you seem to be handing objects around, you are actually passing handles.4 The type of the handle must be correct, however.
If the argument is supposed to be a String, what you pass in must be a string.
Consider a method that takes a string as its argument. Here is the definition, which must be placed within a class definition for it to compile:
int storage(String s) {
return s.length() * 2;
}