In this second tutorial, I walk you through the two different data types in Java: Primitive and Reference. Then, I mention type casting and explain implicit and explicit type casting. Lastly, I explain the Object concept and finish with an Object Oriented Programming example and a quiz question.
Check out rest of the Tutorials: https://berksoysal.blogspot.com/2016/06/java-se-tutorials-basics-exercises.html
2. • Variables are nothing but reserved memory
locations to store values.
• This means that when you create a variable you
reserve some space in memory.
• Based on the data type of a variable, the operating
system allocates memory for this variable.
2016 Winter
3. • There are two data types available in Java:
Primitive Data Types,
Reference/ Object Data Types
2016 Winter
4. • There are eight primitive data types supported by
Java.
• Primitive data types are predefined by the language
and named by a keyword.
• These are;
Byte, Short, Int, Long, Float, Double, Boolean, Char,
String(Not Primitive, Strings are Objects)
Example uottawa.mlLab2 Lab2 Exercises
TimeConversion.java
2016 Winter
5. Byte:
• 8 bit signed.
• Minimum value is -128 (-2^7)
• Maximum value is 127 (inclusive)(2^7 -1)
• Default value is 0
• Byte data type is used to save space in large arrays,
mainly in place of integers, since a byte is four times
smaller than an int.
2016 Winter
6. Short:
• 16-bit signed
• Minimum value is -32,768 (-2^15)
• Maximum value is 32,767 (inclusive) (2^15 -1)
• Short data type can also be used to save memory as
byte data type. A short is 2 times smaller than an int
• Default value is 0.
• Example: short s = 10000, short r = -20000
2016 Winter
7. Int:
• 32-bit signed
• Minimum value is - 2,147,483,648.(-2^31)
• Maximum value is 2,147,483,647(inclusive).(2^31 -1)
Long:
• 64-bit signed
• Minimum value is -9,223,372,036,854,775,808.(-2^63)
• Maximum value is 9,223,372,036,854,775,807 (inclusive).
(2^63 -1)
2016 Winter
8. Float:
• 32-bit floating point.
• Float is mainly used to save memory in large arrays
of floating point numbers.
• Default value is 0.0f.
• Float data type is never used for precise values such
as currency.
Double:
• 64-bit floating point.
• All the other properties are similar to float.
2016 Winter
9. Boolean:
• Represents one bit of information.
• There are only two possible values: true and false.
• This data type is used for simple flags that track true/false
conditions.
• Default value is false.
• Example: boolean var = true;
Char:
• char data type is a single 16-bit Unicode character.
• Minimum value is 'u0000' (or 0).
• Maximum value is 'uffff' (or 65,535 inclusive).
• Char data type is used to store any character.
• Example: char letter ='A'
2016 Winter
10. • Reference variables are created using defined
constructors of the classes. They are used to access
objects.
• Class objects, and various type of array variables
come under reference data type.
• Default value of any reference variable is null.
• Example: Animal mydog = new Animal(“Dog");
2016 Winter
11. • Assigning a value of one type to a variable of another
type is known as Type Casting.
• Example :
long l = 100000; // Adjust this value
short s = (short) l;
System.out.println(s);
2016 Winter
12. • There are two different types of type casting:
Implicit (Widening) Casting,
Explicit (Narrowing) Casting.
2016 Winter
13. • Implicit conversion is automatic in Java,
• Information is preserved.
• Example:
short s = 4;
int i = s;
intshort
2016 Winter
14. • Explicit conversion is NOT automatic,
• There MIGHT be some losses.
• Example:
int i = 100;
// Explicit type casting required
short s = (short) i;
int short
2016 Winter
15. • Each primitive data type has a corresponding “wrapper” class.
• As the name “wrapper” suggests, such class packages a value
inside an object.
.2016 Winter
16. • Always guard a type casting with the proper test to
ensure that the value is in the proper range.
Example:
int i = 100000;
Short s;
if (i < Short.MAX_VALUE)
s = (short) i;
else
System.out.println(“Data Loss”);
2016 Winter
17. • ParseInt method of Integer class converts a String s
into an integer;
Example:
String s1 = "9";
String s2 = "4";
//Observe the difference
System.out.println(s1+s2);
System.out.println
((Integer.parseInt(s1)+Integer.parseInt(s2)));
2016 Winter
18. • Java is an OOP language.
• An Object is an entity that has states and behaviors.
Look around right now and you'll find many examples of
real-world objects: your dog, your desk, your television
set, your bicycle..
• A Class is a group of objects
that has common properties.
It is a template or blueprint
from which objects are created.
2016 Winter
19. • Example:
Let’s create an Animal Class and a Cat and a Dog Object !
Create 2 classes. (Animal and Main)
Animal must have a name, an age and a color attribute.
Also Animal must have setAge(), getName(), getType() and
getAge() methods.
Create a Cat and a Dog object with
different attributes.
2016 Winter
20. • There are two built-in classes that we can use to
generate random numbers in Java.
Math Class random() method,
Java.util.Random Class nextInt(n) method.
2016 Winter
21. • Random() method returns a value greater than 0.0 and
less than 1.0. With a simple calculation we can generate
numbers for any range:
int range = (maximumValue - minimumValue) + 1;
int value = (int)(Math.random()*range) + minimumValue;
• Example (A random number between 1 and 5):
int range = (5 - 1) + 1;
int value = (int)(Math.random()*range) + 1;
2016 Winter
22. • Method nextInt(int n) which returns an int value
between zero (inclusive) and n (exclusive).
• Basically, this approach is cleaner and easier to read
to get random number of a given type uniformly
distributed over a range of your chance.
• Example (A random number between 1 and 5):
Random generator = new Random();
int value = generator.nextInt(5) + 1;
Comparison of the mentioned random number generator methods.
http://stackoverflow.com/questions/738629/math-random-versus-random-nextintint
2016 Winter
23. • You are given 4 classes to demonstrate a 3 digit key
generator and a test program that tries to find that
key in a number of trials.
• Combination class has some missing parts and needs
to be completed.
• Use the given Test.java to run your program.
• Please check the answer AFTER attempting to solve.
2016 Winter
24. public class Quiz {
public static void main(String[] args) {
String s = null;
if (s.length() > 0)
System.out.println(s);
else
System.out.println("empty string");
}
}
Which of the following statements is true for the program above?
1. The program runs without error, but displays nothing on the console.
2. Displays a message of the form “String@5e8fce95” on the console.
3. Displays “empty string” on the console.
4. Produces a compile-time error.
5. Produces a run-time error:
Exception in thread "main" java.lang.NullPointerException
at Quiz.main(Quiz.java:4)