Object Oriented Programming Articles

Page 4 of 588

Why can't we define a static method in a Java interface?

Anjana
Anjana
Updated on 11-Mar-2026 813 Views

From Java 8 onwards, static methods are allowed in Java interfaces.An interface can also have static helper methods from Java 8 onwards. public interface vehicle {    default void print() {       System.out.println("I am a vehicle!");    }    static void blowHorn() {       System.out.println("Blowing horn!!!");    } }Default Method ExampleCreate the following Java program using any editor of your choice in, say, C:\> JAVA.Java8Tester.javapublic class Java8Tester {    public static void main(String args[]) {       Vehicle vehicle = new Car(); vehicle.print();    } } interface Vehicle {    default void print() {   ...

Read More

How do I time a method execution in Java

Manikanth Mani
Manikanth Mani
Updated on 11-Mar-2026 1K+ Views

You should get a start time before making a call and end time after method execution. The difference is the time taken. Exampleimport java.util.Calendar; public class Tester {    public static void main(String[] args) {       long startTime = Calendar.getInstance().getTimeInMillis();       longRunningMethod();       long endTime = Calendar.getInstance().getTimeInMillis();       System.out.println("Time taken: " + (endTime - startTime) + " ms");    }    public static void longRunningMethod() {       try {          Thread.sleep(1000);       } catch (InterruptedException e) {          e.printStackTrace();       }    } }OutputTime taken: 1012 ms

Read More

Function pointers in Java

Akshaya Akki
Akshaya Akki
Updated on 11-Mar-2026 2K+ Views

From Java 8 onwards, the lambda expression is introduced which acts as function pointers.Lambda expressions are introduced in Java 8 and are touted to be the biggest feature of Java 8. Lambda expression facilitates functional programming and simplifies the development a lot.SyntaxA lambda expression is characterized by the following syntax.parameter -> expression bodyFollowing are the important characteristics of a lambda expression.Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple ...

Read More

Search and Replace with Java regular expressions

Sravani S
Sravani S
Updated on 11-Mar-2026 1K+ Views

Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar to the Perl programming language and very easy to learn.A regular expression is a special sequence of characters that help you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data.The replaceFirst() and replaceAll() methods replace the text that matches a given regular expression. As their names indicate, replaceFirst replaces the first occurrence, and replaceAll replaces all occurrences.Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches {   ...

Read More

How to split a Java String into tokens with StringTokenizer?

Abhinanda Shri
Abhinanda Shri
Updated on 11-Mar-2026 533 Views

The hasMoreTokens() method is used to test if there are more tokens available from this tokenizer's string.Exampleimport java.util.*; public class StringTokenizerDemo {    public static void main(String[] args) {       // creating string tokenizer       StringTokenizer st = new StringTokenizer("Come to learn");       // checking elements       while (st.hasMoreElements()) {          System.out.println("Next element : " + st.nextElement());       }    } }OutputNext element : Come Next element : to Next element : learn

Read More

How to check if a String contains another String in a case insensitive manner in Java?

Alankritha Ammu
Alankritha Ammu
Updated on 11-Mar-2026 2K+ Views

One way to do it to convert both strings to lower or upper case using toLowerCase() or toUpperCase() methods and test.Examplepublic class Sample {    public static void main(String args[]){       String str = "Hello how are you welcome to Tutorialspoint";       String test = "tutorialspoint";       Boolean bool = str.toLowerCase().contains(test.toLowerCase());       System.out.println(bool);    } }Outputtrue

Read More

Write a java program to tOGGLE each word in string?

Alankritha Ammu
Alankritha Ammu
Updated on 11-Mar-2026 1K+ Views

You can change the cases of the letters of a word using toUpperCase() and toLowerCase() methods.Split each word in the string using the split() method, change the first letter of each word to lower case and remaining letters to upper case.Examplepublic class Sample{    public static void main(String args[]){       String sample = "Hello How are you";       String[] words = sample.split(" ");       String result = "";       for(String word:words){          String firstSub = word.substring(0, 1);          String secondSub = word.substring(1);          result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";       }       System.out.println(result);    } }OutputhELLO hOW aRE yOU

Read More

StringTokenizer class in Java

V Jyothi
V Jyothi
Updated on 11-Mar-2026 655 Views

The StringTokenizer class of the java.util package allows an application to break a string into tokens.This class is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.Its methods do not distinguish among identifiers, numbers, and quoted strings.This class methods do not even recognize and skip comments.Exampleimport java.util.*;   public class Sample {    public static void main(String[] args) {         // creating string tokenizer       StringTokenizer st = new StringTokenizer("Come to learn");         // checking next token       System.out.println("Next token is : " + st.nextToken());   }     }OutputNext token is : Come

Read More

Java String toCharArray() method example.

Sai Nath
Sai Nath
Updated on 11-Mar-2026 136 Views

The toCharArray() method of a String class converts this string to a character array.Exampleimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       // converts String value to character array type value       String str = " Java was developed by James Gosling";       char retval[] = str.toCharArray();       // displays the converted value       System.out.println("Converted value to character array = ");       System.out.println(retval);    } }OutputConverted value to character array = Java was developed by James Gosling

Read More

How to test String is null or empty?

V Jyothi
V Jyothi
Updated on 11-Mar-2026 603 Views

We can verify whether the given string is empty using the isEmpty() method of the String class. This method returns true only if length() is 0.Exampleimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str = "tutorialspoint";       // prints length of string       System.out.println("length of string = " + str.length());       // checks if the string is empty or not       System.out.println("is this string empty? = " + str.isEmpty());    } }Outputlength of string = 14 is this string empty? = false

Read More
Showing 31–40 of 5,878 articles
« Prev 1 2 3 4 5 6 588 Next »
Advertisements