Java for Beginners 8 - Primitive Types - Initialization Example
Lecture 8: Primitive Types - Initialization Example
Primitive type initalization problem statement
Initialize an int dataType named 'intValue' with the number '900'Primitive Types initialization solution
int intValue = 900;
public class Lecture8 {
public static void main(String[] args) {
// TODO: Initialize an 'int' data type named 'intValue' with the value, '900'
System.out.print(intValue);
}
}
Primitive Types initialization output
900
Hi there, this is Marius from AlefTav Coding. In this lecture you will learn how to initialize a Primitive data type in Java.
In my previous lecture you learned how to declare a Primitive data type by using the pattern, dataType storageName.
Now, after you have told Java the type of data to store at a particular location, the next step is to put a value at that location. Again, you must use a Java pattern. In this pattern, storageName is equal to value. The equals symbol (=) means to assign some value to a particular storage location. You assign a value to the location, storageName. The technical term for this process is initialization.
So, you first declare a data type. Then you initialize it with a value. You can also combine these two steps into one, dataType storageName = value;
Let's look at how this works using diagrams. Here is a declaration. You have declared that at this location only whole numbers can be stored (int intValue;). As we saw in the previous lecture. Next, you intialize that location with the number, 900, a whole number. With this instruction you tell Java to put the value, 900 at the location with the storageName, intValue (int intValue = 900;).
Here, above, I have declared that at this location only single characters can be stored (char charValue;). Then I intialize that location with a single character, the lowercase letter a. Java will then obediently put the value a at the location with the storage name, charValue (char charValue = 'a';).
public class Lecture8 {
public static void main(String[] args) {
int intValue = 900;
System.out.print(intValue);
}
}
Now, let us solve the problem, where you are asked to initialize an int named intValue with the number, 900. In the expected solution we have a declaration and an initalization combined in one step. We use the pattern, data type as int followed by the storage name as intValue. This is followed by the intialization. Assigning the data type intValue with the whole number, 900.
In this program you have a class, Lecture8 with a method, main(). So, the complete solution is int intValue = 900; Next, is a print statement. When I run this program, the output is the value, 900, displayed in my output window.
In this lecture you learned to initialize a primitive data type.
Now, to practice what you have just learned, here is the coding exercise for this lecture.
Primitive type initalization coding exercise
Initialize a char data type named letterY with the value, Y. (See the starting code below)
public class Lecture9 {
public static void main(String[] args) {
// TODO: Initialize a 'char' data type named 'letterY' with the value, 'Y'
System.out.print(letterY);
}
}
My name is Marius from AlefTav Coding. Till next time, KEEP CODING.
Please upvote, re-steem and share. For more of this you can find me at the following links.
https://busy.org/@mariusclaassen
https://steemit.com/@mariusclaassen
https://d.tube/#!/c/mariusclaassen
https://www.udemy.com/java-8-for-complete-beginners/learn/v4/overview