Java
  Home arrow Java arrow Primitive Data Types and Basic Language Rules for Java
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 12
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Primitive Data Types and Basic Language Rules for Java
    ( Page 1 of 4 )

    Last time we discussed some basic concepts related to object-oriented programming. Two major ideas we defined were objects and variables. In this article, we will continue our discussion of variables by explaining how they hold and interact with data.

    Back before man could walk upright and before manicurists began to pluck grizzly hair from the knuckles of our slope-browed ancestors, man has been deciding, in a very primitive way, what type of objects fit into what types of objects. Could water fit into a stylish saber-toothed-tiger bag? Could an enormous prehistoric coconut be contained within the skull cavity of a giant sloth? And who would win in a race, a giant turtle or that enormous sloth?

    Millennia later, man is asking similar questions within the programming world. When we last left off, we were discussing variables and their naming conventions. Now that we know how to name them, we will learn what type of data best fits into each one.

    Before you can use a variable, you have to declare your variable, which means that you must give it both a name and a type. This is how a declaration looks:

    long beer = 10000;

    This tells the program that the type of variable is long, with the delicious name of beer, and that it holds the value 10,000. When declaring a variable, it isn't necessary to assign it a value. We'll discuss this more later in the Literals section.

    Below is a list of the different Primitive Data Types.

    Byte is an 8-bit data type that holds values in the range of -128 to 127. It is sometimes used to save space in arrays, in place of integers, as it does not take up as much space. Why use an integer to store the number 100, when you can use a byte and save four times the space? The default value is 0.

    Short is a 16-bit data type that holds values in the range of -32,768 to 32,767. Similar to our friend the byte, the Short (he prefers vertically challenged) can also be used to save space within an array, replacing an integer, as it is twice as small as an integer. The default value is 0.

    Int is a 32-bit data type that holds values from -2,147,483,648 to 2,147,483,647. This is the standard data type for numbers; however, if you find yourself needing more space you can use long, or if you want to save memory, you can use the byte or short in its place, so long as the number falls within the value range of the aforementioned data types. The default value is 0.

    Long is a 64-bit data type that holds values from 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. If you can't fit your number in that range, too bad for you. Default value is 0L.

    Float is a 32-bit data type. It is used for floating point numbers (not for precise numbers, such as currency). Its default value is 0.0f.

    Double is a 64-bit data type. It too is used for floating point numbers (and again, not for precise numbers such as currency). To save space, you may use float instead. Its default value is 0.0d

    Boolean is a data type that holds two types of values: True or False. By default, its value is set at False.

    Char is used to store any character. It is a 16-bit data type with the value of U0000. It works with the unicode system, within a range of u0000' (or 0) to 'uffff (or 65535).

    String is not technically a data type, but it can be used to hold sentences, and so forth. Its default value is null.

    Literals are a string constant or an explicit number. A literal is a constant value assigned to a variable.



     
     
    >>> More Java Articles          >>> More By James Payne
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT