Java Articles

Page 31 of 450

Is it possible to check if a String only contains ASCII in java?

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

Using regular expressionYou can find whether a particular String value contains ASCII characters using the following regular expression −\A\p{ASCII}*\zThe matches() method of the String class accepts a regular expression and verifies whether the current string matches the given expression if so, it returns true, else it returns false.Therefore, Invoke the matches() method on the input/required string by passing the above specified regular expression as a parameter.Exampleimport java.util.Scanner; public class OnlyASCII {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string value: ");       String input = ...

Read More

How to parse Date from String in the format: dd/MM/yyyy to dd/MM/yyyy in java?

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

The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local).One of the constructors of this class accepts a String value representing the desired date format and constructors SimpleDateFormat object.The format() method of this class accepts a java.util.Date object and returns a date/time string in the format represented by the current object.Therefore, to parse a date String to another date format −Get the input date string.Convert it into java.util.Date object.Instantiate the SimpleDateFormat class by passing the desired (new) format as string to its constructor.Invoke the format() method by passing the above ...

Read More

How to make elements of array immutable in Java?

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

No, you cannot make the elements of an array immutable.But the unmodifiableList() method of the java.util.Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object. The user has only read-only access to the obtained list.And the asList() method of the ArrayList class accepts an array and returns a List object.Therefore, to convert an array immutable −Obtain the desired array.Convert it into a list object using the asList() method.Pass the obtained list as a parameter to the unmodifiableList() method.Exampleimport java.util.Arrays; import java.util.Collections; import java.util.List; public class UnmodifiableExample { ...

Read More

What is ArrayStoreException in Java?

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

When you have created an array of a particular data type with fixed size and populate it if you store a value other than its datatype an ArrayStoreException is thrown at the run time.ExampleIn the following Java program, we are creating an Integer array and trying to store a double value in it.import java.util.Arrays; public class ArrayStoreExceptionExample {    public static void main(String args[]) {       Number integerArray[] = new Integer[3];       integerArray[0] = 12548;       integerArray[1] = 36987;       integerArray[2] = 555.50;       integerArray[3] = 12548;       ...

Read More

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 959 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 to access a derived class member variable by an interface object in Java?

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

When you try to hold the super class’s reference variable with a sub class object, using this object you can access the members of the super class only, if you try to access the members of the derived class using this reference you will get a compile time error.Exampleinterface Sample {    void demoMethod1(); } public class InterfaceExample implements Sample {    public void display() {       System.out.println("This ia a method of the sub class");    }    public void demoMethod1() {       System.out.println("This is demo method-1");    }    public static void main(String args[]) { ...

Read More

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 683 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

Why should a blank final variable be explicitly initialized in all Java constructors?

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

A final variable which is left without initialization is known as blank final variable.Generally, we initialize instance variables in the constructor. If we miss out they will be initialized by the constructors by default values. But, the final blank variables will not be initialized with default values. So if you try to use a blank final variable without initializing in the constructor, a compile time error will be generated.Examplepublic class Student {    public final String name;    public void display() {       System.out.println("Name of the Student: "+this.name);    }    public static void main(String args[]) {   ...

Read More

Why Java wouldn't allow initialization of static final variable in a constructor?

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

If you declare a variable static and final you need to initialize it either at declaration or, in the static block. If you try to initialize it in the constructor, the compiler assumes that you are trying to reassign value to it and generates a compile time error −Exampleclass Data {    static final int num;    Data(int i) {       num = i;    } } public class ConstantsExample {    public static void main(String args[]) {       System.out.println("value of the constant: "+Data.num);    } }Compile time errorConstantsExample.java:4: error: cannot assign a value to final ...

Read More

Can a final keyword alone be used to define a constant in Java?

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

A constant variable is the one whose value is fixed and only one copy of it exists in the program. Once you declare a constant variable and assign value to it, you cannot change its value again throughout the program.Unlike other languages java does not support constants directly. But, you can still create a constant by declaring a variable static and final.Static − Once you declare a variable static they will be loaded into the memory at the compile time i.e. only one copy of them is available.Final − once you declare a variable final you cannot modify its value ...

Read More
Showing 301–310 of 4,498 articles
« Prev 1 29 30 31 32 33 450 Next »
Advertisements