Java Basics 5 - Primitive Data Types Example

in #tutorial6 years ago (edited)

https://aleftavtraining.siterubix.com/

Lecture 5: Primitive Data Types Example

EXAMPLE PROBLEM STATEMENT
// Implement a ‘double’ data type, ‘doubleNumber’, initialized as 9781.3

EXAMPLE SOLUTION
// double doubleNumber = 9781.3;

// 9781.3

LO5 datatypes.jpg

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'

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 8 'primitive' data types. Java allocates a set storage size for each of the different primitive data types. Because the sizes are known at the time of declaration, primitive types are stored at those same locations.

public class Lecture5 {
public static void main(String[] args) {
// TODO: Implement ‘doubleNumber’ as 9781.3
System.out.print(intValue);
}
}

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

Next, I remove the 'TODO' comment, and I type in my solution, double doubleNumber = 9781.3;.

When I run this program the value, '9781.3' will be displayed, indicated in the one-line comment, 'Example Solution' above.

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

PRIMITIVES CODING EXERCISE:
// Implement an ‘int’ data type, intValue, initialized as 4326

My name is Marius from AlefTav Coding. Till next time, KEEP CODING.

If you found this tutorial helpful, please upvote, subscribe, like and share. You can find me at the following links.

https://www.youtube.com/channel/UCUr-H9hVVa0ulnQcqQKYmIw?disable_polymer=true
https://plus.google.com/101690479974829208565
https://www.slideshare.net/MariusClaassen1/java-the-basics?qid=4ff400a3-6e42-4e61-9d23-e48029081df6&v=&b=&from_search=5
https://www.udemy.com/course/1435298/manage/basics/

Sort:  

Hello @mariusclaassen I want you to know I liked your introductory post a lot, your java post seem to be well formatted, and you seem to be a genuine person wanting to help others and share knowledge. I have featured you in the "Pay it Forward" Contest and here is my entry into the contest.

@bashadow, I am delighted that you found my introductory post of interest. Thanks for featuring me in your contest, and will give your post an upvote.

I must admit I can't begin to wrap my head around all that. But @bashadow featured you in the pay it forward contest so I just wanted to say hi. 😊

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.031
BTC 68571.56
ETH 3910.03
USDT 1.00
SBD 3.66