Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 47 of 50

Why do we need generics in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 06-Sep-2019 5K+ 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
Maruthi Krishna
Updated on 06-Sep-2019 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

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

Maruthi Krishna
Maruthi Krishna
Updated on 07-Aug-2019 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

Comparing Strings with (possible) null values in java?

Maruthi Krishna
Maruthi Krishna
Updated on 07-Aug-2019 6K+ 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

How to convert Date to String in Java 8?

Maruthi Krishna
Maruthi Krishna
Updated on 07-Aug-2019 658 Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.The toString() method of the LocalDate class converts the date value of the current Date object in to String and returns it.ExampleFollowing Java example accepts ...

Read More

How to represent fixed date like credit card expiry, YearMonth in java 8?

Maruthi Krishna
Maruthi Krishna
Updated on 07-Aug-2019 605 Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.The of() method of java.time.LocalDate class accepts three integer parameters representing and year, a month of an year, day of a month and, returns the ...

Read More

How to find Date after 1 week in java 8?

Maruthi Krishna
Maruthi Krishna
Updated on 07-Aug-2019 1K+ Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system.The now() method of this class obtains the current date from the system clock.The plus() method of the LocalDate class accepts a long value representing the amount to add and an object of the interface TemporalAmount representing the unit ...

Read More

What is InputMisMatchException in Java how do we handle it?

Maruthi Krishna
Maruthi Krishna
Updated on 07-Aug-2019 4K+ Views

From Java 1.5 Scanner class was introduced. This class accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions.To read various datatypes from the source using the nextXXX() methods provided by this class namely, nextInt(), nextShort(), nextFloat(), nextLong(), nextBigDecimal(), nextBigInteger(), nextLong(), nextShort(), nextDouble(), nextByte(), nextFloat(), next().Whenever you take inputs from the user using a Scanner class. If the inputs passed doesn’t match the method or an InputMisMatchException is thrown. For example, if you reading an integer data using the nextInt() method and the value ...

Read More

What changes has been introduced in JDK7 related to Exception handling in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 07-Aug-2019 130 Views

Since Java 7 try-with resources was introduced. In this we declare one or more resources in the try block and these will be closed automatically after the use. (at the end of the try block)The resources we declare in the try block should extend the java.lang.AutoCloseable class.ExampleFollowing program demonstrates the try-with-resources in Java.import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopying {    public static void main(String[] args) {       try(FileInputStream inS = new FileInputStream(new File("E:\Test\sample.txt"));       FileOutputStream outS = new FileOutputStream(new File("E:\Test\duplicate.txt"))){          byte[] buffer = new byte[1024];     ...

Read More

Can a method throw java.lang.Exception without declaring it in java?

Maruthi Krishna
Maruthi Krishna
Updated on 06-Aug-2019 267 Views

No, for that matter to throw any exception explicitly you need to create an object of that exception and throw it using the throw keyword.Without creating an object you cannot throw an exception explicitly, you might create a scenario that causes the respective exception.ExampleFollowing Java program throws a NullPointerExceptionpublic class ExceptionExample {    public static void main(String[] args) {       System.out.println("Hello");       NullPointerException nullPointer = new NullPointerException();       throw nullPointer;    } }OutputHello Exception in thread "main" java.lang.NullPointerException    at MyPackage.ExceptionExample.main(ExceptionExample.java:6)

Read More
Showing 461–470 of 500 articles
Advertisements