Posts

Java Keywords (Part II): Modifiers

Image
I will continue to show the keyword list on subsequent articles. However, I will gray out the keywords previously discussed. Since the eight primitive data types were also previously discussed, I will gray out those as well. Keywords that were mentioned but not discussed, like package will remain listed until properly explained and illustrated. Java keyword list abstract continue for new switch assert default goto * package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const * float native super while Keyword marked with an asterisk (*) are keywords that, although valid, are not used by programmers. Although the list above is arranged in alphabetical order, I will go through them in a different or

Java Keywords (Part I): Creating Classes

Image
I think it is important for every beginner-level developer to fully understand the proper usage of keywords of any language. I will write a multi-part series outlining all Java keywords (as of Java 8), providing an explanation regarding it usage (or multiple usages), and illustrate this with simple examples. So, without further ado, lets examine this topic. The Java language contains 50 keywords of which only 48 are used. The following is a list of all the keywords in the Java programming language. Java keyword list abstract continue for new switch assert default goto * package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const * float native super while Keyword marked with an asterisk (*) are key

Data Types

Image
The meaning of data type in programming To properly define what is a data type in computer science, we must understand first what "type" means. After perusing the web for a definition, I found this: -A category of people or things having common characteristics . In computer science, in this case specifically for programming in Java, a data type is simply a categorization of things (objects) that have common characteristics. For illustration purposes, let us consider something we are all familiarized with: money. Since I am American, I am more familiarized with American currency . Therefore, I will use that for my first example. American currency comes in bills or banknotes (generally referred as paper currency) and in metallic form called coins. Using the definition of data types, the generic data type known as "American Currency" can be further classified as "Paper Currency" and "Coin". Why do this? Because, although all

Java Packages

I was watching a video today where this topic was brought up and I was kind of surprised at how some people categorize the importance of packages in Java. I will go over two main reasons why packages are important. Before I got into details, let start by enumerating the two reasons I am discussing in this blog. The first, and most common reason, is for organization purposes. I am sure you have heard this many times. The second reason, and the one I believe is badly overlooked, is protection to restrict access to your classes and features. Use packages to organize your classes and features The first point you should know about packages is that packages are basically the folders where your classes reside. This concept should not be foreign to anyone. If you have used a computer at least once in your life, you should already be aware why folders are helpful in keeping your files organized. If you are like me, you love to create folders to keep your files organized and to quickly

The REAL Builder Pattern

Image
I have been reading about Design Pattern lately, and I ran across some articles about the Builder Pattern that are not accurate at all. Because of that, I decided to write a short article about what this pattern is and how to properly implement it. I will start by providing a short description of what this pattern is. I will follow that with some context by providing hypothetical usages for this pattern, and conclude with a (hopefully good) code example. What is the Builder Pattern? Even these bad articles got one thing right: The Builder Pattern is a Creational Pattern . This means that the pattern's main goal is to provide a reusable solution for creating objects. You may ask, why are there more than one creational pattern? Isn't invoking a class constructor enough? Well... yes and no. Why use Builder Pattern? Experts (people who know more than me) have determined that using one of these creational patterns is preferred over invoking the constructor of a c

Using a State-Machine to Control a Wizard

On previous articles, I wrote about real-world usages for the State and Singleton design patterns. I also wrote about using these two patterns together to create a state-machine. Now, I am going use that state-machine to control a Wizard. I am going to use Java Swing to create the User Interface classes. To create my interface using Java Swing, I am going to create three panels for contents, one panel to provide the navigation controls, and a frame to hold it all together. It will be a simple example, but one you could easily expand should you have the need to create your own wizard. The Frame The frame is the application window. I will not go to much into details about the Java JFrame class, as I assume you have some basic knowledge of Java and Java Swing. If you don't much about Java Swing, I suggest you visit this Oracle tutorial on how to create frames . Main.java public class Main extends JFrame { private CardsPanel cards = new CardsPanel(); private Navi

Combining State and Singleton Patterns to Create a State-Machine

Image
In my previous two posts, I discussed real-world applications for the Singleton and State design patterns. In this article, I am going to illustrate how to combine both of these patterns to create a simple wizard. Simple State Design Pattern Implementation In a typical implementation of the design pattern, State is either an interface or abstract class, with each state of the state machine being Singleton classes. My example is going to be slightly different. I will implement the state machine using a state interface and Java enums to implement the Singleton. Using an enum is the recommended way to implement a Singleton in Java. First, let's come up with a simple state interface State.java public interface State { void goNext(Context input); void goPrevious(Context input); } Now that we have an interface defined, we can derive as many states as we need. For this example, three states should be sufficient to demonstrate the wizard's functionality. Jav