Java
  Home arrow Java arrow Page 2 - 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 
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 - 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.

    Arrays

    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.

    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-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek