12.07.2015 Views

OsmWv

OsmWv

OsmWv

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

arguments and return valuesQ:What happens Ifthe argument you want topass Isan object Instead of II primitive?A: You'll learn more about this In later chapters,but you already know the answer. Java passeseverything by value.EverythIng. But...value meansbits Inside the vcrtable. And remem ber,you don'tstuff objects Into variables; the variable Is a remotecontrol-a reference to an object. So If you pass areference to an object into a method, you're passinga copy ofthe remote control. Stay tuned, though, we'llhave lots more to sayabout this.Reminder: Javacares about type!You can't return a Giraffe whenthe return type Is declaredas a Rabbit. Same thing withparameters. You can't pass aGiraffe Into a method thattakes a Rabbit.Q..: Can a method declare multiple return values?Or Isthere some way to return more than onevalue?A: Sort of. A method can declare onIy one returnvalue. BUT... If you want to return, say, three int values,then the declared return type can be an Int orray.Stuff those lnts into the array,and passIt on back. It'sa little more involved to return multiple values withdifferent types; we'll be talking about that in a laterchapter when ~e talk about ArrayLlst.'Q :Do Ihave to return the exact type , declared?A.: You can return anything that can be implicitlypromoted to that type. So, you can pass a byte wherean Int Is expected .The caller won't care,because thebyte fits Just fine Into the tnt the caller will use forassigning the result. You must use an explicit castwhen the declared type Is smaller than what you'retrying to return.Q..: Do Ihave to do something with the returnvalue of a method? can IJust Ignore it?A:Java doesn't require you to acknowledge areturn value. You might want to call a method witha non-void return type, even though you don't careabout the return value. In this case, you're callingthe meth od for the work it does inside the method,rather than for what the method gives returns . InJava, you don't have to assign or use the return value.• Classes define what an object knows and whal anobject does.• Things an object knows are itsInstance variables(state).• Things an object does are its methods (behavior).• Methods can use instance variables so that objectsof the same type can behave differently.• Amethod can have parameters, which means youcan pass one ormore values in to the method.• The number and type ofvalues you pass inmustmatch the order and type of the parametersdeclared bythe method.• Values passed inand out ofmethods can beimplicitly promoted to a larger Iype orexplicitly casttoa smaller type.• The value you pass as an argument toa methodcan be a literal value (2, 'c', etc.) ora variable ofthe declared parameter type (for example, xwherexis an intvariable) . (There are other things youcan pass as arguments, but we're not there yet.)• Amethod must declare a retum type.Avoid retumtype means the method doesn't return anything.• Ifa method declares a non-void return type, it mustreturn a value compatible with the declared returntype.78 chapter 4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!