Session 1 - Data Types in Java
What is Data ?
Most of you have shopped online and this is what a typical purchase looks like
- You select a product which has a Price, Tax, Quantity
- You pay using your Credit Card by entering Name, CC Number, Expiration Date, Billing Address, Zipcode
Words above in Bold are data.
What is then Data Type ?
Can we say Price, Tax, Quantity is of type Number because we can apply Arithmetic (+, -, /, *, %) Operations on them, i.e. we multiply Price & Quantity and than add Tax.
Similarly Your Name, Address are text because it doesn't make sense to have Arithmetic Operation on them and in Java we call them String.
Similarly Your Name, Address are text because it doesn't make sense to have Arithmetic Operation on them and in Java we call them String.
Supported Data Types in Java
String - Text
double - Real Numbers i.e. 33.2, 20.0 etc
int - Non-Real Numbers i.e. 10, 30, 3453 etc
Typical Java Programs using the above Data Types
public class DataTypesDemo {
public static void main(String[] args) {
// define a Data Variable message of type String
String message = "Hello World!";
// print to console i.e. black screen
System.out.println(message);
}
}
Code Snippet - 1
// define a Data Variables of type int
int x = 10;
int y = 20;
int z = x + y;
// print to console i.e. black screen
System.out.println(z);
Code Snippet - 2
// define a Data Variables of type int
double x = 10.0;
double y = 20.0;
double z = x + y;
// print to console i.e. black screen
System.out.println(z);
Question for you.
what type is Phone No. (i.e. int or String) ?
Its String since you will never apply Arithmetic Operations on it.
Homework
There are few others Data Types which I missed that can be found in any book.
what type is Phone No. (i.e. int or String) ?
Its String since you will never apply Arithmetic Operations on it.
Homework
There are few others Data Types which I missed that can be found in any book.
Comments
public static void testSomething() {
double maybeOne = .1 * 10;
System.out.println("Does .1 x 10 = 1 in floating point math?");
System.out.println(maybeOne == 1);
System.out.println("Actual Value:" + maybeOne);
maybeOne = .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1;
System.out.println("How about if we add .1 ten times?");
System.out.println(maybeOne == 1);
System.out.println("Actual Value:" + maybeOne);
}
Output:
Does .1 x 10 = 1 in floating point math?
true
Actual Value:1.0
How about if we add .1 ten times?
false
Actual Value:0.9999999999999999
Javin
An Example of using ArrayList in Java 1.5 with generics