Object Oriented Programming Articles

Page 288 of 589

Enable Assertions from the command line in Java

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 561 Views

By default, assertions are disabled in Java. In order to enable them we use the following command −java -ea Example (or) java -enableassertions ExampleHere, Example is the name of the Java file.Let us see an example for generation of an assertion error by the JVM −Examplepublic class Example {    public static void main(String[] args) {       int age = 14;       assert age >= 18 : "Cannot Vote";       System.out.println("The voter's age is " + age);    } }OutputThe voter's age is 14

Read More

Validate city and state with Java Regular Expressions

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 1K+ Views

In order to match the city and state using regular expression, we use the matches method in Java. The java.lang.String.matches() method returns a boolean value which depends on the matching of the String with the regular expression.Declaration − The java.lang.String.matches() method is declared as follows −Examplepublic class Example {    public static void main( String[] args ) {       System.out.println(city("Mumbai"));       System.out.println(state("Goa"));    }    // validating the city    public static boolean city( String c ) {       return c.matches( "([a - zA - Z] + |[a - zA - Z] + \s[a ...

Read More

Validate the ZIP code with Java Regular expressions

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 545 Views

In order to match the ZIP code using regular expression, we use the matches method in Java. The java.lang.String.matches() method returns a boolean value which depends on the matching of the String with the regular expression.Declaration − The java.lang.String.matches() method is declared as follows −public boolean matches(String regex)Let us see a program which validates the ZIP code with Java Regular Expressions −Examplepublic class Example {    public static void main( String[] args ) {       System.out.println(zipIndia("400709"));       System.out.println(zipUS("10060"));    }    // validate zip    public static boolean zipIndia( String z ) {       ...

Read More

Format floating point with Java MessageFormat

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 1K+ Views

To format message with floating point fillers in Java, we use the MessageFormat class. The MessageFormat class gives us a way to produce concatenated messages which are not dependent on the language. The MessageFormat class extends the Serializable and Cloneable interfaces.Declaration −The java.text.MessageFormat class is declared as follows −public class MessageFormat extends FormatThe MessageFormat.format(pattern, params) method formats the message and fills in the missing parts using the objects in the params array matching up the argument numbers and the array indices.The format method has two arguments, a pattern and an array of arguments. The pattern contains placeholder in {} curly ...

Read More

Replace '*' with '^' with Java Regular Expressions

Nancy Den
Nancy Den
Updated on 11-Mar-2026 2K+ Views

To replace *' with '^' using Java Regular Expressions, we need to use the replaceAll() method. The replaceAll() method returns a String replacing all the character sequence matching the regular expression and String after replacement.Declaration − The java.lang.String.replaceAll() method is declared as follows −public String replaceAll(String regex, String replaced)  Let us see a program to replace '*' with '^' using Java Regular Expressions −Examplepublic class Example {    public static void main( String args[] ) {       String str = new String("H*e*l*l*o");       System.out.println( "Initial String : "+ str);       // replacing '*' with ...

Read More

Replace one string with another string with Java Regular Expressions

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 7K+ Views

To replace one string with another string using Java Regular Expressions, we need to use the replaceAll() method. The replaceAll() method returns a String replacing all the character sequence matching the regular expression and String after replacement.Declaration − The java.lang.String.replaceAll() method is declared as follows −public String replaceAll(String regex, String replaced)  Let us see a program to replace one string with another string using Java Regular Expressions −Examplepublic class Example {    public static void main( String args[] ) {       String str = new String("Good Harry Good");       System.out.println( "Initial String : "+ str);   ...

Read More

Get the list of all the public fields in Java

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 844 Views

An array of field objects is returned by the method java.lang.Class.getFields(). These field objects include the accessible public fields of the class that is represented by the class object.Also, the getFields() method returns a zero length array if the class or interface has no public fields that are accessible or if a primitive type, array class or void is represented in the Class object.A program that demonstrates this is given as follows −Exampleimport java.lang.reflect.*; public class Demo {    public static void main(String[] argv) throws Exception {       Class c = java.lang.Thread.class;       Field[] fields = ...

Read More

Removing a Preference from a Preference Node in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 620 Views

In order to remove a preference from a preference node in Java, we use the remove() method. The remove method() removes all the values associated with the specified key in the preference node.Declaration − The java.util.prefs.Preferences.remove() method is declared as follows −public abstract void remove (String key)where key is the key whose preference is to be removedThe remove methods throws the following exceptions −NullPointerExceptionThis exception occurs when the key is nullIllegalStateExceptionThis exception is thrown when the ancestor node is removed by the removeNode() method.Let us see a program to remove the preference from a preference node −Exampleimport java.util.prefs.Preferences; public class ...

Read More

Determine if a Preference Node exists in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 747 Views

In order to determine the existence a preference node in Java, we use the nodeExists() method. The nodeExists() method returns a boolean value. It returns true when the specified preference node exists in the same tree as this node.Declaration − The java.util.prefs.Preferences.remove() method is declared as follows −public abstract boolean nodeExists(String pathname)throws BackingStoreExceptionwhere pathname is the path name of the node whose existence needs to be determined.Let us see a program to determine if a preference node exists in Java −Exampleimport java.util.prefs.Preferences; public class Example {    public static void main(String[] args) throws Exception {       boolean exist ...

Read More

Display the maximum amount of memory in Java

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 320 Views

In order to display the maximum amount of memory in Java, we use the maxMemory() method. It is a method of the java.lang.Runtime Class. It returns the maximum amount of memory that the Java Virtual Machine will try to use.Declaration − The java.lang.Runtime.maxMemory() method is declared as follows −public long maxMemory()Let us see a program to display the maximum amount of memory in Java −Examplepublic class Example {    public static void main(String[] args) {       // print statement at the start of the program       System.out.println("Start...");       // displays the maximum memory   ...

Read More
Showing 2871–2880 of 5,881 articles
« Prev 1 286 287 288 289 290 589 Next »
Advertisements