[ Pobierz całość w formacie PDF ]
.In thischapter we ll see the basic components of a Java program and we ll learnthat everything in Java is an object, even a Java program.You manipulate objectswith referencesEach programming language has its own means of manipulating data.Sometimes the programmer must be constantly aware of what type ofmanipulation is going on.Are you manipulating the object directly, or areyou dealing with some kind of indirect representation (a pointer in C orC++) that must be treated with a special syntax?101All this is simplified in Java.You treat everything as an object, so there isa single consistent syntax that you use everywhere.Although you treateverything as an object, the identifier you manipulate is actually a reference to an object1.You might imagine this scene as a television (theobject) with your remote control (the reference).As long as you re holdingthis reference, you have a connection to the television, but when someonesays change the channel or lower the volume, what you remanipulating is the reference, which in turn modifies the object.If youwant to move around the room and still control the television, you takethe remote/reference with you, not the television.Also, the remote control can stand on its own, with no television.That is,just because you have a reference doesn t mean there s necessarily anobject connected to it.So if you want to hold a word or sentence, youcreate a String reference:String s;But here you ve created only the reference, not an object.If you decided tosend a message to s at this point, you ll get an error (at run-time) becauses isn t actually attached to anything (there s no television).A saferpractice, then, is always to initialize a reference when you create it:String s = "asdf";1This can be a flashpoint.There are those who say clearly, it s a pointer, but thispresumes an underlying implementation.Also, Java references are much more akin toC++ references than pointers in their syntax.In the first edition of this book, I chose toinvent a new term, handle, because C++ references and Java references have someimportant differences.I was coming out of C++ and did not want to confuse the C++programmers whom I assumed would be the largest audience for Java.In the 2nd edition, Idecided that reference was the more commonly used term, and that anyone changingfrom C++ would have a lot more to cope with than the terminology of references, so theymight as well jump in with both feet.However, there are people who disagree even withthe term reference. I read in one book where it was completely wrong to say that Javasupports pass by reference, because Java object identifiers (according to that author) areactually object references. And (he goes on) everything is actually pass by value.Soyou re not passing by reference, you re passing an object reference by value. One couldargue for the precision of such convoluted explanations, but I think my approachsimplifies the understanding of the concept without hurting anything (well, the languagelawyers may claim that I m lying to you, but I ll say that I m providing an appropriateabstraction.)102 Thinking in Java www.BruceEckel.comHowever, this uses a special Java feature: strings can be initialized withquoted text.Normally, you must use a more general type of initializationfor objects.You must createall the objectsWhen you create a reference, you want to connect it with a new object.You do so, in general, with the new keyword.new says, Make me a newone of these objects. So in the above example, you can say:String s = new String("asdf");Not only does this mean Make me a new String, but it also givesinformation about how to make the String by supplying an initialcharacter string.Of course, String is not the only type that exists.Java comes with aplethora of ready-made types.What s more important is that you cancreate your own types [ Pobierz całość w formacie PDF ]