Found 9150 Articles for Object Oriented Programming

How to create a directory hierarchy using Java?

Maruthi Krishna
Updated on 12-Mar-2024 18:37:31

2K+ Views

The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories. The mkdir() method of this class creates a directory with the path represented by the current object. Creating directory hierarchy There are two main ways to create a directory hierarchy in Java − Using mkdirs() Method Using createDirectories() Method Let us look at these solutions one by one. Using mkdirs() Method To create a hierarchy of new directories you can using the method mkdirs() of the same class. This ... Read More

Converting a StringBuilder to String in Java

Maruthi Krishna
Updated on 02-Sep-2023 15:06:14

59K+ Views

The toString() method of the StringBuilder class reruns String value of the current object. To convert a StringBuilder to String value simple invoke the toString() method on it.Instantiate the StringBuilder class.Append data to it using the append() method.Convert the StringBuilder to string using the toString() method.ExampleIn the following Java program we are converting an array of Strings to a single String using the toString() method of the StringBuilder. Live Demopublic class StringToStringBuilder {    public static void main(String args[]) {       String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna" };       StringBuilder sb = new StringBuilder(); ... Read More

Converting String to StringBuilder in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:38:52

647 Views

The append() method of the StringBuilder class accepts a String value and adds it to the current object.To convert a String value to StringBuilder object −Get the string value.Append the obtained string to the StringBuilder using the append() method.ExampleIn the following Java program, we are converting an array of Strings to a single StringBuilder object. Live Demopublic class StringToStringBuilder {    public static void main(String args[]) {       String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna" };       StringBuilder sb = new StringBuilder();       sb.append(strs[0]);       sb.append(" "+strs[1]);       sb.append(" ... Read More

Removing leading zeros from a String using apache commons library in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:31:57

2K+ Views

The stripStart() method of the org.apache.commons.lang.StringUtils class accepts two strings and removes the set of characters represented by the second string from the string of the first string.To remove leading zeros from a string using apache communal library −Add the following dependency to your pom.xml file    org.apache.commons    commons-lang3    3.9 Get the string.Pass the obtained string as first parameter and a string holding 0 as second parameter to the stripStart() method of the StringUtils class.ExampleThe following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the stripStart() ... Read More

Remove Leading Zeroes from a String in Java using regular expressions

Maruthi Krishna
Updated on 15-Oct-2019 12:51:11

8K+ Views

The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.Following is the regular expression to match the leading zeros of a string −The ^0+(?!$)";To remove the leading zeros from a string pass this as first parameter and “” as second parameter.ExampleThe following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the Regular expressions. Live Demoimport java.util.Scanner; public class LeadingZeroesRE {    public static String removeLeadingZeroes(String str) {       String strPattern ... Read More

How to wrap a JSON using flexjson in Java?

raja
Updated on 06-Jul-2020 12:45:15

1K+ Views

The Flexjson library is a lightweight Java library for serializing and de-serializing java beans, maps, arrays, and collections in a JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON and by default performs a shallow serialization. We can wrap a JSON object using the rootName() method of JSONSerializer class, this method wraps the resulting JSON in a javascript object that contains a single field named rootName.Syntaxpublic JSONSerializer rootName(String rootName)Exampleimport flexjson.JSONSerializer; public class JSONRootNameTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().rootName("My_Employee").prettyPrint(true);       Employee emp = new Employee("Adithya", "Jai", 28, ... Read More

How to make a collection thread safe in java?

Maruthi Krishna
Updated on 15-Oct-2019 07:08:23

2K+ Views

The Collections class of java.util package methods that exclusively work on collections these methods provide various additional operations which involves polymorphic algorithms.This class provides different variants of the synchronizedCollection() method as shown below −Sr.NoMethods & Description1static Collection synchronizedCollection(Collection c)This method accepts any collection object and, returns a synchronized (thread-safe) collection backed by the specified collection.2static List synchronizedList(List list)This method accepts an object of the List interfacereturns a synchronized (thread-safe) list backed by the specified list.3static Map synchronizedMap(Map m)This method accepts an object of the Map interface and, returns a synchronized (thread-safe) map backed by the specified map.4static ... Read More

Can we change method signature in overriding in Java?

Maruthi Krishna
Updated on 24-Apr-2025 12:34:06

2K+ Views

No, you cannot change the method signature while overriding. Changing the method signature becomes method overloading, not overriding. Now, let's understand why - Method Signature In Java, a method signature consists of - Method name: It is used to call the method. Parameter list: It includes the number, type, and order of parameters. Note: The method signature does not include return type, ... Read More

Why do we get ClassNotFoundException when the class exists in Java?

Maruthi Krishna
Updated on 24-Apr-2025 15:30:50

4K+ Views

Whenever we try to load a class, ClassNotFoundException occurs when the application tries to load a class at runtime using methods like Class.forName() or ClassLoader.loadClass(), but the JVM cannot locate the class in the classpath. The following are the reasons of ClassNotFoundException occurence and they are - Incorrect Class Name Incorrect Package Structure ... Read More

Difference between constants and final variables in Java?

Maruthi Krishna
Updated on 24-Apr-2025 11:00:25

13K+ Views

In Java, both constants and final variables are used to define variables that cannot be changed after initialization. But they have some differences. In this article, we will learn how they are different. Final Variable A final variable in Java means you cannot reassign it after it has been initialized. Whether it is a local variable, instance variable, or static variable, once it's assigned, the value cannot be changed. If you try to do so, a compile-time error will be generated. public class FinalExample { public static void main(String args[]) { ... Read More

Advertisements