Java for Beginners 6 - Primitive Data Types: Declaration Examples
Lecture 6: Primitive Data Types: Declaration Examples
Primitive Types example problem statement
Declare 4 of Java's 8 primitive data types(byte, short, int, long, float, double, char, boolean)
Primitive Types example solution
- int intValue;
- double doubleValue;
- char charValue;
- boolean booleanValue;
Hi there, I am Marius from AlefTav Coding. In this lecture you will learn how to declare four of Java's 8 primitive data types.
In a Java program there are many storage locations where different pieces of data are stored. Each one of these storage locations must first be declared before your Java program can use it. The declaration pattern in Java is: dataType storageName. You first identify the type of data that can be stored at a location. Then you give that storage location a name. So that you can refer to that storage area whenever you want to work with the data stored there. This is called a declaration.
Java has two main ways of storing information. These two ways are Reference types, which you will learn about later on in this course, and Primitive types.
public class Lecture6 {
public static void main(String[] args) {
// TODO: Declare 4 of Java's 8 primitive data types
}
}
Primitive data types are storage locations where the name of the location AND the value being stored there are the same. For example, my first declaration in the code above is a storage area where a discrete, whole number can be stored. The data type is int and the storageName is intValue .
Below that is another area, but this time it can only store decimal numbers. This is indicated by the period in the number. The dataType is double and the storage name is doubleValue. This is followed by a third location, where a single character can be stored. This is indicated by the two single quotation marks. The data type being char and the storage name charValue. Finally, I have a location which can store only a value that is true or a value that is false. Here, the data type is boolean and the storage name is booleanValue.
In my code above I created a class, Lecture6 with a main method. My problem statement requires me to declare 4 of Java's 8 primitive data types. The 8 data types are: byte, short, int, long, float, double, boolean and char. My examples above use Java's declaration pattern: dataType storageName.
For working with whole numbers, positive and negative, Java provides the following four from the smallest size to the largest size:
- byte
- short
- int
- long
Then, for working with decimal numbers, again positive or negative, java provides the following two:
- float
- double
For defining a single character, Java provides the type, char. Finally, for working with values that can either be true or false, Java defines the data type, boolean
In this lecture you learned how to declare a primitive data type in Java. Now, complete this coding exercise:
Primitive type declaration coding exercise
Declare a char data type, named letterY
public class Lecture7 {
public static void main(String[] args) {
// TODO: Declare a 'char' data type, named '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