Book Image

Transitioning to Java

By : Ken Fogel
Book Image

Transitioning to Java

By: Ken Fogel

Overview of this book

This comprehensive guide will help non-Java developers already using different languages transition from their current language to all things Java. The chapters are designed in a way that re-enforces a developer’s existing knowledge of object-oriented methodologies as they apply to Java. This book has been divided into four sections, with each section touching upon different aspects that’ll enable your effective transition. The first section helps you get to grips with the Java development environment and the Maven build tool for modern Java applications. In the second section, you’ll learn about Java language fundamentals, along with exploring object-oriented programming (OOP) methodologies and functional programming and discovering how to implement software design patterns in Java. The third section shows you how to code in Java on different platforms and helps you get familiar with the challenges faced on these platforms. In the fourth section, you’ll find out how you can manage and package your Java code. By the end of this Java programming book, you’ll have learned the core concepts of Java that’ll help you successfully transition from a different language to Java.
Table of Contents (23 chapters)
1
Part 1:The Java Development Environment
5
Part 2:Language Fundamentals
15
Part 3:GUI and Web Coding in Java
19
Part 4:Packaging Java Code

Understanding the record class

A record class simplifies the creation of a class that can only have immutable fields. Immutability means that the fields of a class are all final and must have a value assigned to them when the record object is instantiated.

At its simplest, a record only needs the fields listed when the record class is declared, as shown here:

public record Employee(String name, double salary) { }

When we instantiate this record, we must provide the values for name and salary:

var worker = new Employee("Bob", 43233.54);

All records have a default canonical constructor that expects a value for every field in the record. In a regular class, you would need to write a canonical constructor method to assign the values to the fields. You may add a compact constructor to a record that permits you to examine the value each field was assigned.

Here is a compact constructor. Notice that it does not have a parameter list, as it can only have a parameter...