Java
  Home arrow Java arrow Page 2 - Primitive Data Types and Basic Languag...
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
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

Primitive Data Types and Basic Language Rules for Java
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2007-09-24

    Table of Contents:
  • Primitive Data Types and Basic Language Rules for Java
  • Working with Arrays
  • Multidimensional Arrays
  • A Few More Rules

  • 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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ziff Davis Enterprise Virtual Tradeshows: Hot Topics, Cutting Edge Technology, Real-time Networking among IT Professionals. Learn more

    Primitive Data Types and Basic Language Rules for Java - Working with Arrays
    (Page 2 of 4 )

    While they won't let you see through walls like their cousin, the X-ray, Arrays act as a holding cell for multiple values of the same type. It's like taking a group of variables and some glue and sticking them all together. When you create an array, you must determine its length. This length determines the number of placeholders within the array; this number  does not change once the array is created.

    A good way to think of it is like an egg carton. When you go to the store you can buy six eggs or twelve eggs (or if you are an eggomaniac, you can opt for the deluxe 24 pack, but come on, let's not get clucking crazy). Each little crate within that pack was at one point empty. Then they placed it on an assembly line and let the chickens do their thing. Soon, an egg was placed into each holder. Since the carton was only created to hold 12 eggs, you can't put anymore into it. And since it is an egg carton, you can't place anything but eggs within it.

    Anything you put into an array is referred to as an element. And every element has an index number for you to to refer to it.

    In the above display, you will notice there are five boxes, meaning the array has a length of five. Each box of the array has an index number, starting with the number zero and working its way to the number four. Also, you will notice that Index 0 has something inside it, or an element. The other indexes are empty and are waiting to be assigned a value.

    The way we create and assign a value is shown below.

    class ArrayXray {

      public static void main(String[] args) {

        int[] YourArray; // declaring an array of integers

        YourArray = new int[5]; // allocates memory for 5 integers

     

        YourArray[0] = 100; // initializes or sets the first element

        YourArray[1] = 200; // initializes second element

        YourArray[2] = 300; // so forth

        YourArray[3] = 400;

        YourArray[4] = 500;

    In the above example, you have created a new array named "Your Array." We made the array an integer (int) data type and assigned it five elements. Then we gave each of those elements a value. Once you have created your array, you can not change it later without recreating the original array.

    We also initialized the array using the word new. Without doing so we would have been yelled at by the compiler and received an F on our report card, not to mention an error message.

    You can also declare an array in the following manner:

    int[] YourArray = {100, 200, 300, 400, 500};

    You'll note in the above sample that the length of the array is determined by the number of values between the "{" and "}" brackets.

    More Java Articles
    More By James Payne


       · This article covers Primitive Data Types in Java. I hope it proves useful. If you...
     

       

    JAVA ARTICLES

    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections
    - Combating the ‘Object Crisis’

    Iron Speed



    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway