Sunday, June 5, 2016

JAVA Variables and Data Types

 

JAVA Variables

Variables store data that can vary throughout the program run. For example there may be several calculations done in your program where the answer of each calculation is set equal to the variable "x"

3+2 =x
4+6=x
8-2=x

Since each of the possible values for x above must be different, x is a variable, each time a calculation is done the current value of x may change.

Variables can also be set equal to text that changes as you run your program.
 
x = "Going"
x = "Gone"

Every variable in Java has both a name and a type


JAVA Variable Names


The name of a variable must contain only letters (upper and lower case), numbers (0 through 9), and the underscore _ character

The first character of a variable name cannot be a number

The variable name is case-sensitive so consistent use of upper and lowercase letters is a must

The variable names in Java can contain as many characters as you want

Variable names must not match reserved words already used by Java for other functions

x_1
string_x01
Answer_100

Are examples of acceptable variable names


Tip:  Use a Capital letter to begin the name of a Class, use lowercase letters to begin a name of a variable or method

Tip: Make the name meaningful and descriptive to assist in remembering the functionality of the variable



JAVA Variable Types


The type for the variable helps to determine what kind of functions can be performed with that variable 
 

Primitive Java Types


There are 4 number integer types (byte, short, int, and long)
 
      Type int is the most common for integer whole numbers

0
-100
10000


There are 2 floating point number type (float and double)

      Type double is the most common floating point / decimal number type

0.50
-6.75946
5.4e7

Single characters are stored with type char
 
'a'
'#'
'G'

True / False values can be stored with type boolean

true
false

Declaring Variables in Java


You need to type both the variable type and name to declare it in your JAVA code

int amountSpent;

The variables must be declared before you can use them in your code

If you have multiple variables using the same type you can separate them in the declaration using a comma

int numberOfpeople, servingsChicken, servingsBeef, servingsVeggie;
double priceChicken, priceBeef, priceVeggie;

Assigning values to Variables in Java


An assignment statement can be used in your code to assign a value to a variable after it has been declared. Assigning an initial value to a variable is done typically soon after the variable is declared.

numberOfpeople = 45;
 
priceChicken = 4.05 * numberOfpeople ;
 
 

The operation to the right of the equal sign is completed first, then the variable is defined / or redefined as in the example below

                   int counter_one;
                   counter_one=0;                            value is 0
                   counter_one = counter_one +1;     value is 1
                   counter_one = counter_one +1;     value is 2             
 

No comments:

Post a Comment