Learning a new programming language

Learning Java for the first time can be very tough and often confusing; it can be especially difficult to take the theory and use it to create actual solutions to real problems. This empirical, problem-solving aspect of programming comes easily to some but very laboriously to others, including me. From personal experience, I can tell you that, alas, there's not much that can be done about it except keep plugging away.

The more you read, the more exercises you do, the more it'll start to become comprehensible. As in a lot of math and physics, I'm afraid it takes a lot of practice problems from many different perspectives before the practical application starts to crystallize. Until then, your understanding of the theory can become quite good but the ability to create empirical applications of the theory will require lots of practice.

It can be very tough going but I think you'll find it worthwhile! For programming, in particular, I think creating an outline of the program, either in pseudocode or in a flowchart, can very much help in working out the algorithm, or step-by-step solution to the problem with which you're wrestling.

Finally, when you're writing your programs, or expanding other programs, be wary of using constructs you don't really understand. This approach often leads to the most difficult errors to find in a program, semantic errors. If you try to hack something you don't really understand, it's very likely to lead to incredibly frustrating errors and frustration is something you should avoid at all costs. If frustration sets in, please do pause and post on the forums. Pseudocode can work at any level of granularity, from an eagle's eye overview to very fine detail, and a good rule of thumb is if you cannot express something in pseudocode, you likely do not really understand it and should not use it. This is an instance of Occam's Razor and leads to a simple, and often very effective, approach.

Practice, Practice, Practice... and even more Practice!

Practicing programming by attempting tons of problems from the book or from online sites is one of the best ways to learn a programming language. I might even argue practice is the only way, really, as reading only gives you a cursory knowledge and it's when you apply the ideas in real programs that the real learning occurs. As such, I link to a ton of great practice sites in the reference page I maintain for us here:


http://www.sethi.org/tutorials/references_java.shtml

But the best frustrate yourself while working on a program is to make random changes without understanding them. Beginning to learn programming can be very confusing and difficult at first but one tip I might offer is to not make any changes unless you know what you expect that change to do precisely; often, when you find code online, if you use it without truly knowing what each line does, it will only make matters worse.

For First-Time Java Programmers

Students who are learning a programming language for the first time usually make the mistake of writing the program all at once with all the requirements specified in the Lab. Then, they find a lot of compilation errors and don't know where to start fixing the errors. You should only follow this approach if you have some experience programming in that language and feel comfortable catching all the errors at the same time.

In general, a very helpful approach is to code small and test often. In this approach, you should start with a few lines of code that represent one complete thought or extension of your existing code. You can then add this code, as well as some test code to ensure it fulfills its function. This way, you minimize not only syntax errors (by compiling and running successfully) but also greatly reduce the semantic errors (the testing code ensures that your logic is correct, as well).

For beginning programmers, you should try the following step-by-step programming approach:

  1. Start with a very basic program that you can compile, and run successfully.
  2. Modify your program by adding classes, methods, objects, etc. to meet additional lab requirements. Then, compile and run the modified program, fixing any errors you may have.
  3. Repeat Step 2 as many times as needed until you get the program you want.

Asking Questions in Online Courses

Learning programming online can be very confusing but, as you might soon surmise, not all courses are a good fit for everyone to take online. Online learning comes with distinct advantages and disadvantages, I'm afraid, and I talk about that at length here:

http://www.sethi.org/tutorials/faq.shtml#Learning 

One of the best tips I can offer is to try to engage some colleagues or a tutor at your local campus. If neither of those are options, I think starting with the basics of classes is a good way to go. Instead of randomly making changes or moving things around, it's better to work in a structured, systematic manner.

In order to make these precise, methodical changes, you have to establish the foundational principles of a language. In Java, the fundamental component of the language is the concept of a class. You have to master the basics of the syntax and structure of a class.

Object-Oriented Programming

Object-Oriented Programming emphasizes the use of objects. Objects encapsulate properties (data) and the behaviours (functions) that work with those properties into a single entity. These self-contained components are the main building blocks of OO languages and determine the behavior of the program by sending and receiving events or messages between the objects.

A class: is a blueprint or model of the variables (data) and methods (functions) that define a certain type of object. It is a language construct that defines how that category of objects behaves.

Abstraction vs Information Hiding vs Encapsulation

The difference between abstraction, information hiding, and encapsulation is nicely summarized here: http://web.archive.org/web/20080906224409/http://www.itmweb.com/essay550.htm and here: https://stackoverflow.com/questions/24626/abstraction-vs-information-hiding-vs-encapsulation/8694874#8694874 

Abstraction:

"One point of confusion regarding abstraction is its use as both a process and an entity. Abstraction, as a process, denotes the extracting of the essential details about an item, or a group of items, while ignoring the inessential details. Abstraction, as an entity, denotes a model, a view, or some other focused representation for an actual item. Abstraction is most often used as a complexity mastering technique. For example, we often hear people say such things as: "just give me the highlights" or "just the facts, please." What these people are asking for are abstractions."

Information Hiding:

"Abstraction can be (and often is) used as a technique for identifying which information should be hidden. For example, in functional abstraction we might say that it is important to be able to add items to a list, but the details of how that is accomplished are not of interest and should be hidden. Using data abstraction, we would say that a list is a place where we can store information, but how the list is actually implemented (e.g., as an array or as a series of linked locations) is unimportant and should be hidden.

Confusion can occur when people fail to distinguish between the hiding of information, and a technique (e.g., abstraction) that is used to help identify which information is to be hidden."

Encapsulation:

"As a process, encapsulation means the act of enclosing one or more items within a […] container. Encapsulation, as an entity, refers to a package or an enclosure that holds (contains, encloses) one or more items."

Java Class and Program Structure

A java class consists of 3 components: functions, variables, and values, just as in mathematics.

  1. A function is also called a method or behaviour
  2. variables come in two flavours: local variables
  3. or instance variables (also called attributes or state)

The basic structure of a java class requires 5 elements:

When you put all this together in a Java program, the minimal structure of a Java program looks like this (from here):

Basic Structure of a Java program

Non-Beginner Java OOP

As you start to write more advanced Java programs, you'll need to create your own custom classes which you'll use to create objects; here, the class works like a cookie cutter which you can use to create many cookie objects. There is a great image of this process here:

A class that you define consists of three parts: an Identifier or name, some Attributes or instance variables, and some Behaviours or methods/functions. This looks like the image from here (please ignore the word Static below):

And here are some examples of classes in this UML diagram format:


Ricky J. Sethi, PhD <rickys@sethi.org>
Last updated: Friday, August 24 2018
(sethi.org/tutorials/learning-java.shtml)