2. Primitive Data types - ZolotovaNatalia/JavaCourse_2017 GitHub Wiki
Definition of variables
Programmers work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides. The data is given a name, so that it can be re-called whenever it is need. The name, and its value, is known as a Variable. Since Java is a strongly typed language, each variable should have a type. You can call them almost anything you like, but there are a few rules:
- Variable names can't start with a number. So
myVariable
is OK, but not1myVariable
. You can have numbers elsewhere in the variable name, just not at the start:myVariable1
,my1Variable
. - Variable names can't be the same as Java keywords. There are quite a lot of these: public, private, void, static etc. You can find the whole list here: Java keywords
- You can't have spaces or points in your variable names. The variable declaration
int my Variable
will get you an error. We've used the underscore character, but it's common practise to have the first word start with a lowercase letter and the second or subsequent words in uppercase:myVariable
. If you put a comma in the name it is considered as a declaration of two variables :int my,Variable
will create two variablesint my
andint Variable
. - Variable names are case sensitive. So
myVariable
andMyvariableare
are different variable names.
Numbers
In Java You can store a number in different ways. Whole numbers such as 1, 3, 10, 12, etc, are stored using the byte, shor, int or long variable. (The int stands for integer.) Floating point numbers like 8.4, 10.5, 12.8, etc, are stored using the double or float variable. You do the storing with an equals sign ( = ).
Example:
int number1 = 10;
long number2 = 100;
double number3 = 10.45;
float number4 = 13.78;
All these types differ from each other by possible values and memory that they occupy.
Type | Size | Range of values that can be stored |
---|---|---|
byte | 1 byte | ā128 to 127 |
short | 2 bytes | ā32768 to 32767 |
int | 4 bytes | ā2,147,483,648 to 2,147,483,647 |
long | 8 bytes | 9,223,372,036,854,775,808 to 9,223,372,036,854,755,807 |
float | 4 bytes | 3.4eā038 to 3.4e+038 |
double | 8 bytes | 1.7eā308 to 1.7e+038 |
Arithmetic Operators
Arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, and division.
Assume we have two integer variable A = 10
and B = 20
:
Operator | Description | Example |
---|---|---|
+ (Addition) | Adds values on either side of the operator. | A + B will give 30 |
- (Subtraction) | Subtracts right-hand operand from left-hand operand. | A - B will give -10 |
* (Multiplication) | Multiplies values on either side of the operator. | A * B will give 200 |
/ (Division) | Divides left-hand operand by right-hand operand. | B / A will give 2 |
% (Modulus) | Divides left-hand operand by right-hand operand and returns remainder. | B % A will give 0 |
++ (Postfix-Increment) | Increases the value of operand by 1. | B++ gives 21 |
-- (Postfix-Decrement) | Decreases the value of operand by 1. | B-- gives 19 |
Char and String
Char data type is used to store any character: char letterA = 'A'
.
Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects, but we will talk about it on the next lessons. For now it is enough to know how to create a variable of the type String: String myText = "This is a text"
.
Boolean
This data type is used for simple flags that track true/false conditions. Boolean variable can have only two values: true or false. Default value is false.
Example: boolean one = true
.
Have fun with primitive types!
We recommend you to play with different data types by creating variables and performing arithmetic operations with them.
You can do it in the method main()
and print a result using System.out.println()
that was discussed in the first lecture.
For example:
public static void main(String[] args){
int a = 1;
int b = 10;
double c = 12.59;
double d = 15.30;
System.out.println(a + b);
System.out.println(a - d);
System.out.println(c * d);
String name = "Pater";
int age = 10;
// the '+' sign is here a string operation which concatenates multiple strings to a single one.
System.out.println("My name is " + name + ". I have " + age + " cats.");
}
Exercises
- Find out the result of the formula:
? a = ?;
? b = ?;
? c = ?;
? d = ?;
? e = ?;
? f = ?;
? y = 2 * (a - b) / 35.8 + (a * a + b / c) * (d - 5.14 * e) + 25.10 / (a + c * f );
You have to define the types and the value of all the variables first.
Which type should be used for the variable y
? What happens if it is a double
or an int
?
- Print out the result of this formula in the format like:
y = ?, where a = ?, b = ?, c = ?, d = ?, e = ?, f = ?
Remember to use variables for the print out, example
int x = 1;
System.out.println("value of x = " + x);
- Create two int variables, for example, a and b. Perform arithmetic operations:
int a = 1;
int b = 1;
b =+ a;
b += a;
b = ++a;
b = a++;
- Think about the difference between these operations, how b and a variables are changing. I recommend restore the start values of a and b before each operation. Print out the result in the format like:
b =+ a;
b += a;
b = ++a;
b = a++;
- Create five different variables of types
short, int, long, double or float
. Print out results of 10 different expressions with these variables that return boolean value true or false. Try to compare different types and see what happens. For example:
System.out.println(a == b);
System.out.println(a < b);
System.out.println(a >= b);
System.out.println(a != b);