Programming Articles

Page 1581 of 2547

Java Program to check whether one String is a rotation of another.

Ayyan
Ayyan
Updated on 11-Mar-2026 641 Views

To find weather a string is rotation of another, concat the first string to itself twice and find weatherExamplepublic class Sample {    public static void main(String args[]){       String str1 = "gala";       String str2 = "alag";       String s3 = str1+str1;       if(s3.contains(str2)) {          System.out.println("str1 is rotation of str2");       } else {          System.out.println("str1 is not rotation of str2");       }    } }

Read More

How to determine the OS the computer is running using Java?

Sreemaha
Sreemaha
Updated on 11-Mar-2026 254 Views

The System class of java.lang package provides a method named getProperty() this method accepts one of the following string parameters an returns the respective property. java.class.path − If you pass this value as a parameter, the getProperty() method returns the current classpath. java.home − If you pass this value as a parameter, the getProperty() method returns the current Installation directory of the JRE. java.vendor − If you pass this value as a parameter, the getProperty() method returns the current vendor name of the JRE. java.vendor.url − If you pass this value as a parameter, the getProperty() method returns the ...

Read More

How to execute a static block without main method in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

VM first looks for the main method (at least the latest versions) and then, starts executing the program including static block. Therefore, you cannot execute a static block without main method. Example public class Sample { static { System.out.println("Hello how are you"); } } Since the above program doesn’t have a main method, If you compile and execute it you will get an error message. C:\Sample>javac StaticBlockExample.java C:\Sample>java StaticBlockExample Error: Main method not found in class StaticBlockExample, please define the main method as: public static ...

Read More

What are annotations in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

Annotations are a tag (metadata) which provides info about a program. Annotations in Java Start with the symbol ‘@’. They are used by the compiler to detect errors. Software tools to generate code. They are used to show attributes of an element: e.g. @Deprecated, @Override. Annotation are used to describe the purpose of an element of the framework, e.g. @Entity, @TestCase, @WebService Annotations describe the behaviour of an element: @Statefull, @Transaction. Example class Sample{ public void display(){ System.out.println(" "); } } public class Test extends ...

Read More

Write a java program reverse tOGGLE each word in the string?

Manikanth Mani
Manikanth Mani
Updated on 11-Mar-2026 909 Views

To perform a reverse toggle split the words of a string, reverse each word using the split() method, change the first letter of each word to lower case and remaining letters to upper case.Exampleimport java.lang.StringBuffer; public class ToggleReverse {    public static void main(String args[]){       String sample = "Hello How are you";       String[] words = sample.split(" ");       String result = "";       for(String word:words){          StringBuffer s = new StringBuffer(word);          word = s.reverse().toString();          String firstSub = word.substring(0, 1);          String secondSub = word.substring(1);          result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";       }       System.out.println(result);    } }OutputoLLEH wOH eRA uOY

Read More

What are unchecked exceptions in Java?

Sharon Christine
Sharon Christine
Updated on 11-Mar-2026 3K+ Views

An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation. If you have declared an array of size 5 in your program, and trying to call the 6th element of the array then an ArrayIndexOutOfBoundsExceptionexception occurs.Examplepublic class Unchecked_Demo {    public static void main(String args[]) {       int num[] = {1, 2, 3, 4};       System.out.println(num[5]);    } }OutputException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5    at Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)

Read More

How to compare two ArrayList for equality in Java?

Srinivas Gorla
Srinivas Gorla
Updated on 11-Mar-2026 14K+ Views

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.Exampleimport java.util.ArrayList; public class ComparingList {    public static void main(String[] args) {       ArrayList list1 = new ArrayList();       list1.add("JavaFx");       list1.add("Java");       list1.add("WebGL");       list1.add("OpenCV");       ArrayList list2 = new ArrayList();       list2.add("JavaFx");       list2.add("Java");       list2.add("WebGL");       list2.add("OpenCV");       System.out.println(list2);       System.out.println(list1.equals(list2));    } }

Read More

How to convert a list collection into a dictionary in Java?

Nikitha N
Nikitha N
Updated on 11-Mar-2026 2K+ Views

Following is an example to convert a list collection into a dictionary in Java.Exampleimport java.util.ArrayList; import java.util.Dictionary; import java.util.Hashtable; public class CollectionDictionary {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       System.out.println(list);       Dictionary dictionary = new Hashtable();       Hashtable hashTable = new Hashtable();       hashTable.put(1, list.get(0));       hashTable.put(2, list.get(1));       hashTable.put(3, list.get(2));       hashTable.put(4, list.get(3));       System.out.println(hashTable);    } }Output[JavaFx, Java, WebGL, OpenCV] {4=OpenCV, 3=WebGL, 2=Java, 1=JavaFx}

Read More

How to swap two String variables without third variable.

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

To swap the contents of two strings (say s1 and s2) without the third, first of all concatenate them and store in s1. Now using the substring() method of the String class store the value of s1 in s2 and vice versa.Examplepublic class Sample {    public static void main(String args[]){       String s1 = "tutorials";       String s2 = "point";       System.out.println("Value of s1 before swapping :"+s1);       System.out.println("Value of s2 before swapping :"+s2);       int i = s1.length();       s1 = s1+s2;       s2 = s1.substring(0, ...

Read More

What is static import in Java?

Krantik Chavan
Krantik Chavan
Updated on 11-Mar-2026 871 Views

As import statement allows to use a class without its package qualification, static import allows to access the static members of a class without class qualifications. For Example, to access the static methods you need to call the using class name − Math.sqrt(169); But, using static import you can access the static methods directly. Example import static java.lang.Math.*; public class Sample{ public static void main(String args[]){ System.out.println(sqrt(169)); } } Output 13.0

Read More
Showing 15801–15810 of 25,466 articles
Advertisements