Common Primitive Variables

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Java Fundamentals


Introduction

There are four common primitive variables in the Java language: int, double, char, and boolean. This section will introduce primitive variables, and the four commonly used primitive variables in Java programming.

See page 44 in the Gaddis text: http://tinyurl.com/gaddis44

[Primitive Type int]

int values are used to represent numbers, positive or negative, with no decimal spaces. It covers values from -2,147,483,648 to 2,147,483,647. If you get an integer overflow error, it means your number is out of this range.

Example 1: Examples of Integers:

 -10987		456		13		-27 

We can declare integers with the int keyword.

Example 2: Declaring Integers:

 int num1 = 3;
int num2 = 4;
int num3 = num1+num2; 

If we attempt to assign a number with a decimal to an int, Java will simply remove the decimal places. Many people make a mistake thinking the number will be rounded.

 int num = 3.1;  //num will be 3
int num = 3.99; //num will be 3 here as well 

[Primitive Type double ]

Double values are used to represent fractions, or any number using decimals (real numbers). It covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). Some valid real numbers are:

Example 3: Examples of numbers that can be stored in doubles(real numbers)

 -10.9			0.467		78.0	        -89.8389 

We can declare doubles with the double keyword.

Example 4: Using doubles

 double num1 = -10.9;
double num2 = 0.467;
double num3 = 78.0;
double num4 = -89.8389; 

[Primitive Type char ]

Data type char represents an individual character value such as a letter, digit or symbol. Each character is unique, even though some might look similar (such as the digit zero '0', and the letter 'O'). We use apostrophes to indicate character literals.

We can declare chars with the char keyword.

Example 5: Examples of characters

 char capitalA = 'A';
char number7 = '7';
char greaterThan = '>'; 

Java uses a coding scheme to represent each character called Unicode, which has 65,536 unique characters. This allows for symbols from languages all over the world to be used by Java.

Although Java uses unicode, generally we only deal the first 256 symbols, which is the ASCII table. For more info on ASCII visit http://www.asciitable.com/

[Primitive Type boolean]

The boolean data type just has two variables: true and false. We can use this data type to represent conditional values so a program can make particular decisions (if a certain result came out as false, the proper procedure would happen).

We can declare booleans with the boolean keyword.

Example 6: Using booleans

 boolean test; 

test = false; //false is assigned to the test variable 

Summary

In this section you have learned about common primitive types. Primitive data types are used all the time in program you will write for Comp 1010. These will be used in the next section when we discuss arithmetic operators.

Previous Page: Variables Next Page: Arithmetic Operators