Found 4338 Articles for Java 8

How to convert String to StringBuilder and vice versa Java?

Maruthi Krishna
Updated on 02-Jul-2020 10:32:03

18K+ Views

The String type is a class in Java, it is used to represent a set of characters. Strings in Java are immutable, you cannot change the value of a String once created.Since a String is immutable, if you try to reassign the value of a String. The reference of it will be pointed to the new String object leaving an unused String in the memory.Java provides StringBuffer class as a replacement of Strings in places where there is a necessity to make a lot of modifications to Strings of characters.You can modify/manipulate the contents of a StringBuffer over and over again ... Read More

Why String class is popular key in a HashMap in Java?

Maruthi Krishna
Updated on 02-Jul-2020 10:32:54

3K+ Views

A map is a collection in Java which stores key value pairs. The keys of this must not be null and each key should point to only one value. It is represented by the Map interface of java.util package. There are various classes which provides implementation to this interface.The HashMap is a class which implements the Map interface. It is based on the Hash table. It allows null values and null keys.In short, you can store key value pairs in the HashMap object. Once you do so you can retrieve the values of the respective keys but, the values we ... Read More

Is it possible to synchronize the string type in Java?

Maruthi Krishna
Updated on 02-Jul-2020 10:33:35

1K+ Views

A thread is a piece of code (under execution) in a program, which executes a sub task of the process independently. independent process.In other words, a thread is a light weight process which executes a piece of code independently.Thread synchronizationIf a process has multiple threads running independently at the same time (multi-threading) and if all of them trying to access a same resource an issue occurs.To resolve this, Java provides synchronized blocks/ synchronized methods. If you define a resource (variable/object/array) inside a synchronized block or a synchronized method, if one thread is using/accessing it, other threads are not allowed to ... Read More

Why does Java strictly specify the range and behavior of its primitive types?

Maruthi Krishna
Updated on 01-Aug-2019 12:31:17

211 Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double.Java strictly specifies range and behaviors of all the primitive datatypes. Making the users choose the required datatypes based on the application thus reducing the unused occupancy of memory.For example, if you need to store an integer constant of single digit using integer would be a waste of memory instead, you can use byte type since 8 bits would be necessary to store it.ExampleFollowing Java example lists out the ranges of the primitive datatypes.public class ... Read More

Write a program to print message without using println() method in java?

Maruthi Krishna
Updated on 02-Jul-2020 10:34:27

5K+ Views

The println() method of the System class accepts aStringas parameter an prints the given String on the console.Examplepublic class PrintData {    public static void main(String args[]) {       System.out.println("Hello how are you");    } }OutputHello how are youIn addition to this you can print data on the console in several other ways, some of them are −Using Output StreamsUsing output stream classes, you can write dat on the specified destination. You can print data on the screen/console by passing the standard output Stream object System.out as source to them.Exampleimport java.io.IOException; import java.io.OutputStreamWriter; public class PrintData {   ... Read More

List out the default values of numeric and non-numeric primitive data types in Java?

Maruthi Krishna
Updated on 01-Aug-2019 12:21:52

392 Views

When you create instance variables in Java you need to initialize them, else the compiler will initialize on your behalf with default values which are −byte: 0short: 0int: 0long: 0float: 0.0double: 0.0boolean: falsestring: nullExampleIn the following Java program prints the default values of the numeric and non-numeric primitive variables in java.public class DefaultValues {    byte byteVariable;    short shortVariable;    int intVariable;    long longVaraible;    float floatVariable;    double doubleVariable;    boolean boolVariable;    String stringVariable;    public static void main(String args[]){       DefaultValues obj = new DefaultValues();       System.out.println("Default values of numeric variables ... Read More

If I change the return type, will the method gets overloaded in java?

Maruthi Krishna
Updated on 02-Jul-2020 10:08:20

926 Views

When a class has two or more methods by the same name but different parameters, at the time of calling, based on the parameters passed, respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.Exampleclass Test{    public int addition(int a, int b){       int result = a+b;       return result;    }    public int addition (int a, int b, int c){       int result = a+b+c;       return result;    }    public static void main(String args[]){ ... Read More

Sequence of execution of, instance method, static block and constructor in java?

Maruthi Krishna
Updated on 02-Jul-2020 10:09:31

7K+ Views

A static block is a block of code with a static keyword. In general, these are used to initialize the static members of a class. JVM executes static blocks before the main method at the time loading a class.Examplepublic class MyClass {    static{       System.out.println("Hello this is a static block");    }    public static void main(String args[]){       System.out.println("This is main method");    } }OutputHello this is a static block This is main methodA constructor is similar to method and it is invoked at the time creating an object of the class, it is ... Read More

Can we call a constructor directly from a method in java?

Maruthi Krishna
Updated on 02-Jul-2020 10:12:42

9K+ Views

A constructor is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class and, have no return type.There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.The this keyword in Java is a reference to the object of the current class. Using it, you can refer a field, method or, constructor of a class.Therefore, if you need to invoke a constructor explicitly you can do so, using "this()".Invoking a ... Read More

How to call the constructor of a superclass from a constructor in java?

Maruthi Krishna
Updated on 01-Aug-2019 12:09:16

5K+ Views

Whenever you inherit/extend a class, a copy of superclass’s members is created in the subclass object and thus, using the subclass object you can access the members of both classes.ExampleIn the following example we have a class named SuperClass with a method with name demo(). We are extending this class with another class (SubClass).Now, you create an object of the subclass and call the method demo().class SuperClass{    public void demo() {       System.out.println("demo method");    } } public class SubClass extends SuperClass {    public static void main(String args[]) {       SubClass obj = new ... Read More

Advertisements