datalogo
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Variables

pin  A variable is a named memory location which temporarily stores data that can change while the program is running. A variable holds a value.

pin A final is a named memory location which temporarily stores data that remains the same throughout the execution of the program.  It is a constant variable in the program.

pin The type of a variable indicates what kind of value it will store. For example, the "type" may be mathematical in nature, such as an integer or decimal value, or it may be text or letters.

pin The name of a variable is known as its identifier. You, the programmer, will choose the name of the variable.

pin A variable is given a value through an assignment statement.

Java recognizes different data "types" of variables depending upon what kind of data they can contain.  Java has eight built-in primitive data types designated by reserved words:

byte 
short 
int 
long
float
double
char
boolean

Variables of different types occupy different amounts of memory space and are described as having different sizes. 

Of the eight primitive data types in Java, the four most commonly used are: double, int, boolean, and char . When you learn about objects, you will discuss the differences between primitives and objects.

Variables Most Often Used

Data Type 
Java Keyword 
Kind of Value 
Bytes of Memory 
Range of Values
Character 
char 
1 character - Unicode
2
not applicable
Byte
byte
Integer
1
-128 to127  
Short integer 
short
 
Integers 
2
-32,768 to 32,767
(-215 to 215 - 1)  
Integer 
int 
Integers
4
-2,147,483,648 to 2,147,483,647
(-231 to 231 - 1)
Long Integer
  long 
Integers
8
-9223372036854775808 to
9223372036854775807
(-263 to 263 - 1)
Float
float 
Decimal values to 7 decimal digit precision
4
3.4e-38 to 3.4e38
 positive and negative  
Double
double
Decimal values to 15 decimal digit precision 
8
1.7e-308 to 1.73e308
 positive and negative  
Boolean 
bool 
Boolean (Logical) values
True or False 
1
not applicable

  

Rules for assigning variables:

pin Assign int data types when you are sure a variable is an integer (NO decimal points).  (You could also use "short" or "long", but for now we will concentrate on "int". Which type you choose would depend upon the size of the numbers.)

pin Assign double when decimals are needed.  (You could also use "float", but for now we will concentrate on "double".Which type you choose would depend upon the size of the numbers.)

pin Assign char if the variable will always contain only ONE character of data. A char variable is a single letter, number, or symbol and is always assigned using single quotes (char letter = 'M'; ).

pin Assign boolean if you are dealing with TRUE or FALSE situations.

divider

 Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.