Delving Into JAVA for the First Time
I learnt some great Python programming skills via Coursera last year (and a bit of R programming too, although that felt too much like work so I’ve knocked R on the head for the time being). I’ve been busy in the background applying that Python coding knowledge to various web scraping & data mining/data analysis projects.
But what’s up next for me in 2017 as part of my wider career-change programme & associated Personalised Training Program?
Well, if you’ve been follow the Systems Thinking category you’ll know I’m studying Systems Theory (or at least one approach within the bigger Systems field, called Systemic Inquiry) with the Open University. That’s a formal post-graduate course, with assignments and deadlines and all that good stuff, so it’s taking up quite a bit of my time. I’m also working on the Udacity Nanodegree in Machine Learning. Can’t quite believe I still find time to do a finance job as well as all that (thank god it’s only part-time!) but it keeps the cash flow flowing and helps take the pressure off and allows me the luxury (actually, I would describe it a necessity) of retraining into a new field.
But one other item on my original PTP (Programming Stream) was to learn Java programming. I need to get that under my belt if I want to progress onto the next OU post-graduate module on my list: M813 Software Development. So I booked onto the best-selling Java Developer course on Udemy, and it’s time to crack on with studying that now.
(All posts in this Java section of my PTP Programming Stream can be accessed using the ‘Java’ tag.)
Java Developer course – learning outcomes
– Go from complete beginner to fluent Java programmer in c. 60 hours of training
– Learn all that’s needed to get started: downloading and installing Java and a JAVA IDE
– Understanding key concepts:
- Object Oriented Programming
- Java keywords, expressions, operators and statements
– Be able to write my own Java programs
– Be able to apply that skill to writing applications for Web, Android devices, desktop applications
I’ll be blogging about my study of this course, along with any projects and programs I work on as I go.
Downloading Java (Windows environment)
For this course we need to download the Java Development Kit (JDK) which is a popular Java development platform comprising a set of programming tools and a run-time environment. The JDK provides “tools for developing, debugging, and monitoring Java applications” (Oracle, cited on Wikipedia).
The Java JDK download is freely available from Oracle here – choose the JDK download option to get to the Java SE Development Kit 8 Downloads page (you might need to agree to the Cookies policy to get there). Click on the button to accept the Licence Agreement then choose the relevant download file. For Windows environment, this will be either the 32-bit (called x86 by Oracle) or 64-bit.* The Java installation wizard offers various install options but I’m just choosing the defaults here.
* To check whether you’re running 32-bit or 64-bit, click on the Start button & search for ‘About this PC’ or ‘System’, then click on the link that comes up for ‘System’ – the icon is a computer monitor with a tick in it which opens a dialogue box which will tell you which version of Windows O/s your machine is running (in my case it’s windows 8.1) and also the System type (either 32-bit or 64-bit Operating System).
Downloading a Integrated Developer Environment (IDE)
There’s a whole host of free IDEs for Java – a useful blog post on the subject can be found here. The course I’m following on Udemy recommends IntelliJ from Jetbrains, which is either available free (the Community edition) or the paid edition, although other IDEs are available – IntelliJ, NetBeans and Eclipse appear to be the most popular choices. Another blog post here suggests that Eclipse can become ‘confusing’ due to its ‘endless plugins’. Since I really want to keep my learning environment as clean and simple as I can, that probably rules out Eclipse. NetBeans is said to have a great community of developers associated with it, which I can see would be an attraction as one gets more advanced. As this is all very new to me it’s probably easiest for me to go with the recommended platform for this course.
Heading on over to their website >> JetBrains IDEA >> it looks kinda cool. I love their interactive ‘cookie policy’ too! And it does help that we can get a free 90 day trial period of the full IntelliJ ultimate edition as part of this Udemy Java Developer course. I’ve downloaded and installed IDEA and, during the install, associated it with .java. I’ve also changed settings to configure for Java by associating it with the Java Development Kit installed above. Which means I’m ready to get going and create my very first java application.
The Udemy Learning Environment
I thought it’s worth mentioning at this stage how impressed I am with the Udemy learning platform too. It’s very clean and modern and a better feel to it so far than Coursera (although I’m not going to knock Coursera at all because some of their courses and learning programmes are awesome and I’ve learned a lot over there). I’ve not suffered any software glitches so far and I’m not really expecting to either from these first impressions. I also find both the dashboard and emails to be very welcoming and encouraging, really helping me stay engaged with the platform and a particular course. OK, so onward with the learning!
Hello Java
To write my first mini Java program, I fire up IntelliJ IDEA & create a new project using Java (make sure the Project SDK box lists the Java Development Kit you have installed). I set up a new project from scratch (rather than using a Template) called HelloWorld and save in a suitable directory. A default directory is created by IDEA for Java projects but I prefer to keep everything on the Desktop for easier access so create a new directory called Java_prog then save my first program in a suitably named project sub-directory.
IDEA creates the project directory and associated files then brings up the project window. Click on the expand triangle against the project name HelloWorld to expand the selections: .idea folder, scr (source code) folder and HelloWorld.iml file (used by IDEA for tracking project status).
Because Java is an object-oriented programming language, it is heavy on classes which in layman’s terms are sets or collections of things which have the same property. So there might be a class called carwheel and each of the wheels on all of the cars produced by a factory are an instance of the carwheel class. See the Java documentation on classes on the Oracle website here. Create a new Java class in IDEA by right-clicking on the src folder, selecting New then Java Class and in the dialogue box name the new class Hello.
The new class Hello will sit under the src folder and will include the default code:
/** * Created by *username* on 24/01/2017. */ public class Hello { }
We can now add in the relevant code for our first mini program HelloWorld inside the curly brackets of the Hello class:
/** * Created by Deborah K on 24/01/2017. */ public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); } }
IntelliJ is pretty neat for allowing this copy+paste while preserving some formatting – shame it doesn’t allow for the same formatting as shown in the IDE itself . Another cool thing to note about using an IDE (yes, it’s my first time so I am going to marvel over these things) is that it enters closing brackets, closing double quotes, etc as you go along, which is much easier than typing raw into Notepad++ as I was doing last year with Python.
Once we’ve finished typing in the program code, it’s easy enough to run from directly in the IDE window pane: just right-click and select Run (the menu option shows the standard green play triangle + Run Hello.main). Running this for the first time on my PC brought up a Firewall security warning: I clicked OK to allow IntelliJ to communicate via my private network – the default option). On successfully running the code, IDEA opens a new execute pane at the bottom of the screen to show the results of the execution:
"C:\Program Files\Java\jdk1.8.0_111\bin\java" ... Hello World! Process finished with exit code 0
So this was my first (stupidly simple) Java application, which ran successfully, proving my Java set up is all working perfectly.
A Quick Tour of IntelliJ IDEA
The src folder in our project is the location for the Java source code we write for our program. For example by creating a new class called Hello, IDEA created a new file for our Java application called Hello.java (note the .java file extension). We can navigate to this file using Windows File Manager and may be viewed using other environments such as Notepad++. However, it is recommended that programs created within IDEA are only ever editied within IDEA itself.
Within the Out sub-directory (see filepath: Out\Production\HelloWorld\~) there is also a file created by IDEA called HelloWorld.class. The .class file is the compiled version of the program we created for our project. This compiled version of a program is the one which should be distributed to clients or public who are going to use the program (as opposed to the base source code version).
We’ve seen already that IDEA automatically formats the code as we type, and auto-completes relevant close brackets, etc. It also automatically saves the files in the project. But you can also manually save at any time by choosing from the drop-down menu File then Save All.
The three panes in the IDEA environment are the project file locations menu on the left and the program pane defaulted to middle/right at the top with the execution pane defaulting to the bottom. It is possible to reconfigure the locations of the execution pane by clicking on the Settings cog icon at the top right of that pane and selecting the required option (e.g. Move to > Right, or Resize, etc). Clicking the button to the right of the settings cog – the little arrow pointing down to a bar – collapses the execution pane entirely (until the next time you Run the program again). The borders of all three panes can also be moved by hovering over the border, clicking and dragging to resize (similar to resizing a column in Excel).
Panes can also be viewed or hidden via the View drop-down menu, selecting Tools Menu then toggling the relevant pane(s) on or off. It’s also possible to set the panes to Floating, again choosing the relevant option in the Settings (cog icon) menu for that pane. I prefer the default layout as this is the set-up I’ve got used to in SQL IDEs at work, but who knows – that may change with more developer experience. If you ever want to save a custom pane set-up, just choose menu item Window then Store Current Layout as Default to set this as your new default layout. You can always use Window then Restore Default Layout to put everything back to the default again.
The drop-down menus also provide hot-keys which in Windows are Alt+{a number}. So these are worth exploring and getting to know for common commands, as is just reviewing the various different menu options.
This has just been a very quick intro to setting up and using Java for the first time. There’s lots more to learn and the course I’m studying (see reference below) moves on to Java Variables, Data types and Operators next.
References:
The Complete Java Developer Course by Tim Buchalka available on Udemy