Java

Java Concepts: Objects; Classes, Variables, and Methods

tl;dr (too long; didn’t read) – Java uses classes which declare variables that store information and are modified/retrieved by methods once an object of that class is instantiated. The next post will contain code complimenting these concepts. // Continue if you want to read more. /* Java is an object-oriented programming language, but just what is an object? To find out, we must first delve into the concept of classes. Classes are “templates” of objects. These “templates” dictate all the variables, or “data,” and methods, which are essentially “actions,” that the still unknown object will have. The class will have a name, along with variables and methods. The only time you may see these names is when you are instantiating an object from the class or calling (“executing”) a method of an object, which can only be done because of the code in its originating class. To instantiate means “to create an instance of,” which, if used in real-world context (although awkward), would be akin to saying “make me one of those, and make it just like the others.” Wait, I know I might have lost you by this point, but you needed to understand what variables, methods, and classes were before an object made any sense. We know a class is a template, right? Well, consider that you can instantiate multiple objects based on this class. What this means is that they are absolutely identical in every way except for maybe their data and name, which we normally call a “reference” (because it’s actually referring to a location in memory, a hexadecimal number that would glaze your eyes over!). If a metaphor would help, they’re all identical twins that know different things, but can do the same actions! So, even though you declare variables and methods in a class, when a variable is modified or a method is run they are not affecting the class but the object that was instantiated! Now it makes sense suddenly, doesn’t it? But wait! How does this “instantiation” thing work you ask? Good question, through a type of method (yes, these are methods) called constructors! What they do is (hold onto your hats because I’m using the next word in a different sense) instantiate the variables of an object so that the object knows what data is has in case it has to do something. Now wait, we just instantiated the object, why instantiate the variables; moreover, what’s the difference? The answer is we haven’t instantiated the object yet, we need to set values to the variables so that the object has data. Only then should the object be considered instantiated! So now we know what an object is: an instance of a class that has variables and methods dictated by the class. You might be wondering about methods at this point. Methods have, traditionally, two types: accessors, and mutators. What’s an Accessor? Well, quite simply, it accesses and returns (“gives back”) data stored in the object. Nothing too special, right? Mutators, on the other hand, can do all sorts of creative things but normally modify the data of an object in some way. You won’t always get data back from these, nor will they always tell you whether they successfully executed, but their purpose is clear: when something gets changed, this is the type of method it becomes. Something to note is that this is simply a naming convention, not a requirement. You can call them “Destroyer” methods and nobody can really say anything about it, as the compiler will not complain and your code will still run. Despite this, I encourage you to think of others who may read your code, and to try your best to keep to a standard  and/or convention! The next post will actually involve some code examples of all these concepts, which will be linked here upon creation. If you’re still having trouble, don’t fret, keep reading up on the concepts as much as you can and, if necessary, ask for metaphors or similes! */

Read more

Java: An Introduction to a Notorious Object-Oriented Language

tl;dr (too long; didn’t read) – Java is infamously difficult according to many programmers, but it’s good to know. The next post delves into terminology and concepts. // Continue if you want to read more. /* Java is notorious as being one of the most difficult to learn for beginner programmers, but don’t worry! This just means when you grasp it, the other object-oriented languages such as C# (Microsoft’s take on Java) or C++ will be a snap to pick up. It’s like learning Latin before you learn Spanish or French! Now, in no way am I saying Java was the first language, because it was probably (I may be wrong here) Assembly or some low-level language and you’ll worry about that if you have to take “Fundamentals of Computer Systems” or require binary/assembly code in whatever it is you’re doing such as x86; however, we’re not learning a low-level language, we’re learning a “high-level language.” The reason Java is considered a high-level language is because of the way it interacts with the system. Not only does it have its own compiler (think of it as a big translator that pre-builds programs for now) and “garbage collector” (which automates memory management) but it also has a virtual environment in which it is run, making it universally available among the operating systems. These are all reasons it’s favorable to have some idea how to code in Java, even if you’ll be writing mostly Javascript or C#, etc. In the next posts we’ll discuss what a class is, what a method and its different types are, what a variable and its different types/scopes are, and how they all fit together to create an instantiated entity or “object.” */

Read more