Home arrow Java & J2EE arrow Primitive Data Types and Basic Language Rules for Java

Primitive Data Types and Basic Language Rules for Java

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.

  1. Primitive Data Types and Basic Language Rules for Java
  2. Working with Arrays
  3. Multidimensional Arrays
  4. A Few More Rules
By: James Payne
Rating: starstarstarstarstar / 15
September 24, 2007

print this article



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 & J2EE Articles          >>> More By James Payne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: