Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 11 of 50

Can we throw an Unchecked Exception from a static block in java?

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

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Examplepublic class MyClass {    static{       System.out.println("Hello this is a static block");    }    public static void main(String args[]){       System.out.println("This is main method");    } }OutputHello this is a static block This is main methodExceptions in static blockJust like any other method in Java when an exception occurs in static block you can handle it using try-catch pair.Exampleimport ...

Read More

How to check if a file is readable, writable, or, executable in Java?

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

In general, whenever you create a file you can restrict/permit certain users from reading/writing/executing a file.In Java files (their abstract paths) are represented by the File class of the java.io package. This class provides various methods to perform various operations on files such as read, write, delete, rename, etc.In addition, this class also provides the following methods −setExecutble() − This method issued to set the execute permissions to the file represented by the current (File) object.setWritable() − This method is used to set the write permissions to the file represented by the current (File) object.setReadable() − This method is used ...

Read More

Out of memory exception in Java:\\n

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

Whenever you create an object in Java it is stored in the heap area of the JVM. If the JVM is not able to allocate memory for the newly created objects an exception named OutOfMemoryError is thrown.This usually occurs when we are not closing objects for long time or, trying to act huge amount of data at once.There are 3 types of errors in OutOfMemoryError −Java heap space.GC Overhead limit exceeded.Permgen space.Example 1public class SpaceErrorExample {    public static void main(String args[]) throws Exception {       Float[] array = new Float[10000 * 100000];    } }OutputRuntime exceptionException in ...

Read More

Checked Vs unchecked exceptions in Java programming.

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

Checked exceptionsA checked exception is an exception that occurs at the compile time, these are also called as compile-time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.When a checked/compile time exception occurs you can resume the program by handling it using try-catch blocks. Using these you can display your own message or display the exception message after the execution of the complete program.Exampleimport java.io.File; import java.io.FileInputStream; public class Test {    public static void main(String args[]){       System.out.println("Hello");       try{         ...

Read More

Shadowing of static methods in Java\\n

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

When superclass and the subclass contain the same instance methods including parameters, when called, the superclass method is overridden by the method of the subclass.Exampleclass Super{    public void sample(){       System.out.println("Method of the Super class");    } } public class MethodOverriding extends Super {    public void sample(){       System.out.println("Method of the Sub class");    }    public static void main(String args[]){       MethodOverriding obj = new MethodOverriding();       obj.sample();    } }OutputMethod of the Sub classMethod shadowingWhen superclass and subclass contain the same method including parameters and if they are ...

Read More

Can we assign values to final arrays in Java?

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

The array is a container that can hold a fixed number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. The following are the important terms to understand the concept of Array.Element − Each item stored in an array is called an element.Index − Each location of an element in an array has a numerical index, which is used to identify the element.The size of the array will be determined at the time of creation.Examplepublic class ArrayExample {    public static void main(String args[]){   ...

Read More

Traversing contents of a hash map in Java

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

A map is a collection in Java which stores key-value pairs. The keys of this must not be null and each key should point to only one value. It is represented by the Map interface of java.util package. There are various classes which provide implementation to this interface.The HashMap is a class which implements the Map interface. It is based on the Hash table. It allows null values and null keys.In short, you can store key-value pairs in the HashMap object. Once you do so you can retrieve the values of the respective keys but, the values we use for ...

Read More

How to import classes from within another directory/package in Java?

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

In Java classes and interfaces related to each other are grouped under a package. The package is nothing but a directory storing classes and interfaces of a particular concept. For example, all the classes and interfaces related to input and output operations are stored in the java.io package.There are two types of packages namely user-defined packages and built-in packages (pre-defined)The import keywordWhenever you need to use the classes from a particular package −First of all, you need to set a classpath for the JAR file holding the required package.Import the required class from the package using the import keyword. While ...

Read More

Assigning arrays in Java.

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

While creating variables first of all we will declare them, initialize them, assign/re-assign values to them.Similarly, while creating arrays −You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;Assigning values to an arrayWhen we assign primitive values of one type to a variable of other (datatype) implicitly they are converted.But, when you try to assign a higher datatype ...

Read More

How to execute an external program like windows media player in Java?

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

Using the Runtime classJava provides a class named java.lang.Runtime, using this class you can interface with the current environment.The getRunTime() (static) method of this class returns a Runtime object associated with the current application.The exec() method accepts a String value representing the command to execute a process in the current environment (system) and executes it.Therefore, to execute an external application using the Runtime class −Get the run time object using the getRuntime() method.Execute the required process by passing the path of it as a String value to the exec() method.Exampleimport java.io.IOException; public class Trail {    public static void main(String ...

Read More
Showing 101–110 of 500 articles
« Prev 1 9 10 11 12 13 50 Next »
Advertisements