Intro to Variables and Operators

Variables are used for storage of information in a program. They can be addressed by name and have specific types based on the information being stored.

 

Check out my other posts if you want to learn what an IDE is and how to download one or how to use Eclipse or how to use jGrasp.

 

Primitive Data Types of Variables

 

Each of the following number data types allows the user to save an integer or decimal value into variables, if it is within the range.

Whole Numbers:

 

byte – Range: -128 to 127
short – Range: -32768 to 32767
int – Range: -2147483648 to 2147483647
long – Range: -9223372036854775808 to 9223372036854775807

 

Decimal Numbers:

 

float – 32-bit floating point
double – 64-bit floating point

 

Most commonly used are int and double. Default for all is 0 or 0.0.

 

boolean – two possible values: true or false. Default is false.

 

char – char or character allows the user to store a single letter or symbol, including numbers. Although, if the user stores a number as a character, they can not perform mathematical operations on it.

 

Primitive data types are declared simply by stating the type then the name then assigning a value to the variables like so:

 

datatype name = value;

 

or

 

int num = 5 variable

 

or

 

char c = 'a'

 

Note that numbers are assigned with no quotes and characters are assigned with single quotes. So the following is also true:

 

char c1 = '5'

 

But c1 does not have the value of 5, it has the character 5 stored in it. So c1 can not be used as a number.

 

Strings, A Non-Primative Data Type

 

Strings are a non-primitive data type, but Java makes them simple to use.
A string is any grouping of characters be it one, many or none. This means that you can have a string with just one character, a word, a full sentence or it could be empty.

 

Strings are also declared similar to the way primitive data types are declared.

 

String word = "Hello World" variables

 

Now the variable called word has the word “Hello World” stored inside. Note that with strings you use double quotes to store the value.

 

Operators

 

+ – adds two numbers together or in the case of strings it concatenates them like so: “He” + “llo” = “Hello”
— – subtracts the first number from the second
* – multiplies two numbers
/ – divides the first number by the second
= – assigns a value to the given variable

 

Brackets

 

Like in math, one can use brackets to separate operations to dictate a certain order. All brackets must be closed for a sequence to be complete, and all close brackets must be preceded by an open bracket.
YouTube
LinkedIn
Share
RSS