Java uses four types for integers: byte, short, int, and long. Choose the type that is most appropriate for your variable. For example; if you know an integer stored in a variable is within a range of byte, declare the variable as a byte.
Java uses two types for floating-point numbers; float and double. The double type is twice as big as a float. So, the double is known as double-precision, while float is known as single precision. You should use the double type because it is more accurate than the float type.
The byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types to save memory when you are certain that the value will be within -128 and 127:
byte myNum = 100;
System.out.println(myNum);
The short data type can store whole numbers from -32768 to 32767:
short myNum = 5000;
System.out.println(myNum);
The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int data type is the preferred data type when we create variables with a numeric value.
int myNum = 100000;
System.out.println(myNum);
The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L":
long myNum = 15000000000L;
System.out.println(myNum);
You should use a floating-point type whenever you need a number with a decimal, such as 9.99 or 3.14515.
The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "f":
float myNum = 5.75f;
System.out.println(myNum);
The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a "d":
double myNum = 19.99d;
System.out.println(myNum);
The operators for numeric data types are:
First, we should know what is an even number and what is an odd number. From the definition, an even number is an integer that is "evenly divisible" by two. This means that if the integer is divided by 2, it yields no remainder. And the odd number is an integer which is not a multiple of two. If it is divided by two the result is a fraction.
Zero is an even number because zero divided by two equals zero.
So, to determine whether the number is even or odd, we can use the '%' operator. Let say the input are: 2, 3, 4, 5, 6.
2%2 is 0 so 2 is even.
3%2 is 1 so 3 is odd.
4%2 is 0 so 4 is even.
5%2 is 1 so 5 is odd.
and so on.
Remember! Even numbers can be either positive or negative. Odd numbers also can be either positive or negative.
Suppose today is Monday and you have a lecture. Your lecturer give you an assignment and asked you to submit within 10 days. Find, what day is in 10 days?
There are 7 days in a week.
Day 1 Monday
Day 2 Tuesday
Day 3 Wednesday
Day 4 Thursday
Day 5 Friday
Day 6 Saturday
Day 7 Sunday
Since today is Monday and the next 10 day is suppose to:
(1 + 10)
and to know, what day is that (1+10) day, we can use mod operator. 1 week has 7 days, so the next 10 days is:
(1 + 10) % 7
which is the 4th day or Thursday.
Write a program that obtain minutes and remaining seconds from an amount of time in seconds. For example, 500 seconds contains 8 minutes and 20 seconds.
Analysis
We know that:
1 minute = 60 second
Thus,
500 / 60 = 8 minutes
and the remaining ..
500 % 60 = 20 seconds
How to convert seconds to hour, minute and second. For example; if the input is 23 seconds, then the output will be 0:0:23, if the input is 62, then the output will be 0:1:2, and if the input is 86399 seconds, then the output will be 23:59:59.
Analysis
86399 / 60 = 1439 minutes
86399 % 60 = 59 seconds
1439 / 60 = 23 hours
1439 % 60 = 59 minutes
Thus 86399 second is 23 hours, 59 minutes, and 59 seconds
A literal is a constant value that appears directly in a program.
For example:
int i = 34;
long k = 10000;
double d = 5.0;
An integer literal can be assigned to an integer variable as long as it can fit into a variable. A compilation error would occur if the literal were too large for the variable to hold.
For example, statement byte b = 128 would cause a compilation error because 128 cannot be stored in a variable of the byte type. The range for a byte value is from -128 to 127.
Floating-point literals are written with a decimal point. By default, a floating-point literal is treated as a double type value.
For example, 5.0 is considered a double value, not a float value.
To make a number a float, use suffix f or F, and to make a number a double, use suffix d or D.
For example, 100.2f or 100.2F for a float, and 100.2d or 100.D for a double number.
Floating-point literals can also be specified in scientific notation. For example,
1.23456e+2 == 1.23456e2 = 1.23456 x 102 = 123.456
1.23456e-2 == 1.23456 x 10-2 = 0.0123456
Note: E or e represents an exponent and can be in either lowercase or uppercase.
Example
it can be translated into a Java expression as:
(1+2*a)/3 + 4* (b + c)*(5 - d- e)/f - 6 * (7/g + h)
Operators contained within pairs of parentheses are evaluated first. Multiplication, division and remainder operators are applied next. If an expression contains several multiplication, division and remainder operators, they are applied from left to right. Addition and subtraction operators are applied last. If an expression contains several addition and subtraction operators, they are applied from left to right.
Formula to converts a Fahrenheit degree to Celsius:
it can be translated into a Java expression as:
double fahrenheit = 100;
double celsius = (5.0/9) * (fahrenheit - 32);
Java allows combining assignment and addition operators using a shorthand operator. For example,
statement
i = i + 8;
can be written as:
i += 8;
The += is called the addition assignment operator. Other shorthand operators are:
There are two more shorthand operators for incrementing and decrementing a variable by 1.
What is the output of the following code segments?
float a = 12.5F;
int b = (int)a;
System.out.println("a is " + a);
System.out.println("b is " + b);
What is the output of the following code segments?
double amount = 5;
System.out.println(amount/2);
System.out.println(5/2);
What is the output of the following segment of code:
public class SalesTax {
public static void main(String[] args) {
double purchaseAmount = 197.55;
double tax = purchaseAmount * 0.06;
System.out.println("Sales tax is " + (int)(tax * 100) / 100.0);
}
}