javaworld

Comparing Java objects with equals() and hashcode()

In this Java Challenger you’ll learn how equals() and hashcode() combine to make object comparisons efficient and easy in your Java programs. Simply put, these methods work together to verify if two objects have the same values.  Without equals() and hashcode() we would have to create very large "if" comparisons, comparing every field from an object. This would make code really confusing and hard to read. Together, these two methods...

javaworld

Class and object initialization in Java

Classes and objects in Java must be initialized before they are used. You've previously learned that class fields are initialized to default values when classes are loaded and that objects are initialized via constructors, but there is more to initialization. This article introduces all of Java's features for initializing classes and objects. download Get the code Download the source code for example applications in this...

javaworld

Java tip: Fields and methods in Java

The Java tutorial Java 101: Classes and objects in Java introduces the fundamentals of Java classes and objects, including how to declare classes, describe attributes via fields, describe behaviors via methods, initialize objects via constructors, and instantiate objects from classes and access their members. This Java tip builds on that introduction, introducing seven advanced techniques for working with fields and methods in...

javaworld

Java tip: Fields and methods in Java

The Java tutorial Java 101: Classes and objects in Java introduces the fundamentals of Java classes and objects, including how to declare classes, describe attributes via fields, describe behaviors via methods, initialize objects via constructors, and instantiate objects from classes and access their members. This Java tip builds on that introduction, introducing seven advanced techniques for working with fields and methods in...

javaworld

Java tip: When to use composition vs inheritance

In Java 101: Inheritance in Java, Part 1, you learned how to leverage inheritance for code reuse, by establishing is-a relationships between classes. Composition is a closely related programming technique that is used to establish has-a relationships instead. Whereas inheritance extends the features of one class to another, composition allows us to compose one class from another class. The distinction is subtle at first, but it will...

javaworld

Java XML and JSON: Document processing for Java SE, Part 2: JSON-B

In this post, we'll continue exploring XML and JSON in Java 11 and beyond.Examples in this post will introduce you to JSON-B, the JSON Binding API for Java. After a quick overview and installation instruction, I'll show you how to use JSON-B to serialize and deserialize Java objects, arrays, and collections; how to customize serialization and deserialization using JSON-B; and how to use JSON-B adapters to convert source objects to...