Thursday, December 12, 2024

Getting started with Java

Getting started in Java can seem daunting, but with a bit of time and elbow grease, you can easily start your programming journey!

Installing Java

You can install Java from Oracle's website page Java Downloads. Select the newest JDK (Java Development Kit) and your system's OS. I chose the Windows x64 Installer. Follow the normal installation process on your PC.

Installation and IDE

To begin your first Java program, you can use the Notepad application on your PC, or install an IDE (Integrated Development Environment). Using an IDE over a simple text editor like notepad has many advantages as they contain many helpful tools for developers like error checking and debugging, and some will automatically compile code and provide you a console to test your code within the program.

For more information on installing Java and an IDE, I recommend Coding With John’s video on YouTube “Create Your First Java Program from Scratch in Minutes”. In his video, he recommends the IDE Eclipse.

 

Now that you have Java and your IDE installed, start your journey by creating your first program, “Hello World”

Java Basics

Java is an object-oriented programming (OOP) language where the code is centered around objects. An Object combines properties (attributes) and methods (behaviors), typically representing a real-world entity. A Class is a blueprint or template for creating objects, defining the properties and behaviors that can be done to an object, while an object is an instance of a class that inherits these properties and methods. For example, the class may be Animals, while an object could be created for different types of Animals, like Mammals. You can extend classes to create subclasses, so instead of an object being created for Mammals, you can create a new subclass for Mammals that extends from the Animals class and create an object from the Mammals class for Cats.

The Four Major Principles of Java

There are 4 major principles to learn for Java.

Encapsulation: Hiding the complex implementation details around data by bundling the data within a class. For example, a user may be able to call data on a cat’s diet using a public accessor (a way to fetch information) without seeing how the Diet object was created (inherited from the Animal class). You may also be able to edit data using a public Mutator without seeing the complex details of how the diet is updated.

Abstraction: Abstraction is the development of classes, objects, and types that only focus on exposing the essential functionalities of a class, object, or type.

Inheritance: Subclasses can inherit properties and methods from existing classes. In our example above, the Animals class may have the attributes “Number of Legs” and “Diet.” Classes extended from it will also have these attributes, so the Mammals and Cats class will inherit these attributes and any methods unless overridden.

Polymorphism: Allows you to use the same method to have different behaviors by overriding. In our animal class example, if we have a method makenoise = ‘Growl’ we can use polymorphism on the subclass reptile to define behavior to output ‘Ssssssss’ instead of ‘Growl’.


Recommended Resources