Programming Articles - Page 2502 of 3366

What is a MalformedURLException and how to fix it in java?

Maruthi Krishna
Updated on 02-Jul-2020 14:26:42

21K+ Views

While working with client-server programming in Java (JSE), if you are using java.net.URL class object in your program, you need to instantiate this class by passing a string representing required URL to which you need to establish connection. If the url you have passed in the string which cannot be parsed or, without legal protocol a MalformedURLException is generated.ExampleIn the following Java example we are tring to get establish a connection to a page and publishing the response.We have tampered the protocol part, changed it to htt, which should be http or, https.import java.util.Scanner; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; ... Read More

What happens if an exception is not handled in a java program?

Maruthi Krishna
Updated on 02-Jul-2020 14:28:11

3K+ Views

An exception is an issue (run time error) occurred during the execution of a program. For understanding purpose let us look at it in a different manner.Generally, when you compile a program, if it gets compiled without a .class file will be created, this is the executable file in Java, and every time you execute this .class file it is supposed to run successfully executing each line in the program without any issues. But, in some exceptional cases, while executing the program, JVM encounters some ambiguous scenarios where it doesn’t know what to do.Here are some example scenarios −If you ... Read More

Do all properties of an Immutable Object need to be final in Java?

Maruthi Krishna
Updated on 02-Jul-2020 14:29:20

1K+ Views

Immutable class/object is the one whose value cannot be modified. For example, Strings are immutable in Java i.e. once you create a String value in Java you cannot modify it. Even if you try to modify, an intermediate String is created with the modified value and is assigned to the original literal.Defining immutable objectsWhenever you need to create an object which cannot be changed after initialization you can define an immutable object. There are no specific rules to create immutable objects, the idea is to restrict the access of the fields of a class after initialization.ExampleFollowing Java program demonstrates the ... Read More

Why variables defined in try cannot be used in catch or finally in java?

Maruthi Krishna
Updated on 02-Jul-2020 14:30:03

6K+ Views

A class in Java will have three kinds of variables namely, static (class), instance and, local.Instance variables − These variables belong to the instances (objects) of a class. These are declared within a class but outside methods. These are initialized when the class is instantiated. They can be accessed from any method, constructor or blocks of that particular class.Class/static variables − class/static variables belong to a class, just like instance variables they are declared within a class, outside any method, but, with the static keyword.They are available to access at the compile time, you can access them before/without instantiating the ... Read More

Program to calculate Bitonicity of an Array

sudhir sharma
Updated on 01-Jul-2020 06:07:18

125 Views

The bitonicity of an array is defined using the following syntax −To find bitonicity of an array based on its elements is −Bitonicity = 0 , initially arr[0] i from 0 to n Bitonicity = Bitonicity+1 ; if arr[i] > arr[i-1] Bitonicity = Bitonicity-1 ; if arr[i] < arr[i-1] Bitonicity = Bitonicity ; if arr[i] = arr[i-1]ExampleThe code for finding the bitonicity of an array we have used a variable called bitonicity, that changes its based on the comparison of the current and previous elements of the array. The above logic updates the bitonicity of the array and final bitonicity ... Read More

Program to calculate the area of a Tetrahedron

sudhir sharma
Updated on 06-Aug-2019 06:46:49

181 Views

A tetrahedron is a pyramid with triangular base i.e. it has a base that is a triangle and each side has a triangle. All the three triangles converge to a point. As in the figure, Area of Tetrahedron = (√3)a2ExampleThe code to find the area of tetrahedron uses the math library to find the square and square-root of a number using sqrt and pow methods. For calculating the area we take a floating point and the value of the expression "((sqrt(3)*a*a))" is given to it.#include #include int main() {    int a= 5;    float area, volume;   ... Read More

Program to build DFA that starts and ends with ‘a’ from the input (a, b)

sudhir sharma
Updated on 06-Aug-2019 06:41:47

1K+ Views

DFA stands for Deterministic Finite Automata. It is a finite state machine that accepts or a string based on its acceptor.Here, we are going to make a DFA that accepts a string that starts and ends with a. The input is from the set (a, b). Based on this we will design a DFA. Now, Let's discuss some valid and invalid cases that are accepted by a DFA.Strings that are accepted by DFA: ababba, aabba, aa, a.Strings that are not accepted by DFA: ab, b, aabab.ExampleThis program check for a string that starts and ends with a. This DFA will ... Read More

How can we Implement a Stack using Queue in Java?

Vivek Verma
Updated on 22-Jul-2025 12:42:53

1K+ Views

This article will discuss how to implement a Stack using a Queue in Java.  Stack in Java In Java, a Stack is a subclass (child class) of the Vector class, and it represents a LIFO stack of objects, which stands for Last-in-First-Out. The last element added at the top of the stack (In) can be the first element to be removed (Out) from the stack. The following diagram will give you a clear idea about the Stack: Queue in Java In Java, the Queue class extends the Collection interface, and it supports the insert and remove operations using a FIFO, ... Read More

How many ways to make an object eligible for GC in Java?

raja
Updated on 23-Nov-2023 09:07:55

510 Views

The process of destroying unreferenced objects is called a Garbage Collection(GC). Once an object is unreferenced it is considered as an unused object, hence JVM automatically destroys that object. There are various ways to make an object eligible for GC. By nullifying a reference to an object We can set all the available object references to "null" once the purpose of creating an object is served. Example public class GCTest1 { public static void main(String [] args){ String str = "Welcome to TutorialsPoint"; // String object referenced by variable str and it is not eligible for GC yet. ... Read More

How to read DataInputStream until the end without need to catch an EOFException in java?

Maruthi Krishna
Updated on 02-Jul-2020 14:09:12

1K+ Views

While reading the contents of a file in certain scenarios the end of the file will be reached in such scenarios a EOFException is thrown.Especially, this exception is thrown while reading data using the Input stream objects. In other scenarios a specific value will be thrown when the end of file reached.In the DataInputStream class, it provides various methods such as readboolean(), readByte(), readChar() etc.. to read primitive values. While reading data from a file using these methods when the end of file is reached an EOFException is thrown.ExampleFollowing program demonstrates how to handle the EOFException in Java.import java.io.DataInputStream; import ... Read More

Advertisements