Found 4338 Articles for Java 8

Restrictions while declaring a generic (type) in Java

Maruthi Krishna
Updated on 06-Sep-2019 12:57:21

955 Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Restrictions on genericsYou cannot use generics in certain ways and in certain scenarios as listed below −You cannot use primitive datatypes with generics.class Student{    T age;    Student(T age){       this.age = age; ... Read More

Out of memory exception in Java:

Maruthi Krishna
Updated on 06-Sep-2019 11:31:01

901 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 1 Live Demopublic class SpaceErrorExample {    public static void main(String args[]) throws Exception {       Float[] array = new Float[10000 * 100000];    } }OutputRuntime exceptionException ... Read More

Why do we need generics in Java?

Maruthi Krishna
Updated on 06-Sep-2019 11:29:28

4K+ Views

Reference typesAs we know a class is a blue print in which we define the required behaviors and properties and, an interface is similar to class but it is a Specification (containing abstract methods).These are also considered as datatypes in Java, unlike other primitive datatypes a literal of these kind of types points/refers to the location of the object. They are also known as reference types.GenericsGenerics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the ... Read More

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

Maruthi Krishna
Updated on 06-Sep-2019 11:19:46

2K+ 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

Is it possible to throw exception without using "throws Exception" in java?

Maruthi Krishna
Updated on 06-Sep-2019 11:02:55

3K+ Views

When an exception occurs in Java, the program terminates abnormally and the code past the line that caused the exception doesn’t get executed.To resolve this you need to either wrap the code that causes the exception within try catch ot, throw the exception using the throws clause. If you throw the exception using throws clause it will be p[postponed to the calling line i.e.Example Live Demoimport java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ExceptionExample{    public static String readFile(String path)throws FileNotFoundException {       String data = null;       Scanner sc = new Scanner(new File("E://test//sample.txt"));       ... Read More

How to find If a given String contains only letters in Java?

Maruthi Krishna
Updated on 07-Aug-2019 12:13:04

2K+ Views

To verify whether a given String contains only characters −Read the String.Convert all the characters in the given String to lower case using the toLower() method.Convert it into a character array using the toCharArray() method of the String class.Find whether every character in the array is in between a and z, if not, return false.ExampleFollowing Java program accepts a String from the user and displays whether it is valid or not.import java.util.Scanner; public class StringValidation{    public boolean validtaeString(String str) {       str = str.toLowerCase();       char[] charArray = str.toCharArray();       for (int i ... Read More

While chaining, can we throw unchecked exception from a checked exception in java?

Maruthi Krishna
Updated on 03-Jul-2020 08:32:08

638 Views

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects).While re-throwing exceptions you can throw the same exception as it is with out adjusting it as −try {    int result = (arr[a])/(arr[b]);    System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); }catch(ArithmeticException e) {    throw e; }Or, wrap it within a new exception and throw it. When you wrap a cached exception within another exception and throw it, it is known as exception chaining or, exception wrapping, by doing this you can adjust your exception, throwing higher ... Read More

How to check if an URL is valid or not using Java?

Maruthi Krishna
Updated on 09-Sep-2019 07:30:14

2K+ Views

The URL class of the java.net package represents a Uniform Resource Locator which is used to point a resource(file or, directory or a reference) in the worldwide web.This class provides various constructors one of them accepts a String parameter and constructs an object of the URL class. While passing URL to this method if you used an unknown protocol or haven’t specified any protocol this method throws a MalformedURLException.Similarly, the toURI() method of this class returns an URI object of the current URL. If the current URL is not properly formatted or, syntactically incorrect according to RFC 2396 this method throws ... Read More

Comparing Strings with (possible) null values in java?

Maruthi Krishna
Updated on 07-Aug-2019 11:59:25

5K+ Views

Strings in Java represents an array of characters. They are represented by the String class.Using compareTo() methodThe compareTo() method of the String class two Strings (char by char) it also accepts null values. This method returns an integer representing the result, if the value of the obtained integer is −0: Given two Strings are equal or, null.1 or less: The current String preceeds the argument.1 or more: The current String succeeds the argument.Exampleimport java.util.Scanner; public class CompringStrings {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter your first string ... Read More

What is the difference between Enumeration interface and enum in Java?

Maruthi Krishna
Updated on 07-Aug-2019 12:03:00

1K+ Views

Enum in Java is a datatype which stores a set of constant values. You can use these to store fixed values such as days in a week, months in a year etc.You can define an enum using the keyword enum followed by the name of the enumeration as −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Just like arrays, the elements/constants in an enum are identified using numbers starting from 0 in the above example the days are identified using numbers as shown in the following illustration −Retrieving values from an enumYou can retrieve all the elements ... Read More

Advertisements