TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Objects, Objects.requireNonNull Example

Use the Object and Objects classes. Object is a superclass and Objects is a class with helpful methods.
Objects. Class instances exist within a hierarchy. At the top, a superclass exists: this is Object. We can treat any instance as an object.Class
The Objects class. Another class, Objects, offers many helpful utility methods. These allow us to validate objects (with requireNonNull) or compute values based on them (with hash).
RequireNonNull. Let us begin with this method. We pass an Object to requireNonNull—it does nothing unless the object is null. If null, it throws an Exception.Exceptions

Note: This method is used for parameter validation, as in constructor bodies. Please see the next example.

Java program that uses Objects.requireNonNull import java.util.Objects; public class Program { public static void main(String[] args) { // Ensure an object is not null. String value = null; Objects.requireNonNull(value); } } Output Exception in thread "main" java.lang.NullPointerException at java.util.Objects.requireNonNull(Unknown Source) at program.Program.main(Program.java:11)
Validate constructor. Here we use Objects.requireNonNull to validate a constructor on a test class. The String argument must not have a null value. It is required.

Note: In main we test the constructor. When called with a String like "Andrew" it works with no error. But null causes an Exception.

Java program that validates in constructor import java.util.Objects; class Test { public Test(String name) { Objects.requireNonNull(name, "name cannot be null"); } } public class Program { public static void main(String[] args) { // This is safe. Test t = new Test("Andrew"); // This will cause an exception. Test t2 = new Test(null); } } Output Exception in thread "main" java.lang.NullPointerException: name cannot be null at java.util.Objects.requireNonNull(Unknown Source) at program.Test.<init>(Program.java:8) at program.Program.main(Program.java:18)
Objects.hash. This method computes a hash code for one or more objects. We pass values directly as arguments. It can be used to implement the hashCode method.

Here: We implement hashCode on the Test class. The hash for Test is based on two parameters: the X and Y fields.

Tip: This is a convenience method. When we have just one object to base a hash code on, using hashCode directly is clearer.

Java program that uses Objects.hash import java.util.Objects; class Test { int x; int y; public Test(int x, int y) { this.x = x; this.y = y; } @Override public int hashCode() { return Objects.hash(this.x, this.y); } } public class Program { public static void main(String[] args) { // Get hash code for these objects. Test t = new Test(1, 10); System.out.println(t.hashCode()); Test t2 = new Test(2, 20); System.out.println(t2.hashCode()); } } Output 1002 1043
An abstraction. This abstract model gives programs the powerful capability of generalization. Any instance, now, may be treated an object.
Usually, we access methods defined directly on a class. But methods from the Object class are universal, and help in many program parts.Methods
Helpers. With the Objects class, we access methods that act on many objects. Some methods, like requireNonNull, are helper methods, ones that make a common pattern easier to implement.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf