Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 9 of 50

Which packages contain Wrapper class in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

Java provides certain classes called wrapper classes in the java.lang package. The objects of these classes wrap primitive datatypes within them. Following is the list of primitive data types and their respective classes −Primitive datatypeWrapper classcharCharacterbyteByteshortShortintIntegerlongLongfloatFloatdoubleDoublebooleanBooleanPackageWrapper classes in Java belong to the java.lang package, Therefore there is no need to import any package explicitly while working with them.ExampleThe following Java example accepts various primitive variables from the user and creates their respective wrapper classes.import java.util.Scanner; public class WrapperClassesExample {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter an integer value: ...

Read More

Can I define more than one public class in a Java package?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 7K+ Views

No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public. If you have more than one public classes a single file a compile-time error will be generated.ExampleIn the following example we have two classes Student and AccessData we are having both of them in the same class and declared both public.import java.util.Scanner; public class Student {    private String name;    private int age;    Student(){       this.name = "Rama";       this.age = 29;    }    Student(String name, int age){     ...

Read More

While overriding can the subclass choose not to throw an exception in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 679 Views

If the super-class method throws certain exception, you can override it without throwing any exception.ExampleIn the following example the sampleMethod() method of the super-class throws FileNotFoundException exception and, the sampleMethod() method does not throw any exception at all. Still this program gets compiled and executed without any errors.import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super {    public void sampleMethod()throws FileNotFoundException {       System.out.println("Method of superclass");    } } public class ExceptionsExample extends Super {    public void sampleMethod() {       System.out.println("Method of Subclass");    }    public static void main(String args[]) { ...

Read More

How can we split a string by sentence as a delimiter in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 11K+ Views

The split() method of the String class accepts a String value representing the delimiter and splits into an array of tokens (words), treating the string between the occurrence of two delimiters as one token.For example, if you pass single space “ ” as a delimiter to this method and try to split a String. This method considers the word between two spaces as one token and returns an array of words (between spaces) in the current String.If the String does not contain the specified delimiter this method returns an array containing the whole string as an element.Examplepublic class SplitExample { ...

Read More

What is loose coupling how do we achieve it using Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 951 Views

Coupling refers to the dependency of one object type on another, if two objects are completely independent of each other and the changes done in one doesn’t affect the other both are said to be loosely coupled.You can achieve loose coupling in Java using interfaces -Exampleinterface Animal {    void child(); } class Cat implements Animal {    public void child() {       System.out.println("kitten");    } } class Dog implements Animal {    public void child() {       System.out.println("puppy");    } } public class LooseCoupling {    public static void main(String args[]) {       Animal obj = new Cat();       obj.child();    } }Outputkitten

Read More

How can we check if specific string occurs multiple times in another string in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ Views

You can find whether a String contains a specified sequence of characters using any of the methods −The indexOf() method − The indexOf() method of the String class accepts a string value and finds the (starting) index of it in the current String and returns it. This method returns -1 if it doesn’t find the given string in the current one.The contains() method − The contains a () method of the String class accepts a sequence of characters value and verifies whether it exists in the current String. If found it returns true else it returns false.In addition to these, you ...

Read More

How to trim white space in StringBuffer in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ Views

The String class of the java.lang package represents a set of characters. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings objects are immutable, once you create a String object you cannot change their values, if you try to do so instead of changing the value a new object is created with the required value and the reference shifts to the newly created one leaving the previous object unused.The StringBuffer (and StringBuilder) class is used when there is a necessity to make a lot of modifications to a String.Unlike Strings, objects of ...

Read More

Difference between peek(), poll() and remove() method of Queue interface in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 16K+ Views

This represents a collection that is indented to hold data before processing. It is an arrangement of the type First-In-First-Out (FIFO). The first element put in the queue is the first element taken out from it.The peek() methodThe peek() method returns the object at the top of the current queue, without removing it. If the queue is empty this method returns null.Exampleimport java.util.Iterator; import java.util.LinkedList; import java.util.Queue; public class QueueExample {    public static void main(String args[]) {       Queue queue = new LinkedList();       queue.add("Java");       queue.add("JavaFX");       queue.add("OpenCV");     ...

Read More

How to remove the redundant elements from an ArrayList object in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 547 Views

The interface set does not allow duplicate elements. The add() method of this interface accepts elements and adds to the Set object, if the addition is successful it returns true if you try to add an existing element using this method, the addition operations fails to return false.Therefore, to remove redundant elements of an ArrayList object −Get/create the required ArrayList.Create an empty set object.Try to add all the elements of the ArrayList object to set objectives.Clear the contents of the ArrayList using the clear() method.Now, using the addAll() method add the contents of the set object to the ArrayList again.Exampleimport ...

Read More

Difference between next() and hasNext() in java collections?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

Java provides Iterator and ListIterator classes to retrieve the elements of the collection objects.The hasNext() methodThe hasNext() method of these interfaces returns true if the collection object has the next element else it returns false.Exampleimport java.util.ArrayList; import java.util.Iterator; public class hasNextExample{    public static void main(String[] args){       ArrayList list = new ArrayList();       //Instantiating an ArrayList object       list.add("JavaFX");       list.add("Java");       Iterator it = list.iterator();       System.out.println(it.hasNext());       it.next();       System.out.println(it.hasNext());       it.next();       System.out.println(it.hasNext());    } ...

Read More
Showing 81–90 of 500 articles
« Prev 1 7 8 9 10 11 50 Next »
Advertisements