Found 2616 Articles for Java

How to use the substring() method of java.lang.String class in Java?

raja
Updated on 07-Feb-2020 05:41:38

183 Views

The substring() method returns a String datatype which corresponds to the original String starting from the begin index until the end Index. If the end index is not specified, it is imperative that the endIndex is the String length. Since we are dealing with the String, the index starts at '0' position.Syntaxpublic String substring(int beginIndex) public String substring(int beginIndex, int endIndex)beginIndex: the starting index or position where we want to start to cut or substring our String.endIndex:    the end index or position where we want to end our cutting or substring our String.This method returns a String datatype which corresponds to the ... Read More

How to handle the exception using UncaughtExceptionHandler in Java?

raja
Updated on 30-Jul-2019 22:30:26

819 Views

The UncaughtExceptionHandler is an interface inside a Thread class. When the main thread is about to terminate due to an uncaught exception the java virtual machine will invoke the thread’s UncaughtExceptionHandler for a chance to perform some error handling like logging the exception to a file or uploading the log to the server before it gets killed. We can set a Default Exception Handler which will be called for the all unhandled exceptions. It is introduced in Java 5 Version.This Handler can be set by using the below static method of java.lang.Thread class.public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler ueh)We have to provide an implementation of the interface ... Read More

How to change/increase the heap size of the Java Virtual Machine in Java?

raja
Updated on 11-Feb-2020 10:21:05

1K+ Views

A Java program can execute in the Java Virtual Machine (JVM) uses the heap memory to manage the data. If our Java program requires more memory, there is a possibility that the Java Virtual Machine(JVM) will begin to throw OutOfMemoryError instances when attempting to instantiate an object in Java.To Change/increase the JVM Heap SizeIn Java, It is possible to increase the heap size allocated by the JVM using command line options-Xms - Set an initial Java heap size-Xmx - Set the maximum Java heap size-Xss - Set the Java thread stack sizeExampleLive Demo public class HeapSizeTest {    public static void main(String[]args){       // To get ... Read More

Can we define a static constructor in Java?

raja
Updated on 30-Jul-2019 22:30:26

4K+ Views

No, we cannot define a static constructor in Java, If we are trying to define a constructor with the static keyword a compile-time error will occur.In general, static means class level. A constructor will be used to assign initial values for the instance variables. Both static and constructor are different and opposite to each other. We need to assign initial values for an instance variable we can use a constructor. We need to assign static variables we can use Static Blocks.ExampleLive Demopublic class StaticConstructorTest {    int x = 10;    // Declaratiopn of Static Constructor    static StaticConstructorTest() { ... Read More

When can we use intern() method of String class in Java?

raja
Updated on 07-Feb-2020 05:45:33

317 Views

The intern() method of String class can be used to deal with the string duplication problems in Java. Using intern() we can save a lot of memory consumed by duplicate string instances. A string is duplicate if it contains the same content as another string but it can be occupied different memory locations.We know that JVM maintains a separate heap memory for string literals for the performance. Once we declare a string literal it will go to this pool and if another variable is assigned with the same literal value, it will be picked from the pool instead of creating a new ... Read More

Can we change the order of public static void main() to static public void main() in Java?

raja
Updated on 07-Feb-2020 05:47:14

3K+ Views

Yes, we can change the order of public static void main() to static public void main() in Java, the compiler doesn't throw any compile-time or runtime error. In Java, we can declare access modifiers in any order, the method name comes last, the return type comes second to last and then after it's our choice. But it's recommended to put access modifier (public, private and protected) at the forefront as per Java coding standards.Syntaxpublic static void main(String args[]) {    // some statements }ExampleLive Democlass ParentTest {    int age = 10;    public int getAge() {       age ... Read More

Can we extend an enum in Java?

raja
Updated on 07-Feb-2020 05:58:29

3K+ Views

No, we cannot extend an enum in Java. Java enums can extend java.lang.Enum class implicitly, so enum types cannot extend another class.Syntaxpublic abstract class Enum> implements Comparable, Serializable {    // some statements }EnumAn Enum type is a special data type which is added in Java 1.5 version.An Enum is used to define a collection of constants, when we need a predefined list of values which do not represent some kind of numeric or textual data, we can use an enum.Enums are constants and by default, they are static and final. so the names of an enum type fields are in uppercase letters.Public or protected modifiers can only ... Read More

What are the differences between a static block and a constructor in Java?

raja
Updated on 11-Feb-2020 10:10:35

2K+ Views

Static blockThe static blocks are executed at the time of class loading.The static blocks are executed before running the main () method.The static blocks don't have any name in its prototype.If we want any logic that needs to be executed at the time of class loading that logic needs to placed inside the static block so that it will be executed at the time of class loading.Syntaxstatic {    //some statements }ExampleLive Demopublic class StaticBlockTest {    static {       System.out.println("Static Block!");    }    public static void main(String args[]) {       System.out.println("Welcome to Tutorials Point!");    } }OutputStatic Block! ... Read More

Can we define a method name same as class name in Java?

raja
Updated on 30-Jul-2019 22:30:26

4K+ Views

Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur. But this is not recommended as per coding standards in Java. Normally the constructor name and class name always the same in Java.ExampleLive Demopublic class MethodNameTest {    private String str = "Welcome to TutorialsPoint";    public void MethodNameTest() { // Declared method name same as the class name       System.out.println("Both method name and class name are the same");    }    public static void main(String args[]) {       MethodNameTest test ... Read More

Can we declare a constructor as private in Java?

raja
Updated on 30-Jul-2019 22:30:26

9K+ Views

Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class. We can use this private constructor in the Singleton Design Pattern.Conditions for Private ConstructorA private constructor does not allow a class to be subclassed.A private constructor does not allow to create an object outside the class.If all the constant methods are there in our class we can use a private constructor.If all the methods are static then we can use a private constructor.If we try to extend a class which is having private constructor compile ... Read More

Advertisements