Welcome to “Java Diaries” a series in which I document my self-teaching of Java as both a record of what I’ve done and a reference for me to check back on later – hopefully aiding the information I’ve been consuming to sink in. Click here to view all posts in this series.
In between packing up my flat last night for a move I managed to finish off the “Primitive Data Type” section of the Udemy course. In the first part of the Java Primitive Data Types section we covered:
And in this second part we covered:
“Primitive Data Types” are defined by Java and are called by using a keyword (such as byte, int, etc) and then you name them and give them a value, either a literal value or an expression. In future lessons of the course, I understand that we will learn how to create our own data types but for now we have only learnt the primitive data types.
Floats are single precision data types. At first I thought this meant that a Float can only hold a single decimal point but in fact they can hold 7 decimal points such as: 1.7654321. You denote float numbers with an “f” in the same you can denote a long with an “L“. Floats have a width of 32 (or 4 bytes) which is the same as an Int.
The syntax for a Float is as follows:
float myFloatValue = 5f
In the above code we have defined a float and named it myFloatValue and gave it a literal value of 5f.
We can use floats to perform equations using either whole numbers or decimal numbers. For example if we ran:
float myFloatValue = 5f / 3f;
Then the returned result would be: 1.6666666. Even though we used whole numbers, a decimal number was returned.
As the name suggests Doubles hold double the data of Floats. This means that a Double is a double precision data type, with a width of 64 (or 8 bytes). Strangely though, whereas a Float has 7 decimal spaces, a Double has 16 decimal places (as opposed to the 14 you may assume).
The syntax for a Double is as follows:
double myDoubleValue = 3d;
As you can see we have defined a double with the name myDoubleValue and given it a literal value of 3d. In the same way we denote floats with an “f” we can denote doubles with a “d”.
double myDoubleValue = 5d / 3d;
If we do the same equations as we did with the float but using doubles instead the returned value is: 1.6666666666666667.
Despite a Double taking up twice as much memory as a float Tim, the course teacher, states the following three things as a compelling reason to use Doubles instead of floats.
- Double precision data types are calculated faster on modern computers.
- Many inbuilt functions of Java use doubles by default (the same way some use integers as the default).
- Doubles are inherently more precise, allowing for a greater range.
At this point in the lesson, Tim set a challenge. The task was to create a weight conversion program which converted a set number of pounds to kilograms using the knowledge that 1 kilo is equal to 0.45359237d pounds.
For me, the code wasn’t the problem here it was more poor math skills. At first I did the following:
double poundValue = 10d; double kilogramValue = 0.45359237d; double conversion = poundValue / kilogramValue; System.out.println(poundValue + " pounds equals " + conversion + " kilograms.");
What I wrote here was pretty much correct except the fact that you wouldn’t divide (/) the poundValue by kilogramValue you would instead multiply (*) the values – this was more a math mistake as opposed to a programming mistake as the code in this current form still ran, it was just wrong mathematically. Once I ran it and realised the output was wrong I changed it from divide to multiply and it worked – I used the following code.
//CHALLENGE - Write a program that converts pounds to kilograms. //Stores the number of pounds double poundValue = 10d; //Stores the value of how many kilos are = to 1 pound. double kilogramValue = 0.45359237d; double conversion = poundValue * kilogramValue; //Prints out the result System.out.println(poundValue + " pounds equals " + conversion + " kilograms.");
This ran correctly and produced the correct result but I used one more line of code needed. Instead of defining a double named kilogramValue I could have just done the following equation in the double named conversion:
double conversion = poundValue * 0.45359237d;
Doing this would have removed a line of code, making for cleaner, more efficient code. Something to remember for the future!
After this challenge we moved on to the last two primitive data types: Chars & Booleans.
The next data type the lessons covered was the char type which I believe stands for character. This data type can only store 1 character, and this character can be a letter, number, special character or unicode.
The syntax for a char is as follows:
char myChar = 'D';
If we did a system print out of myChar the character D would be returned. Quite simple to understand! Notice the syntax uses single quotes (‘ ‘) and not double quotes (” “).
As mentioned, we can also use Unicode which can return special symbols such as the copyright symbol, when using Unicode symbols the syntax would be:
char myChar = '\u00A9';
In this case, we used \u to tell the program that we were using Unicode. If we did a system print out of this a © would be returned. A list of unicode characters can be found here – https://unicode-table.com/en/#control-character
There was another challenge at this point, which was to use the unicode chart at the above link to find the character for the registered trademark symbol, ®, which was the same as the copyright, but using a different unicode reference.
//CHALLENGE - Display a registered symbol on screen. char registeredSign = '\u00AE'; System.out.println("Second Unicode output was: " + registeredSign);
Next up was the boolean data type, not a lot of time was spent on this in the course but essentially booleans can have one of two values: true or false.
if myBoolean = true then run this piece of code else run this piece of code
The syntax in that example is non-existent as I don’t know what the syntax for Java else/if statements is yet but hopefully you get the idea. You define a boolean like so:
boolean isHuman = true;
Last but not least are strings.
Whereas a Char can only hold one character, Strings can hold many. You define a String like so:
String myString = "This is a string"; System.out.println("myString is equal to: " + myString);
Notice String has a capital “S”, I found without it the program didn’t run.
If we ran the above code the output would be:
myString is equal to: This is a string
You can append other data types to Strings using the + operator, for example:
String myString = "This is a string"; System.out.println("myString is equal to: " + myString); // You can append Strings with text. myString = myString + ", and this is more."; System.out.println("myString appended is equal to: " + myString); // You can append with Unicode characters too. myString = myString + " \u00A9 2018"; System.out.println("myString copyrighted is equal to: " + myString); String numberString = "250.55"; numberString = numberString + "49.95"; // Strings don't support numeric operations, it treats numbers as text only. System.out.println("The result is " + numberString); String lastString = "10"; int myInt = 50; lastString = lastString + myInt; // Because strings are treated as text, so is myInt System.out.println("My last string is " + lastString);
When running the above code the result would be:
myString appended is equal to: This is a string, and this is more. myString copyrighted is equal to: This is a string, and this is more. © 2018 The result is 250.5549.95 My last string is 1050
There are a few things you may notice in the above code:
- Strings can’t be used in mathematical equations* so appending other data types such as Int will end up with the Int being converted to a String.
- You can use Unicode characters in strings directly, with no need to use a Char variable.
*Apparently there are functions which can convert strings to other data types which do allow mathematical equations, but by default you can’t do this.
And that’s a wrap on Primitive Data Types – next up is “Operators in Java” and I’m looking forward to it!