Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Maruthi Krishna
500 articles
How to find a unique character in a string using java?
In this article, we will learn how to find a unique character in a string in Java. A unique character is a character that occurs only once in the string, while all others can occur multiple times. We can solve this problem using the following ways: Using Brute Force method. Using HashMap. Using brute force method To find a unique character in a given string, we will use a brute force approach. In the brute force approach, we will use two loops to compare each character to the rest of the string. Let's look at the steps: ...
Read MoreHow to check that a string is parse-able to a double in java?
Let's learn how to check if a string is parseable to a double in Java. We have multiple ways to do this. Some of them are: Using Double.parseDouble() Using Double.valueOf() Using the constructor of the Double class Using Double.parseDouble() The parseDouble() method of the java.lang.Double class accepts a String value, parses it, and returns the double value of the given String. If you pass a null value to this method, it throws a NullPointerException and if this method is not able to parse the given string ...
Read MoreWhat is ARM in Java?
Automatic resource management(ARM) or try-with-resources is a new exception handling mechanism that we are going to discuss in this article. What is a resource? A resource is an object which implements AutoClosable interface. Whenever you use a resource in your program, it is recommended to close it after use. For example, if you open a file using FileInputStream, it is recommended to close the stream after usage. This is done to free up the system resources. In Java, we can use the close() method to close the resources. But if you forget to close the resource, it will lead ...
Read MoreHow to insert a string in beginning of another string in java?
Let's learn how to add a string at the beginning of another string in Java. We can do this in the following ways: Using the toCharArray(). Using the String concatenation operator. Using the StringBuilder class. Using the StringBuffer class. Using the toCharArray() toCharArray() method of the String class that helps us to convert a string into a character array. We will use this method to insert a string at the beginning of another string. Following are the steps to do so: Get both strings, suppose we have a string str1 and the string to be added at ...
Read MoreHow convert an array of Strings in a single String in java?
In this article, we will learn how to convert an array of strings into a single string in Java. There are several ways to do so. The following are the different ways to convert an array of strings into a single string: Using the StringBuffer class. Using the toString() method of the Arrays class. Using the StringJoiner class. Using StringBuffer The StringBuffer class is used for creating mutable (modifiable) strings. It is similar to the String class, but it is mutable. The StringBuffer class is synchronized, which means it is thread-safe. To convert an array of strings into ...
Read MoreHow can we add an underscore before each capital letter inside a java String?
In this article, we will learn how to add an underscore before each capital letter in a Java String. We also call this process as "camel case to snake case" conversion. We can solve this problem using the following ways: Using StringBuffer class. Using Regular Expressions. Using StringBuffer class We will use the method called append() of the StringBuffer class to add an underscore. In this method, we will traverse through each character of the string and check if it is an uppercase letter. If it is, we will append ...
Read MoreCan we define constructor inside an interface in java?
Interfaces in Java are used for defining a contract that classes can implement. They can contain method signatures, default methods, static methods, and constants. we must implement the methods defined in the interface in the implementing class. Can we define a constructor inside an interface in Java? No, constructors cannot be defined inside an interface in Java. Constructors are special methods that are used to initialize objects of a class, and since interfaces cannot be instantiated, they do not have constructors. Why can constructors not be defined in an interface? There are several reasons why constructors cannot be defined ...
Read MoreConstructor overloading in enumerations in Java.
Overloading is one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters. It allows one method to perform different tasks based on the parameters passed to it. Whenever you call this method, the method body will be bound with the method call based on the parameters we pass to it. Constructor Overloading in Java Constructors are special methods that are called when an object is created. Constructor overloading in Java allows a class to have multiple constructors with different parameters. This is useful when you want to create objects in ...
Read MoreWhat is the return type of a Constructor in Java?
A Constructor in Java is similar to a method, and it is invoked at the time of creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class. The main purpose of a constructor is to initialize the instance variables of a class. Return Type of Constructor In Java, constructors do not have a return type, not even void. They are used to initialize the object when it is created. A constructor doesn’t have any return type. ...
Read MoreCan we call a method on \\\"this\\\" keyword from a constructor in java?
The “this" keyword in Java is used as a reference to the current object within an instance method or a constructor. Using this, you can refer to the members of a class, such as constructors, variables, and methods. Calling a Method using "this" From a Constructor Yes, as mentioned, we can call all the members of a class (methods, variables, and constructors) from instance methods or constructors. Example In the following Java program, the Student class contains two private variables, name and age, with setter methods and a parameterized constructor that accepts these two values. From the constructor, we are ...
Read More