Smita Kapse

Smita Kapse

388 Articles Published

Articles by Smita Kapse

Page 19 of 39

How to compile assert in Java?

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

In order to compile assert in Java, we simply set the boolean expression as false.Let us see an example program −Examplepublic class Example {    public static void main(String[] args) {       assert false;       System.out.println("Compiled and executed successfully!!!");    } }OutputCompiled and executed successfully!!!

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

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 846 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

Display the maximum amount of memory in Java

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 325 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

Generate Random bytes in Java

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

In order to generate random bytes in Java, we use the nextBytes() method. The java.util.Random.nextBytes() method generates random bytes and provides it to the user defined byte array.Declaration − The java.util.Random.nextBytes() method is declared as follows −public void nextBytes(byte[] bytes)where bytes is the byte array.Let us see a program to generate random bytes in Java −Exampleimport java.util.Random; public class Example {    public static void main(String[] args) {       Random rd = new Random();       byte[] arr = new byte[7];       rd.nextBytes(arr);       System.out.println(arr);    } }Output[B@15db9742Note − The output might vary ...

Read More

How to schedule tasks in Java to run for repeated fixed-rate execution, beginning after the specified delay

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

One of the methods of the Timer class is void scheduleAtFixedRate(TimerTask task, long delay, long period). This method schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.In fixed-rate execution, each execution is scheduled with respect to the scheduled run time of the initial execution. Fixed-rate execution is apt for repetitive activities that are respond to absolute time. Likewise, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain in sync.Declaration − The java.util.Time.scheduleAtFixedRate(TimerTask task, long delay, long period) method is declared as follows −public void scheduleAtFixedRate(TimerTask task, long delay, long period)Here, task is ...

Read More

Get the declared method by name and parameter type in Java

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

The declared method can be obtained by name and parameter type by using the java.lang.Class.getDeclaredMethod() method. This method takes two parameters i.e. the name of the method and the parameter array of the method.The getDeclaredMethod() method returns a Method object for the method of the class that matches the name of the method and the parameter array that are the parameters.A program that gets the declared method by name and parameter type using the getDeclaredMethod() method is given as follows −Examplepackage Test; import java.lang.reflect.*; public class Demo {    public String str;    private Integer func1() {       ...

Read More

Generate Random double type number in Java

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

In order to generate Random double type numbers in Java, we use the nextDouble() method of the java.util.Random class. This returns the next random double value between 0.0 (inclusive) and 1.0 (exclusive) from the random generator sequence.Declaration - The java.util.Random.nextDouble() method is declared as follows −public float nextDouble()Let us see a program to generate random double type numbers in Java −Exampleimport java.util.Random; public class Example {    public static void main(String[] args) {       Random rd = new Random(); // creating Random object       System.out.println(rd.nextDouble()); // displaying a random double value between 0.0 & 1.0    } ...

Read More

Display the current method name in Java

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

The current method name that contains the execution point that is represented by the current stack trace element is provided by the java.lang.StackTraceElement.getMethodName() method.A program that demonstrates this is given as follows −Examplepublic class Demo {    public static void main(String args[]) {       System.out.println       ("The method name is: " + new Exception().getStackTrace()[0].getMethodName());    } }OutputThe method name is: mainNow let us understand the above program.The method getMethodName() is used to obtain the current method name that contains the execution point that is represented by the current stack trace element. This is printed using System.out.println(). ...

Read More
Showing 181–190 of 388 articles
« Prev 1 17 18 19 20 21 39 Next »
Advertisements