Java Lesson 3: Basic Data Types

0
38

Introduction

Types are used to declare variables and arrays. Unlike some languages, Java is a strongly-typed language:

  • Every variable and expression must have a type.
  • The variable and its type must be declared before the variable can be used in a program.
  • Each basic type, also referred to as a primitive type, is defined by the Java specification.
  • Developers may create their own types; but these must be defined before use.
  • All assignments are checked for type compatibility.
  • Failure to follow these rules generally results in a compile-time error message.

Purpose

The purpose of the basic types is two-fold:

  1. They specify how much memory a variable of that type will use.
  2. They describe the range (e.g., integers between -128 and +127) that a variable of that type must fall into.

Memory considerations were very important in the past because memory was expensive and therefore quite limited. Computers now have lots of inexpensive memory. The most commonly used numeric types are int and double.

Basic (Primitive) Data Types in Java

Boolean

Either of the pre-defined values true or false.

Boolean
Data Type Description Range
boolean Can have only one of two logical values: true or false. Returned by all relational operators and required by the conditional expressions of control statements such as while and if. true, false

Integer

All integer types are signed whole numbers. They can be positive or negative.
Examples: 1, 2, 3, -18, 63, -75, 255

Integer
Data Type Description Range
byte A signed 8-bit type. Especially useful when working with a stream of data from a file or network. -128 to +127
short A signed 16-bit type. Seldom used. -32,768 to +32,767
int A signed 32-bit type. Most commonly used integer type. Especially useful for controlling loops and to index arrays. -2,147,483,648 to +2,147,483,647
long A signed 64-bit type. Used when a variable of type int is not sufficiently large to hold the desired value. Useful for big whole numbers in scientific applications such as the number of stars in the galaxy. -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

Floating Point

All floating point types are signed real numbers (i.e., numbers with a decimal point and (optionally) numbers after decimal point). They can be positive or negative.
Examples: 2.7, 1.0, -43.8, 531., 98.6, 3.1415926535

Floating Point
Data Type Description Range
float A single-precision value that uses 32 bits of memory. Used for relatively small real numbers, if you must conserve memory, or some Java library methods. Otherwise use type double. 1.4e-045 to 3.4e+038
double A double-precision value that used 64 bits of memory. Optimized for high-speed calculations. Commonly used where high precision is needed and for math functions such as sin() and sqrt. 4.9e-324 to 1.8e+308

Character

The Unicode international character set is used by Java to represent characters from dozens of human languages. The subset from 0 to 127 is the traditional ASCII character set. Because char can be used as an integer data type, variables of type char can be added together, incremented, or decremented.
Examples: ‘A’ – ‘Z’, ‘a’ – ‘z’, ‘@’, ‘$’, ‘#’, ‘~’, ‘0’ – ‘9’, ‘?’, µ, β, ©, Ϣ, æ, ć, ∞

Character
Data Type Description Range
char An unsigned 16-bit type. Used for letters, symbols, and a character representation of numbers in many human languages. 0 to 65,535

Strings

Computer languages handle a sequence of characters – known as strings – in a variety of ways, such as an array of characters or as a primitive data type. In Java, a string is an object of type String. Objects are complex and will be covered in a later Objects and Classes lesson, but because strings are so commonly used we will briefly discuss them now.

String
Data Type Description Range
String A sequence of zero or more characters. Used to declare string variables and arrays of strings. One string can be assigned to another string. Strings can be concatenated using the + operator. If any operands are not Strings, they are converted to Strings before being concatenated. null or virtually any character sequence you can type on a keyboard.

Examples:

  • “This is a String”
  • “The answer is 42.”

Example Program3

Let’s put all this knowledge together and create, compile, and run our third Java program. This program is a bit longer than the previous examples, but take it section-by-section and try to figure out what is does. Future tutorials will cover the concepts in more detail.

Use your text editor to enter the following code example and save it in a file called Program3.java. Then compile and run it, just as you did in the previous two example programs.

Screenshot of Program3.java tutorial3_1

Usage Guidelines

With so many types to choose from, deciding which type is appropriate can be difficult for beginning programmers. Generally speaking, the vast majority of your early usage will consist of:

Boolean
Inherently used by relational operators and conditional expressions. For example:
   if (a < b) {...
   while (inventory >= minQuantity) {...
int
Any time you are counting the quantity of something, and for loop control variables. For example:
   numFrogs = numFrogs + 1;
   for (int i = 0; i < j; i++) {...
double
Very large or very small numbers, or that require fractions. For example:
   lengthInMeters = 2.75;
   totalCost = (12 * 0.39); // a dozen eggs

Type Wrappers

With the exception of String, the types we have examined so far are known as primitive types, so named because they are fundamental to simple mathematical operations. They are purposely designed to be fast and efficient.

Java also provides type wrappers, which are used to hold information about the primitive types (boolean, int, character, long, double, float) and provide methods for comparison and string output. The type wrappers (Boolean, Integer, Character, Long, Double, Float) are similar to the primitive types  but all type wrappers begin with a capital letter. We will examine these type wrappers in detail later, but mention them now because they have some useful methods, such as toString() and valueOf().

Summary

In this tutorial we learned the basic (primitive) types that Java provides to handle various kinds of information, be they numbers or characters. You will have to decide what data types to use when developing your programs. Then you will declare variables to be of those types… and that is the subject of the next lesson.

Next Lesson

Next we will move on to Variables.

 

Leave a Reply