Found 2616 Articles for Java

How can we implement a custom iterable in Java?

raja
Updated on 23-Nov-2023 11:23:04

1K+ Views

An Iterable interface is defined in java.lang package and introduced with Java 5 version. An object that implements this interface allows it to be the target of the "for-each" statement. This for-each loop is used for iterating over arrays and collections. An Iterable interface can also be implemented to create custom behavior. Syntax public interface Iterable Example import static java.lang.String.format; import java.util.*; // Person class class Person { private String firstName, lastName; private int age; public Person(){ } public Person(String firstName, String lastName, int age) { this.firstName = firstName; ... Read More

Importance of return type in Java?

raja
Updated on 23-Nov-2023 11:30:37

23K+ Views

A return statement causes the program control to transfer back to the caller of a method. Every method in Java is declared with a return type and it is mandatory for all java methods. A return type may be a primitive type like i nt, float, double, a reference type or void type(returns nothing). There are a few important things to understand about returning the values. The type of data returned by a method must be compatible with the return type specified by the method. For instance, if the return type of some method is boolean, we can not return an integer. The ... Read More

How to read the data from a properties file in Java?

raja
Updated on 14-Sep-2023 14:00:27

28K+ Views

The Properties is a subclass of Hashtable class and it represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string. The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties.load() method of Properties class is convenient to load .properties file in the form of key-value pairs.Syntaxpublic class Properties extends Hashtablecredentials.properties fileExampleimport java.io.*; import java.util.*; public class ReadPropertiesFileTest {    public static void main(String args[]) throws IOException {       Properties prop ... Read More

How to instantiate a static inner class with reflection in Java?

raja
Updated on 24-Nov-2023 09:18:44

1K+ Views

A static inner class can be instantiated without the need for an instance of the outer class. In general, an Inner class is a part of nested class, called Non-static nested classes in Java. The types of inner classes are member inner class, anonymous inner class, and local inner class. We can instantiate a static inner class with reflection using InnerClass.class.newInstance(). If we need an instance of the outer class to instantiate a non-static inner class, we can specify it before a new operator. Example import java.lang.reflect.*; public class InnerclassWithReflectionTest { public static void main(String args[]) { ... Read More

Will a finally block execute after a return statement in a method in Java?

raja
Updated on 24-Nov-2023 09:21:53

17K+ Views

Yes, the finally block will be executed even after a return statement in a method. The finally block will always execute even an exception occurred or not in Java. If we call the System.exit() method explicitly in the finally block then only it will not be executed. There are few situations where the finally will not be executed like JVM crash, power failure, software crash and etc. Other than these conditions, the finally block will be always executed. Example public class FinallyBlockAfterReturnTest { public static void main(String[] args) { System.out.println(count()); } public ... Read More

Importance of XOR operator in Java?

raja
Updated on 24-Nov-2023 09:25:52

11K+ Views

Bitwise XOR (exclusive or) "^" is an operator in Java that provides the answer '1' if both of the bits in its operands are different, if both of the bits are same then the XOR operator gives the result '0'. XOR is a binary operator that is evaluated from left to right. The operator "^" is undefined for the argument of type String. Example 1 public class XORTest1 { public static void main(String[] args) { boolean x = false; boolean y = false; boolean xXorY ... Read More

How to convert an OutputStream to a Writer in Java?

raja
Updated on 24-Nov-2023 09:29:19

2K+ Views

An OutputStream class is a byte-oriented whereas Writer class is a character-oriented. We can convert an OutputStream class to a Writer class using an OutputStreamWriter class and pass an argument of ByteArrayOutputStream object to OutputStreamWriter constructor. An OutputStreamWriter is a bridge from a character stream to a byte stream, the characters written to it are encoded into bytes using a specified charset. Syntax public class OutputStreamWriter extends Writer Example import java.io.*; public class OutputStreamToWriterTest { public static void main(String[] args) throws Exception { String str = "TUTORIALSPOINT"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ... Read More

How to find the unicode category for a given character in Java?

raja
Updated on 24-Nov-2023 10:28:35

4K+ Views

A Character class is a subclass of an Object and it wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char. We can determine the unicode category for a particular character by using the getType() method. It is a static method of Character class and it returns an integer value of char ch representing in unicode general category. Syntax public static int getType(char ch) Example public class CharacterTypeTest { public static void main(String args[]) { System.out.println("T represnts unicode category of: " + Character.getType('T')); ... Read More

How to print a formatted text using printf() method in Java?

raja
Updated on 01-Jul-2020 08:12:21

435 Views

The printf() method allows us to format output to a java.io.PrintStream or java.io.PrintWriter. These classes also contain a method called format() which can produce the same results, so whatever we read here for the printf() method can also be applied to the format() method.SyntaxSystem.out.printf(“format-string” [, arg1, arg2, … ]);Example1import java.io.PrintStream; public class PrintfTest1 {    public static void main(String[] args) {       int i = 1234;       System.out.printf("Decimal: %1$, d Octal: %1$o Hex: %1$x", i);       String str = "Tutorials Point";       System.out.printf("%15s", str);    } }OutputDecimal: 1, 234 Octal: 2322 Hex: ... Read More

How to find the number of days in a month of a particular year in Java?

raja
Updated on 23-Nov-2023 10:08:36

1K+ Views

A GregorianCalendar is a concrete subclass of Calendar class and it provides the standard calendar system used by most of the world. In Java, this GregorianCalendar can handle both the Gregorian calendar as well as Julian calendar. We can determine or find the number of days in a month of a particular year by using the getActualMaximum() method of GregorianCalendar class. This method returns the maximum value that the GregorianCalendar field can have. The parameter can be any field of a Calendar class. Syntax public int getActualMaximum(int field) Example import java.util.*; public class NoOfDaysInAMonthOfAYearTest { public static void main(String []args) { ... Read More

Advertisements