Home arrow Java arrow Page 2 - 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 - Primitive types in Java
(Page 2 of 6 )

There are a variety of primitive types defined in the Java language. They are common to the C/C++ language, and are described below. There are a number of complexities and intricacies associated with these types, but for now the basics will suffice.

The boolean Type

In Java and other languages, the Boolean type represents a truth value with two possible states: On or off, true or false. Booleans are used in conditional statements such as if and else, while and the test condition in for loops.

The char type

The char type represents a single unicode character. It is the building block of Strings, which are used to represent text in Java. You can declare a new char variable, c, with the value of a, like this:

char c = 'a';

There are many useful functions for working with chars that are built into the Java language, such as isDigit() and isLowerCase(). Functions like these return Boolean values, but there are various other functions that return other types of primitive values.

The int type

Numerical types in Java fall into many categories, such as byte, short, int and long. For now we will focus on the int type, which is the most commonly used numerical type.

The int type represents whole integer values ranging from -231 to 231 – 1. Int’s are natural numbers and can be both positive and negative.

The float types

Real numbers with fractional components are represented in Java as either float or double values. Floats are represented in Java as 32 bit values and, as the name suggests, doubles are 64 bit floating-point values.

The String type

In Java, the string type is not really a primitive type; it is a class. However, due to its popularity, it was added to the java language so that it could be used in programs as simply as the previously mentioned primitives. One very handy addition to Java is the fact that the String class has many methods to manipulate and deal with Strings.


If you have any experience with C/C++, then you will be glad to know that arrays in Java are the same as arrays in C. We can declare an array that will hold 100 int's with this line of code:

int[] array = new int[100];

There are various other ways to declare the same array structure, but this one will do for now, because it is identical to the syntax used by the C compiler. All indexes in the array are initialized to zero by default. We can set the value of each index in the array with a for-loop construct, like this:

int i;

for( i = 0; i < 100; i++ ){

array[i] = i;


The first position in the array is indexed at zero, and the final position in the array is the size of the array – 1; in this case, 100 –1, or 99. So, the array now holds the integer values 0 to 99. Arrays can hold primitive types as well as objects.

Now that we have a good understanding of some of the primitive types that are available to us in Java, let’s write a very simple class and compile it.
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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials