When you first start learning Java, there are quite a few concepts that may seem foreign to you. One of the most important is the concept of Object Oriented Programming; one of many important subjects that are essential to successfully learning Java programming.
If you have no previous Java programming experience, Java for Absolute Beginners is a great place to get your feet wet while learning by example. In this tutorial, you will learn some of the core concepts inherent to the Java programming language. Without a solid foundation in these concepts, you’re unlikely to be successful with Java or most other programming languages for that matter.
The great thing about learning to program in Java is that there are excellent classes available and a large developer community willing to help should you run into issues along the way. If you get stuck on a particular statement, chances are that hundreds or even thousands of others have been stuck in the same spot before you and a quick Internet search should net you plenty of solutions for your problem.
Whether you have zero programming experience or are just new to Java (and have experience in another language), you will be well served to learn the concepts contained within this article because you will rely on them in practically every line of code you write for your Java applications.
All computer programs, regardless of the programming language they are written in, read data from somewhere (a file, the keyboard, the mouse, the Internet, etc.), process the data, and then write the data somewhere else such as the computer monitor or a database file on the hard drive.
In Java, like most other programming languages, data is kept in variables. Yes variables – like X and Y and all those other letters you thought you would never use again in high school algebra. As you create Java programs, you have to declare variables, assign values to them, and execute operations using these variables.
Every variable in a Java program has a certain data type. The data type determines what type of data can be contained within that variable and what operations can be executed on the variable. A variable could be a number. Numbers are further broken down into categories based on their size. A variable can also be a string of text. Although you cannot perform mathematical equations on strings, you can use the “+” operator to concatenate two or more strings together.
Similar to punching keys on a calculator, your Java programs rely on operators (+, -, *, /, ?, and a host of others) to work with the variables in your program. Operators can be used to assign value to variables, perform mathematical functions, and create new objects based on variables.
Operators are also used for conditional logic statements such as for loops, while loops, if statements, switch statements, and method calls. Although some of these terms may seem foreign to you, you can learn more about them in the Learn Java from Scratch course.
Classes & Objects
Classes are designed to group variables and operations together in coherent modules. Classes have fields, constructors and methods among other attributes. Objects are nothing more than specific instances of a class. This is Object Oriented Programming in a nutshell.
The best way to think about classes and objects is to think about cars. The Car Class contains all possible cars within it. All cars share certain attributes such as having four wheels, an engine, a transmission, seatbelts, a steering wheel, etc. When you create an object, it is a specific instance of a car. By default, this object takes on all of the attributes of the Car Class and it can also have its own attributes specific to that particular instance. This is known as inheritance.
If you don’t understand all of it just yet, don’t worry – it will come to you. The point is simply that object oriented programming allows you to inherit certain attributes of a class without having to write that individual code every time you want to use it in your program. In other words, you don’t have to tell your program that a Car object has wheels, a steering wheel, an engine, a transmission, etc. because the program already knows these things because of the attributes defined within the Car Class.
A field is a variable that belongs to a class or object. For instance, the Car Class could define a field called brand. All Car objects would have this brand field, but the value of this field can be different for each instance of the class. This allows Car objects to be different from one another while still sharing the basic attributes inherent to all cars.
A constructor is a special type of method that is executed when an object is created. The purpose of constructors is to assign values to the fields that are specific to that particular instance of the object. For instance, if you are creating a Car object, you may need to tell the program what brand the car is, what color it is, how fast it is going, and in what direction it is traveling. A constructor allows you to do these things easily when the object is created during program execution.
A method is a group of operations that carry out a certain function together. Methods take advantage of another object-oriented programming concept known as encapsulation. Using the car example once again, imagine there is a method for making the car go faster. As the driver, all you know is that when you press the gas pedal, the car goes faster. You do not need to know (nor do you care) how the car goes faster; simply that it goes faster. Encapsulation means that the inner workings of a particular method are hidden from the rest of the program and other methods within the program. This protects the integrity of data no matter how complex your program may be. Within the Accelerate method, there could be a set of instructions for the Car object including increasing fuel, allowing more air into the engine, and tightening the steering wheel to compensate for the increased speed and torque.
Methods are what actually do the “work” in your programs. Just like classes and objects, methods only have to be written in your program once. After that, you simply need to call that method to access its internal functions and it can output the desired result back to the calling method and the program can continue with execution.
Creating useful and functional methods is a very important part of successful Java programming. You can learn more about creating methods in The Ultimate Java Tutorial.
Certainly you have noticed that this article does not include any programming code whatsoever. This is intentional because these core concepts are inherent to just about every object oriented programming language and must be understood before you can actually start writing your own code.
Once you have a basic understanding of these principles, you can start creating your own Java programs using the Introduction to Java Training Course. Many of these concepts may seem slightly abstract to you now, but once you start writing your own code, they will begin to make much more sense and you will realize the value of object oriented programming and the Java programming language for your own future creations.