Home arrow Java arrow Page 3 - Java Part 3: Primitive Types
JAVA

Java Part 3: Primitive Types


In part two of this ten part series, I discussed some basic Java language syntax. In this article, I will discuss some of the Java primitive types and describe the basics of a Java class. I will also touch on the JDK (Java Development Kit) and describe how to compile a small class.

Author Info:
By: Chris Noack
Rating: 4 stars4 stars4 stars4 stars4 stars / 5
December 03, 2001
TABLE OF CONTENTS:
  1. · Java Part 3: Primitive Types
  2. · Primitive types in Java
  3. · Creating our first class
  4. · Our new class...explained
  5. · Compiling the Application
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Java Part 3: Primitive Types - Creating our first class
(Page 3 of 6 )

We will write a trivial class, which will hold no real purpose, other than to show us the basics of classes and objects. Our class will be called “Number”. The code for our class is shown below. To keep it simple, I have left out some features that an entirely correct class would contain. The most prominent of these omissions are the visibility declarations, which I will be discussing in furture articles.

class Number { // The class definition

float value = 0; // A Class member, float type,

// set to zere by defualt.

Number(){} // The default constructor

Number( float val ){ // Another Constructor.

value = val;

}

public static void main( String[] args ){

Number number = new Number();

number.addTo( 5 );

number.divideBy( 5 );

System.out.println( +number.getVal() );

System.exit(1);

}

float addTo( float x ) { // The Following 4 methods manipulate

return value += x; // the class member "value"

}

float subFrom( float x ) {

return value -= x;

}

float multiplyBy( float x ) {

return value *= x;

}

float divideBy( float x ) {

if( value == 0 ){

return 0;

}

return value /= x;

}

float getVal() { // An accessor method, that simply returns

return value; // the value of the "value" member

}

}

blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials