Outdoors, Green Living, Homesteading, Sustainable living, Green Building

Learn to Code Java!

How easy is it to begin learning Java? I will explain steps needed to get started. What tools are needed? Which tools to avoid at first. Ask yourself a few questions. Why do you want to learn Java? What are your main goals in learning Java? What do you want Java to do for you? What kinds of problems do you want it to solve? You can use Java to code console apps, desktop apps, web apps, phone/tablet apps etc. Are you learning it because of school or because of work? For hobby? For self-education?

  • Get the JDK (Java Development Kit)
  • Get a console or command line, either Linux shell or Windows shell.
  • Get a text editor JEdit, Atom or Editpad Pro.
  • Get a java.exe interpreter and javac.exe compiler.
  • Get a google, and with a google get an Oracle Java documentation or tutorial.
  • Get a HelloWorld.java example.
  • Consider getting Ant build tool, though it’s not necessary for learning and I don’t use it for simple things, it’s a good tool to begin to become familiar with. And it’s easy to use at the command line.

This is all you need to begin learning Java but there are a couple of more steps in getting started.

  • add JDK bin folder to the system path variable.
  • set the classpath variable to ‘.’ current folder.

Other tips I have, make a c:\dev\java\work  folder to put things in as you learn. If you do not know how to use the console begin to learn simple things. Even learn to make a simple batch file on Windows or shell scripts on Linux. You can use Windows notepad.exe or even the vi editor on Linux if you wish to begin with, no text editor need be installed. I install jdk under c:\dev\java\jdk9  where 9 is the version major version number. so my bin folder is in c:\dev\java\jdk9\bin  which is what you need on the path so that you may use the command line tools. Ant will also have to be added to the path environment variable.

So pull up the editor, get the hello world example text and paste it into a blank buffer, save it as HelloWorld.java and then you will see that the editor should highlight the syntax in color for you. If not then you need to search for how to turn on syntax highlighting. It’s not necessary but it is nice. Sorry, no highlighting for Notepad users. Next compile with javac HelloWorld.java, if no errors then run with java HelloWorld. If all went well, then you should see something like “Hello World!” output to console. Congratulations you are now a Java programmer!

The next thing to learn will be more about System.out.println() or other System.out methods.  You can also use the Java Scanner class to get input from the command line easily.  Sorry, no color at the windows consoles at least. That is without using a 3rd party library such as JCurses.

You can code GUI apps with text editors and you should. Except for phone/tablet apps because they are a bit more complex you will need to install the Android Development Kit and Studio. If you want to learn web development then download Tomcat web server and run it. With Tomcat learn Servlets and very basic Java Server Pages. Learn Java Server Faces and Facelets. Learn Spring and Hibernate. And don’t expect to learn all of that in a day. I’d say a year of constant hard work maybe. You will also need to learn HTML 5, CSS 3 and at least some Javascript would be nice. Firebug is a good plugin for Javascript. And you will need several browsers for testing and development and testing. Then pick one probably Firefox for main development browser.

But for graphical fun just use java AWT, and Swing API’s. And begin to learn the replacement for those called JavaFX. And you can do this with text editors and command line tools and should.  I recommend coding applets for starting GUI and use appletviewer.exe tool to run them. Applets make it easy to get started drawing some graphics, using color and playing sound. You can move on after you get little better at GUI coding to Java frames for desktop and Java Web Start apps, which are basically the web replacement for applets.

For databases get SQLite or H2, find the appropriate JDBC drivers and get the JDBC Api’s and tutorials. Get an SQL tutorial from the Google.

Finally, if you must, get you an Eclipse IDE and check it out.  Or try out IntelliJ Idea or Netbeans, or one called BlueJ. There are probably others. There is a learning curve to using any tools, and using IDE’s do not make learning to program Java easier but can do the opposite. You can get by without an IDE for much by using Jakarta Ant build tool at the command line. You simply create a build.xml file and put it int he folder with your source. Then in that folder simply type ‘ant’ and it reads that file and builds your project, it removes old class files, recompiles all source files building new class files. It can build javadocs with the javadoc.exe tool which is the java documentation from your project and much more. It can even create jar archive files and deploy them to websites.

If you want to help out on opensource projects you will need to get you a GIT or SVN tool installed. CVS was the old tool and may still be used on some projects. There is a learning curve to using these tools so be prepared. Once you pass over a few hurdles its not bad at all.

The Java language comes with some very simple testing functionality. But you may want to look into testing frameworks such as JUnit for Java. There is one for javascript as well. You can write your own test without that, these are called drivers.  You simply make a class that sole purpose is to create an object and call its methods to test it out.

First things first. Learn Objects first. In Java, everything is an Object. The quicker you learn Java OOP the better. And let me warn you that Javascript OOP is similar but different and somewhat confusing compared to Java OOP.  Just begin some basic class definitions with constructors, methods, and fields. Make everything public ( a bad habit you will break later) and don’t use static class members, to begin with.  Later you will learn about inner classes, local classes, and anonymous classes. Anonymous inner classes are the more practical and useful. Though you are better off just making outer classes for now. You should always just put one class in one source file, it must be declared Public and it must be named the same as the source file. You can put other non-public classes in the same source file, it’s just not normally recommended because it can make it harder to find a class in amongst many source files when you go to look for it.  You will want to learn about Interfaces and Abstract classes and methods fairly quickly.

Public access means a class member can be seen by any other class anywhere, in the same package or others. Private means a member can only be seen by the class that it is a member of, not even sub-classes. You will need to learn about inheritance and polymorphism fairly soon too.  If a member has no access modifier then it can be seen by all classes in the same package (folder).  BTW ‘.’ is the current folder which is the default package in Java. If you include no package statement then the class is in this default package. Import in Java simply imports the name so that you do not have to use the full path to the class name such as java.awt.Button. If you import it then all you need to use Is Button. And java.lang.* is automatically imported in all classes. So you do not have to use java.lang.Integer, for example, only Integer and with no import java.lang.*; or import java.lang.Integer; For classes to be available to use in your app all that is needed is that they are found in the classpath.  So look into how to add classes or archives jar files to the classpath. There are a number of ways to do this.

I hope this is a good starting point for you if it is please let me know by emailing me admin@softwaredeveloperzone.com And read my articles at Software Developer Zone, softwaredeveloperzone.com








Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.