A Look at Variables, Data Types, and Arithmetic Operators.

A Look at Variables, Data Types, and Arithmetic Operators.

Variables.

Variables allow a programmer to store and work with data in the computer's memory. Our job is to determine how many variables a program will need and what types of data it will hold.

File: Variable.java

1 // This program has a variable
2
3 public class Variable
4{
5    public static void main(String [] args)
6    {
7    int value; 
8    value = 5;
9    System.out.print("The value is");
10    System.out.println(value);
11 }
12}

Program Output:

The value is 5

int value is the variable of type integer. Line 7 is called a variable declaration. A variable must be declared before it is used.

value = 5 is called an assignment statement. The = equal sign is an operator that stores the value on its right into the variable value.

Some variable RULES

Variable Name ( Legal or Illegal)

dayOfWeek Legal

5Tomatoes Illegal, because identifiers cannot begin with a digit.

june1997 Legal

mix#6Eggs Illegal, because identifiers may only use alphabetic letters, digits, underscores, or dollar signs.

car parts Illegal because identifiers cannot contain spaces.

Data Types.

Primitive data types

Java uses primitive data types for holding numeric data. A variable's data type is the type of data that a variable can hold.

NOTE: It is important to properly select the appropriate type of data that your program will work with.

Primitive data types for numeric data

Data Type Size and Range:

byte 1 byte Integers in the range of -128 to +127

short 2 bytes Integers in the range of -32,768 to +32,767

int 4 bytes Integers in the range of 22,147,483,648 to 12,147,483,647

long 8 bytes Integers in the range of 29,223,372,036,854,775,808 to 19,223,372,036,854,775,807

float 4 bytes Floating-point numbers in range of 63.4 3 10–38 to 63.4 3 1038, with 7 digits of accuracy

double 8 bytes Floating-point numbers in the range of 61.7 3 10–308 to 61.7 3 10308, with 15 digits of accuracy

NOTE: These data types are called “primitive” because you cannot use them to create objects. With the primitive data types, you can only create variables and a variable can only be used to hold a single value.

Integer Data Types Example

1 // This program has variables of several of the integer types.
2
3 public class IntegerVariables
4 {
5 public static void main(String[] args)
6 {
7  int checking; // Declare an int variable named checking.
8  byte miles; // Declare a byte variable named miles.
9  short minutes; // Declare a short variable named minutes.
10  long days; // Declare a long variable named days.
11
12 checking = -20;
13 miles = 105;
14 minutes = 120;
15 days = 189000;
16 System.out.println("We have made a journey of " + miles +
17 " miles.");
18 System.out.println("It took us " + minutes + " minutes.");
19 System.out.println("Our account balance is $" + checking);
20 System.out.println("About " + days + " days ago Mubanga " +
21 "stood on this spot.");
22 }
23 }

Program Output:

We have made a journey of 105 miles.

It took us 120 minutes.

Our account balance is $-20

About 189000 days ago Mubanga stood on this spot.

Floating-Point Data Types.

The float data type is considered a single-precision data type. It can store a floating-point number with 7 digits of accuracy. The `double data type is considered a double-precision data type. It can store a floating-point number with 15 digits of accuracy.

File: Sale.java

1 // This program demonstrates the double data type.
2
3 public class Sale
4 {
5 public static void main(String[] args)
6 {
7 double price, tax, total;
8
9 price = 29.75;
10 tax = 1.76;
11 total = 31.51;
12 System.out.println("The price of the item " +
13 "is " + price);
14 System.out.println("The tax is " + tax);
15 System.out.println("The total is " + total);
16 }
17 }

Program Output

The price of the item is 29.75

The tax is 1.76

The total is 31.51

boolean Data Type

The boolean data type allows you to create variables that may hold one of two possible values: true or false.

File: TrueFalse.java

1 // A program for demonstrating boolean variables
2
3 public class TrueFalse
4 {
5 public static void main(String[] args)
6 {
7 boolean bool;
8
9 bool = true;
10 System.out.println(bool);
11 bool = false;
12 System.out.println(bool);
13 }
14 }

Program Output

true

false

char Data Type

The char data type is used to store characters. A variable of the char data type can hold one character at a time. Character literals are enclosed in single quotation marks.

File: Letters.java

1 // This program demonstrates the char data type.
2
3 public class Letters
4 {
5 public static void main(String[] args)
6 {
7 char letter;
8
9 letter = 'A';
10 System.out.println(letter);
11 letter = 'B';
12 System.out.println(letter);
13 }
14 }

Program Output

A

B

Arithmetic Operators.

Operators are used for manipulating numeric data in Java. Java uses binary operators that operate on two operands. For instance:

Arithmetic.jpg

Integer Division

Produces an integer quotient. That is, 7 / 4 evaluates to 1 and the expression 17 / 5 to 3.

Any fractional part in integer division is discarded - no rounding takes place.

Java provides the remainder % operator that produces a remainder after division, 7 % 4 evaluates to 3 etc.

Operator Precedence

Java evaluates operations in the order that is shown below.

OpPrecedence.jpg

Equality and Relational Operators

Operators.jpg

File: Comparison.java

1  // Compare integers using if statements, relational operators
2  // and equality operators.
3  import java.util.Scanner; // program uses class Scanner
4
5 public class Comparison
6 {
7 // main method begins execution of Java application
8 public static void main(String[] args)
9 {
10 // create Scanner to obtain input from command line
11 Scanner input = new Scanner(System.in);
12
13 int number1; // first number to compare
14 int number2; // second number to compare
15
16  System.out.print("Enter first integer: "); // prompts user
17  number1 = input.nextInt(); // read first number from use19
 18 System.out.print("Enter second integer: "); // prompts user
 19 number2 = input.nextInt(); // read second number from user
20
21    if (number1 == number2)
22    System.out.printf("%d == %d%n", number1, number2);
23    if (number1 != number2)
24    System.out.printf("%d != %d%n", number1, number2);
25    if (number1 < number2)
26    System.out.printf("%d < %d%n", number1, number2);
27    if (number1 > number2)
28    System.out.printf("%d > %d%n", number1, number2);
29    if (number1 <= number2)
30    System.out.printf("%d <= %d%n", number1, number2);
31    if (number1 >= number2)
32    System.out.printf("%d >= %d%n", number1, number2);
33 } // end method main
34 } // end class Comparison

Program Output

Enter first integer: 777

Enter second integer: 777 777 == 777

777 <= 777

777 >= 777

Enter first integer: 1000

Enter second integer: 2000

1000 != 2000

1000 < 2000

1000 <= 2000

Enter first integer: 2000

Enter second integer: 1000

2000 != 1000

2000 > 1000

2000 >= 1000

The import statement on line 3 imports a Scanner class that scans for user keyboard input through the input object.

Thank you.