internet.com

Go to WebDeveloper Home


hm-v6-139x61.gif

Database Journal: The Knowledge Center for Database Professionals

find a web host with:
CGI Access
DB Support
NT Servers
UNIX Servers
Telnet Access

advanced search
jobs

Get FREE Development Info via your Email!

The Webdeveloper channel
FlashKit
GIF.com
HiermenusCentral
webdeveloper Jobs
Java Boutique
JavaScript.com
JavaScript Source
ScriptSearch
StreamingMedia World
WDJ
WDVL
WebDeveloper.com
WebReference.com
XMLFiles.com

internet.com
Internet News
Internet Investing
Internet Technology
Windows Internet Tech.
Linux/Open Source
Web Developer
ECommerce/Marketing
ISP Resources
ASP Resources
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers

internet.commerce
Be a Commerce Partner










J A V A
WebDeveloper.com

Java Starter
(Part 2)

By Thomas Ko

Primitive Types - Java in All its Rawness

Remember when we created the Car object lexus, we used the command line Car lexus = new Car(); . On the left side of the equal sign we are declaring that lexus is a Car object and on the right side we are creating a new Car object that is assigned to lexus. The same thing is true with these two lines:

double kilometres;
int speed;

In these two cases we are declaring kilometres as a double and speed as an int. Both int and double are known as primitive types so they don't require the "new" command (as we did with lexus) to create them. Here's a list of other primitive types:


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.

Return Types/Parameters - Trust Them to Deliver

Return Types:

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().

Parameters:

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...

[ Click here to move to the next part of the article ]

Fast Jump to Anywhere on WebDeveloper.com®:


Contact the WebDeveloper.com® staff

Last modified: 20

 

Refresh Daily
Join Editor-in-Chief David Fiedler The Editor With No Time and find truth, justice, and a clue or two.


Browse by Category
[ Site Map ]

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


internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs