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 take in. Click here to view all posts in this series.
Following on from the first post in this series I continued on with the Udemy course for an hour or so last night – I didn’t get as much done as I would have liked as I got a new laptop today so most of my spare time in the evening after work was related to getting this set-up and running. This involved redownloading IntelliJ and the Java JDK and re-configuring them.
Once this was done it was on to the lessons and this time I was learning about data types with a focus on ones which supported whole numbers. These are:
- Int (Integer)
First, what is a variable? In the course variables are described as a box which the computer can store “stuff” in. Pretty simple analogy and gets the point across. Variables can be named using a convention similar to “myVariableName”. You generally start the variable name with a lowercase word such as “my”, any words that follow this you would capitalise for readability’s sake and not include spaces such as “VariableName”. Put these two rules together and you get “myVariableName”.
Remembering the correct spelling and capatilisation of your variable names is very important to make sure the program runs error free. For example if you have “myVariableName” defined and then try to create a command using the variable but write it as “myvariableName”, the program wouldn’t work as it would see “myvariableName” (lowercase V) as something different to “myVariableName” (uppercase V).
Luckily IntelliJ IDEA has a lot of great features to help mitigate issues that could come from this, for example if I had defined a variable named “mySecondVariable”, if I was then to write a command which needed to use this variable IntelliJ would suggest an autocompletion of the word for me, which both speeds things up as well as makes sure I there are no typos when writing the code.
With variables explained it was time to learn about the data types that can be assigned to variables with a focus on those which use whole numbers (as opposed to decimal numbers). Below I’ve shown the range of each data type, the range being the minimum and maximum numbers that can be held in that data type. Think of each data type being a box as mentioned but the boxes are different sizes meaning they hold varying amounts of “stuff”.
In the code you’ll notice a comment which says “…has a width of…” this is something the tutor used to help explain that each data type has a size associated with it, which makes sense. The bigger box (to fit more data in) the heavier it will be (in this case, heavy on the computer as it has more data to filter through).
An integer has a range of -2,147,483,648 to 2,147,483, 648 to 2,147,483,648 to 2,147,483, 647. In general using Int should be the default, it has enough of a range that it can hold most numbers you’d be using and doesn’t massively tax systems when using them. Also, Java by default usually outputs equations as Int values, this can cause problems when performing a calculation inside a Byte for example, because if an Int value is returned this won’t work with the Byte data type. More on how to fix this below!
// int has a width of 32 int myMinValue = -2_147_483_648; int myMaxValue = 2_147_483_647; int myTotal = (myMinValue/2); System.out.println("myTotal = " + myTotal);
A byte has a range of -128 to 127.
// byte has a width of 8 byte myMinByteValue = -128; byte myMaxByteValue = 127; byte myNewByteValue = (byte) (myMinByteValue/2); System.out.println("myNewByteValue = " + myNewByteValue);
A short has a range of -32768 to 32767.
// short has a width of 16 short myMinShortValue = -32768; short myMaxShortValue = 32767; short myNewShortValue = (short) (myMinShortValue / 2); System.out.println("myNewShortValue = " + myNewShortValue);
A long has a range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807…I’m not even sure how I’d say this out loud. When writing data of the long type you affix an L to the end of the number. This can be either lowercase or UPPERCASE but you have to be mindful that a lowercase l at a quick glance could look like a 1 so if you had the number 1000l you could read this as 10001 – better to write 1000L. I’m not 100% sure why we would use L in the first place. From my understanding it is because writing L further helps the compiler realise that the number is a long number and not an L (though I thought the fact we’ve assigned the variable as data type long would be enough? If you know leave a comment!)
Tip: Java accepts _‘s in numbers as a way of making numbers more readable. For example you could write one million as 1000000 but you could also write it as 1_000_000 both these would be correct. You can see this in action with the below code.
// long has a width of 64 long myMinLongValue = -9_223_372_036_854_775_808L; long myMaxLongValue = 9_223_372_036_854_775_807L; System.out.println("myLongValue = " + myMinLongValue);
The Case for Casting
As mentioned earlier, when doing arithmetic in a program the result needs to fit with the data type being used. You can do arithmetic two ways, you can do it using a “literal” or an “expression“.
- A literal equation would be something using full numbers which have been defined such as “10 + (10 * 20)”.
- An expression is when you use other variables to complete the equation such as “myVariableOne + (myVariableTwo * myVariableThree)”.
But some times you might need to do an equation using other data types and you do this through “casting“.
Type casting in Java is to cast one type, a class or interface, into another type i.e. another class or interface. – JavaRevisited
Check out the code below to see casting in action:
short myMinShortValue = 20000; short myNewShortValue = (short) (myMinShortValue / 2); System.out.println("myNewShortValue = " + myNewShortValue);
In this code we see the line:
short myNewShortValue = (short) (myMinShortValue / 2);
This is creating a new short variable named myNewShortVariable. We then tell the compiler that we want myNewShortVariable to equal the outcome of the expression myMinShortValue divided (/) by 2.
You’ll notice that after the = sign the word short is repeated again in brackets. This is how casting works. Without (short) being there the result of myMinShortValue / 2 would provide an Integer value of 10000. But because we have the casting (short) this tells the compiler that we want the outcome of the upcoming equation to be of the short data type.
We can cast with bytes too like so:
byte myNewByteValue = (byte) (myMinByteValue/2);
However we don’t need to cast with Int because as discussed, the output defaults to an Int value anyway and the Long data type accepts Int values so no need to cast here either.
This covers casting on a basic level, from other bits of research I’ve done outside this course we can use casting in a lot of different ways, but this is a good way of learning to get your head around it initially.
This was all I got through in this most recent lessons but I’m looking forward to learning more as I go along! The next step is to look at the data types which hold decimal numbers (useful for programs which convert currency or measurements as a basic example). If you see anything wrong in the information above let me know in the comments and I can amend it!