Java
  Home arrow Java arrow Page 4 - Java Part 3: Primitive Types
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java Part 3: Primitive Types
By: Chris Noack
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2001-12-03

    Table of Contents:
  • Java Part 3: Primitive Types
  • Primitive types in Java
  • Creating our first class
  • Our new class...explained
  • Compiling the Application
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Java Part 3: Primitive Types - Our new class...explained


    (Page 4 of 6 )

    class Number{

    The first line is the class definition. It tells the Java compiler that the class being defined is called “Number”. The class name must start with a capital letter. Also, the file that holds your class must have the same name as the class.

    float value = 0;

    The next part of our class is the member declaration. This states that a “Number” object has a member called “value”, which is a float type and is set to zero every time and instance of the Number class is created.

    Number(){}

    Number( float val ){

    value = val;

    }


    These are called the class’s constructors. A constructor is automatically called when a new instance of a class is created. There can be multiple constructors for a class and the compiler will decide which class to call based on the type of value passed to that constructor. Constructors have no return type and if several are present, they must have different parameter types or a different numbers of arguments.

    The first constructor is the default constructor; it leaves the member value

    set as zero, which is the default value. The second constructor is called if an instance of the Number class is created with a float argument. Also, if an int argument is supplied to this constructor, the Java compiler will automatically cast it to a float value.

    Next, we have the main method. I will discuss this method after the class methods have been discussed.

    float addTo( float x ){

    return value+=x;

    }


    The addTo( float ) method seems rather simplistic and trivial, but this

    is not the case. In object-orientated programming, we want to design our classes so that we never have to directly reference their member variables (such as

    value) directly. Instead, we use class methods, which either modify or return the

    classes members so that information is hidden from other classes and we don't have to know how every class works, but just how to use it via its member functions. This is a common practice that should be employed in all object-orientated languages.

    addTo( float ), subFrom( float ), multiplyBy( float ) and divideBy( float ) are all modifier methods that allow us to change value without having to directly reference it.

    The getValue() method is an accessor method. It simply returns the number stored in the value member variable.

    public static void main( String[] args ) {

    Number number = new Number( 5 );

    number.addTo( 5 );

    number.divideBy( 2 );

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

    System.exit(1);

    }


    This is the classes main method. The first line of this method simply tells the Java compiler that the main method takes an array of Strings as an argument. We don’t use these string arguments in our example, so we will not discuss it here.

    Number number = new Number( 5 );

    Creates a new Number object with the name number. Java variables are case sentitive, meaning that x is different from X and myVal is different from MYVAL. Because 5 was supplied as an argument to the new instance of our Number class, the second constructor is called when the object is created, and consequently, value is set to 5.0.

    number.addTo(5);

    This line calls the addTo member function of the Number class. We could have performed the same operation with a line such as

    number.value += 5;

    but this would have directly referenced the value member variable, and this is what we are trying to avoid.

    number.divideBy( 2 );

    This line calls the divideBy() member function, which divides the value member by 2. The value member variable is now equal to 5 again.

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

    Don’t get confused by this line just yet. It is one of Java's printing functions. The argument to println() is preceeded by a +. This tells the Java compiler to print the value which number.getVal() holds to the screen, instead of simply printing its value as a String.

    The final line exits the main function and ends the application. Now that we know how to create a simple class, I will discuss compiling a java application.

    More Java Articles
    More By Chris Noack


     

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT