Lecture 5 – Primitive Data Types Example

Primitive types problem statement

Implement a ‘double’ data type, ‘doubleNumber’, initialized as 9781.3

Primitive types example solution

double doubleNumber = 9781.3;

Primitive types example output

9781.3

Problem statement

Hi there, I am Marius from AlefTav Coding.  In this lecture I will look at an example of Java’s primitive data types.  You will learn how to Implement a ‘double’ data type, named, ‘doubleNumber’, initialized as 9781.3′

public class Lecture5 {
public static void main(String[] args) {
// TODO: Implement a ‘double’ data type, ‘doubleNumber’, initialized as 9781.3
System.out.println(“doubleNumber”);
}
}

How Java stores information

Inside your computer there are many storage locations where different pieces of data are stored. You as a Java developer must first prepare each one of these storage locations before your Java program can use it. Technically, you must first declare a storage location. This declaration pattern in Java is to identify the type of data that can be stored at a location. Then you give that storage location a name. In this example, the data type is double and the storage name is ‘doubleNumber’.

Now, after you have told Java what type of data to store at a location, the next step is to put a value at that location. The  equals symbol,  = means to assign a value.  You assign the value 9781.3 to the location named, ‘doubleNumber’.  The technical term for this step is initialization.  I have combined the declaration step and initialization step into one as follows: double doubleNumber = 9781.3;

The reason I am using the Java data type, double is because here I work with a fractional number, 9781.3. The data type, double is one of Java’s primitive data types.  Java allocates a set storage size for each of the different primitive data types. Because Java knows the sizes at the time of declaration, it will store primitive types at those locations.

My class in this lesson is, Lecture5 with the usual main() method.  The solution code must be typed in the line with the TODO comment.  After that, I do a printout. So, I instruct Java to print the value stored at the location named ‘doubleNumber’.

// TODO: Implement a ‘double’ data type, ‘doubleNumber’, initialized as 9781.3

Next, I remove the TODO comment, and I type in my solution, double doubleNumber = 9781.3;  The complete program is as follows:

public class Lecture5 {
public static void main(String[] args) {
double doubleNumber = 9781.3;
System.out.println(“doubleNumber”);
}
}

When I run this program the value, 9781.3 will be displayed, indicating that my program works correctly.

Coding Exercise

In this lecture you learnt how to implement the primitive data type, double.  Below is a coding exercise for you to complete as homework:

public class Lecture6 {
public static void main(String[] args) {
// TODO: Implement an ‘int’ data type, intValue, initialized as 4326
System.out.println(intValue);
}
}

My name is Marius from AlefTav Coding.  Thanks for reading and KEEP ON CODING.

4 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 5 (4 votes, average: 5.00 out of 5)
You need to be a registered member to rate this.
(38 total tokens earned)
Loading...

Responses