|
|
|
| Type name (keyword) | Number of bytes | Range of values |
|---|---|---|
| byte | 1 | −128 . . . 127 |
| short | 2 | −32768 . . . 32767 |
| int | 4 | −2,147,483,648 . . . 2,147,483,647 |
| long | 8 | −9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807 |
|
public class Safe
{
public static void main(String[] args)
{
int x;
short y;
y = 1;
x = y; // Safe conversion
System.out.println(x);
}
}
|
What happens inside the computer (behind the scene):
|
public class UnSafe
{
public static void main(String[] args)
{
int x; // x uses 4 bytes
short y; // y uses 2 bytes
x = 65538; // 65538 = 2^16 + 2
// Binary: 00000000 00000001 00000000 00000010
y = (short) x; // Unsafe conversion
// y = 00000000 00000010 (lower half of x)
// (Correct only for numbers < 65535)
System.out.println(y);
//***** Prints 2 !!!
// Because 00000000 00000010 is equal to 2
}
}
|
What happens inside the computer (behind the scene):
|
How to run the program:
|
|
|
We will look at an example a little bit later.
|
Most of the integer (arithmetic) operators should look familiar to you...
(Except for the % operator)
| Operator symbol | Operation | Note |
|---|---|---|
| + | addition | Binary operator, e.g.: 9 + 4 = 13 |
| − | subtraction | Binary operator, e.g.: 9 − 4 = 5 |
| * | multiplication | Binary operator, e.g.: 9 * 4 = 36 |
| / | division (= quotient) | Binary operator, e.g.: 9 / 4 = 2 |
| % | modulus (= remainder) | Binary operator, e.g.: 9 % 4 = 1 |
| ( ... ) | brackets | Changes order of computation |
| − | negation |
Changes the sign of the value:
− 4 = (−4)
(Read as: − 4 = negative 4) |
|
It does so by truncating the floating point division
Examples:
9 / 4 = (floating point result = 2.25) = 2 -9 / 4 = (floating point result = -2.25) = -2 9 / -4 = (floating point result = -2.25) = -2 -9 / -4 = (floating point result = 2.25) = 2 |
The sign of the result is always equal to the sign of the dividend
Examples:
9 % 4 = (floating point result = 2.25) = 1 -9 % 4 = (floating point result = -2.25) = -1 9 % -4 = (floating point result = -2.25) = 1 -9 % -4 = (floating point result = 2.25) = -1 |
dividend = quotient × divisor + remainder
|
Example:
9 / 4 = 2 9 % 4 = 1 9 = 2 * 4 + 1 -9 / 4 = -2 -9 % 4 = -1 -9 = (-2)* 4 + (-1) 9 / -4 = -2 9 % -4 = 1 9 = (-2)*(-4) + 1 -9 / -4 = 2 -9 % -4 = -1 -9 = 2 *(-4) + (-1) |
|
Special emphasis:
|
|
| Operator | Priority | Note |
|---|---|---|
| ( .... ) | Highest | |
| − (negation) | Higher | Unary operator, e.g.: −3 |
| * / % | High | Binary operator, e.g.: 4 * 5 |
| + − | Lowest | Binary operator, e.g.: 4 + 5 |
| Operator | Associativity | Example |
|---|---|---|
| ( .... ) | "inside out" | ((3 + 4) * 5 ) = (7 * 5) |
| − (negation) | Right | − − 3 = − (−3) = 3 (from right to left) |
| * / % | Left | 4 * 5 % 7 / 4 = 20 % 7 / 4 = 6 / 4 = 1 |
| + − | Left | 4 − 6 + 4 − 7 = (−2) + 4 − 7 = 2 − 7 = −5 |
Integer expression: 72 / 10 + 72 % 10
Evaluated as follows: 72 / 10 + 72 % 10
= 7 + 72 % 10
= 7 + 2
= 9
|
(This is how you compute the sum of the digits in the number 72 !!!)
Integer expression: 22 - - 3 * - 4 + - - 1
Evaluated as follows: 22 - - 3 * - 4 + - - 1
= 22 - (-3) * - 4 + - - 1
= 22 - (-3) * (-4) + - - 1
= 22 - (-3) * (-4) + - (-1)
= 22 - (-3) * (-4) + (+1)
= 22 - 12 + 1
(Use associativity rule) = 10 + 1
= 11
|
Integer expression: (22 - - 3) * - (4 + - - 1)
Evaluated as follows: (22 - - 3) * - (4 + - - 1)
= (22 - (-3)) * - (4 + - - 1)
= (22 - (-3)) * - (4 + - (-1))
= (22 - (-3)) * - (4 + 1)
= 25 * - (4 + 1)
= 25 * - 5
= 25 * (-5)
= -125
|
How to run the program:
|
|
|
|
Let n = 15432 seconds
1. We can find the # seconds as follows:
257
-------------
60 / 15432
15420
--------
12
So: 15432 seconds = 257 minutes + 12 seconds
Or: 15432 seconds = 15432/60 minutes + 15432/60 seconds
2. Next we convert the remaining 257 minutes into hours:
4
--------
60 / 257
240
----
17
Therefore: 257 minutes = 257/60 hours + 257%60 minutes
|
input n; // n = total number of seconds
seconds = n % 60; // computes seconds
n = n / 60; // n is now = remaining minutes
minutes = n % 60; // computes minutes
n = n / 60; // n is now = remaining hours
hours = n;
|
n = 15432; hours = 15432 / 3600 = 4 r = 15432 % 3600 = 1032 minutes = 1032 / 60 = 17 seconds = 1032 % 60 = 12 |
import java.util.Scanner;
public class Hours
{
public static void main(String[] args)
{
int n, r, hours, minutes, seconds;
Scanner in = new Scanner(System.in);
System.out.print("Enter # seconds: ");
n = in.nextInt();
// in.nextInt() reads in an integer value
seconds = n % 60; // computes seconds
n = n / 60; // n is now = remaining minutes
minutes = n % 60; // computes minutes
n = n / 60; // n is now = remaining hours
hours = n;
System.out.print(n);
System.out.print(" seconds = ");
System.out.print(hours);
System.out.print(" hours + ");
System.out.print(minutes);
System.out.print(" minutes+ ");
System.out.print(seconds);
System.out.println(" seconds.");
}
}
|
How to run the program:
|
|
|
Let n = 100000000 millisec
1. Find total number of seconds since midnight, Jan 1 1970:
n = n / 1000 = 100000000 / 1000 = 100000 (total # seconds)
2. Find seconds in current time:
seconds = n % 60 = 100000000 % 1000 = 40 *** part of answer
n = n / 60 = 100000 / 60 = 1666 (total # minutes)
3. Find minutes in current time:
minutes = n % 60 = 1666 / 60 = 46 *** part of answer
n = n / 60 = 1666 % 60 = 27 (total # hours)
3. Find hours in current time:
hours = n % 27 = 27 % 24 = 3 *** part of answer
|
n = System.currentTimeMillis(); // n = total # milli sec n = n / 1000; // n is now = total # sec seconds = n % 60; // Compute seconds in current time n = n / 60; // n is now = total # minutes minutes = n % 60; // Compute minutes in current time n = n / 60; // n is now = total # hours hours = n % 60; // Compute hours in current time |
public class ShowCurrentTime
{
public static void main(String[] args)
{
long nMillis; // We need long for accuracy !!!
long n, hours, minutes, seconds;
nMillis = System.currentTimeMillis();
n = nMillis/1000; // Total # Second;
seconds = n % 60; // Seconds
n = n / 60; // Total # minutes;
minutes = n % 60; // Minutes
n = n / 60; // Total # hours;
hours = n % 24; // Hours
System.out.print(hours);
System.out.print(":");
System.out.print(minutes);
System.out.print(":");
System.out.print(seconds);
System.out.println(" GMT");
}
}
|
How to run the program:
|
|
public class Caveat2
{
public static void main(String[] args)
{
short a, b, c;
a = 2;
b = 3;
c = a + b; // Compilation error !
}
}
|
Can you see why the statement c = a + b will cause a compilation error ?
|
|
byte ⇒ short ⇒ int ⇒ long |
long a;
int b;
short c;
byte d;
/* ---------------------------------
Automatic conversion happens in
all the following assignments
--------------------------------- */
a = b;
a = c;
a = d;
b = c;
b = d;
c = d;
|
long a;
int b;
short c;
byte d;
/* -----------------------------------
Unsafe assignment requires casting
------------------------------------ */
b = (int) a;
c = (short) a;
c = (short) b;
d = (byte) a;
d = (byte) b;
d = (byte) c;
|
public class Exercise1
{
public static void main(String[] args)
{
int a = 15;
int b = 24;
System.out.println(b - a + 7);
System.out.println(b - a - 4);
System.out.println(b % a / 2);
System.out.println(b % (a / 2));
System.out.println(b * a / 2);
System.out.println(b * (a / 2));
System.out.println(b / 2 * a);
System.out.println(b / (2 * a));
}
}
|
16 5 4 3 180 168 180 0 |
How to run the program:
|
|
12 is divisible by 2 ⇔ 12 % 2 = 0
12 is divisible by 3 ⇔ 12 % 3 = 0
12 is divisible by 4 ⇔ 12 % 4 = 0
12 is not divisible by 5 ⇔ 12 % 5 = 2
12 is divisible by 6 ⇔ 12 % 6 = 0
12 is not divisible by 7 ⇔ 12 % 7 = 5
|
| Type name (keyword) | Range of values |
|---|---|
| byte | −128 . . . 127 |
| short | −32768 . . . 32767 |
| int | −2,147,483,648 . . . 2,147,483,647 |
| long | −9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807 |
public class Overflow
{
public static void main(String[] args)
{
int x, y, z;
long a, b, c;
x = 1000000;
y = 3000;
z = x * y; // 3,000,000,000 is outside the range of int
System.out.println(z);
a = 1000000;
b = 3000;
c = a * b; // 3,000,000,000 is within the range of long
System.out.println(c);
}
}
|
Output:
-1294967296 (not 3000000000 !!!)
3000000000
|
Explanation:
|
How to run the program:
|
|