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

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

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



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.


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.
blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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