Found 2616 Articles for Java

Does Java support multi-dimensional Arrays?

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

424 Views

No, Java does not support multi-dimensional arrays.Java supports arrays of arrays.In Java, a two-dimensional array is nothing but, an array of one-dimensional arrays.                  int[][] arr = new int[2][4];The expression arr[i] selects the one-dimensional array and the expression arr[i][j] selects the element from that array.Array indices in each dimension range from zero to "length". Where length is the array length in the given dimension. There is no array assignment operator. The number of dimensions and the size of each dimension is fixed once the array has been allocated.

Where to use a StringBuffer/StringBuilder than a String in Java?

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

207 Views

The String class objects are immutable whereas the StringBuffer and the StringBuilder objects are mutable.A StringBuffer is synchronized while a StringBuilder is not synchronized.A Concatenation operator "+" is internally implemented using either StringBuffer or StringBuilder.If the Object value is not going to change use String Class because a String object is immutable.If the Object value can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.If the Object value can change and will be modified by multiple threads, use a StringBuffer because StringBuffer is synchronized.

Why should we use a StringBuffer instead of a String in Java?

raja
Updated on 11-Feb-2020 08:04:56

719 Views

A StringBuffer is a thread-safe, mutable sequence of characters.Unlike a String class (immutable), the StringBuffer class is mutable. That is, we can change the contents of a StringBuffer object.When we modify a string of StringBuffer class, we are not creating a new String object, but rather operating directly on the original string itself.For this reason, the StringBuffer class offers a different set of methods than the String class, all of which operate directly on the buffer that contains the string. A StringBuffer can be defined simply by the use of the new operator and bypassing the string value inside a   ... Read More

What are the differences between the Heap memory and the String Constant Pool in Java?

raja
Updated on 06-Feb-2020 10:00:04

5K+ Views

Heap MemoryThe heap memory is a run time data area from which the memory for all java class instances and arrays is allocated.The heap is created when the JVM starts up and may increase or decrease in size while the application runs.The size of the heap can be specified using –Xms VM option. The heap can be of fixed size or variable size depending on the garbage collection strategy. Maximum heap size can be set using –Xmx option.By default, the maximum heap size is set to 64 MB. String Constant PoolString uses a special memory location to reuse of String objects ... Read More

Why String literal is stored in String Constant Pool in Java?

raja
Updated on 17-Nov-2023 14:10:14

2K+ Views

There are two ways to create a String object in Java By using the new operator String str = new String("Tutorials Point"); By using String literal String str = "Tutorials Point"; Whenever we call new String() in Java, it will create an object in the heap memory and String literals will go into String Constant Pool (SCP). For objects, JVM used SCP which is for efficient memory management in Java. Unlike other Java objects, instead of managing String object on the heap area, they introduced the String constant pool. One of important characteristic of String constant pool is that it does not create the same String ... Read More

Why String class is immutable or final in Java?

raja
Updated on 17-Nov-2023 14:21:41

7K+ Views

The string is immutable means that we cannot change the object itself, but we can change the reference to the object. The string is made final to not allow others to extend it and destroy its immutability. Security Parameters are typically represented as String in network connections, database connection URLs, usernames/passwords, etc. If it was mutable, these parameters could be changed easily. Synchronization and Concurrency making String immutable automatically makes them thread safe thereby solving the synchronization issues. Caching when compiler optimizes our String objects, it seems that if two objects have the same value (a =" test", and b =" test") and ... Read More

How to call an interface method in Java?

raja
Updated on 11-Feb-2020 08:06:59

18K+ Views

In order to call an interface method from a java program, the program must instantiate the interface implementation program. A method can then be called using the implementation object.Examplepublic interface InterfaceDemo{     default public void displayNameDefault(String name){        System.out.println("Your name is : " + name);    }     public void displayName(String name);     public void displayNameAndDesignation(String name, String designation); }The above interface defines three methods for displaying a name and optionally a job title. One method is a default method that contains implementation logic. The remaining two methods do not include implementation logic.public class InterfaceDemoImpl ... Read More

Why the constructor name is same as the class name in Java?

raja
Updated on 06-Feb-2020 10:09:33

3K+ Views

Every class object is created using the same new keyword, so it must have information about the class to which it must create an object. For this reason, the constructor name should be the same as the class name.Exampleclass MyConstructor{    public MyConstructor() {       System.out.println("The constructor name should be same as the class name");    }    public static void main(String args[]){       MyConstructor mc = new MyConstructor();    } }In the above program, the constructor name should be the same as the class name (MyConstructor).OutputThe constructor name should be same as the class name

Can we declare a main method as private in Java?

raja
Updated on 11-Feb-2020 07:49:21

4K+ Views

Yes, we can declare the main method as private in Java.It compiles successfully without any errors but at the runtime, it says that the main method is not public.Example:class PrivateMainMethod {    private static void main(String args[]){        System.out.println("Welcome to Tutorials Point");     } }The above code is working successfully at compile time but it will throw an error at the runtime.Output:Error: Main method not found in class PrivateMainMethod, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Can we execute a java program without a main method?

raja
Updated on 07-Oct-2023 02:45:53

26K+ Views

Yes, we can execute a java program without a main method by using a static block.  Static block in Java is a group of statements that gets executed only once when the class is loaded into the memory by Java ClassLoader, It is also known as a static initialization block. Static initialization block is going directly into the stack memory. Example class StaticInitializationBlock{    static{       System.out.println("class without a main method");       System.exit(0);    } } In the above example, we can execute a java program without a main method (works until Java 1.6 version). ... Read More

Advertisements