|
|
Java Starter
|
| Type | Value |
| boolean | true/false |
| char | 16-bit unicode |
| byte | 8-bit integer |
| short | 16-bit integer |
| int | 32-bit integer |
| long | 64-bit integer |
| float | 32-bit decimal number |
| double | 64-bit decimal number |
In general you'll usually see int, double, char and boolean, but it's good to know the rest. Another type that's worth knowing is String which is in between a primitive type and Object. It can hold all types of characters. Here's the syntax:
String name; or String name = new String();Now that you grasp the various primitive types, you can add methods and variables to set the name and cost of the car.
Whenever a method is executed one of two things must happen. Something is returned or nothing is. For the latter, it's simple enough since we don't have to worry about dealing with anything after the method ends. These type of methods are given the void return type. In our program these three methods public void tuneUp(), public void drive( int distance ), public void showSpecs() were all void so when we ran them in the main() method we didn't have to set a new variable to receive anything. (*Note* Constructor has no return type.) On the other hand the method public int maxSpeed(), had an int return type which is why we had this line int max = maxSpeed(); in the main() method. For this case, max is declared as an int and it receives the value returned by the maxSpeed() method. A return type can be any primitive type or any Object. In other words, Car can be a return type. The syntax for a method returning a Car object would be: public Car buyCar(). And the stuff inside the parentheses...? You guessed it. Those are called parameters. Like return types, parameters can be primitive or be an Object. In our example public void drive (int distance) uses an int parameter called distance. When we ran this method in main(), we executed the line lexus.drive(74.7); which means distance gets the value of 74.7. Therefore the line kilometres += distance; really meant (in this case) kilometres = 0.0 + 74.7. Parameters are essential for passing information from one class to another and in fact are sometimes the only way you can do so. What the heck am I talking about now? Read on and find out...
Refresh Daily
Join Editor-in-Chief David Fiedler
and find truth, justice, and a clue or two.
![]()
ActiveX / VBscript ![]()
Animated GIF Archive ![]()
Browsers ![]()
CGI / Perl ![]()
Database Connectivity ![]()
Design / Graphics ![]()
E-Commerce ![]()
HTML-Advanced: DHTML, CSS ![]()
HTML / Site Authoring Tools ![]()
Intranet/Groupware ![]()
Java ![]()
JavaScript ![]()
Multimedia: Audio / Video / Streaming Technologies ![]()
Opinions ![]()
Refresh Daily: Editorial Column ![]()
Security ![]()
Servers & Server Tools ![]()
Site Design / Graphics ![]()
Site Management / Marketing / Log File Analysis ![]()
Tutorials ![]()
VRML / 3D ![]()
XML