Found 4338 Articles for Java 8

Can we use Switch statement with Strings in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

9K+ Views

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.Syntaxswitch(expression) {    case value :       // Statements       break;    case value :       // Statements       break;       // You can have any number of case statements.       default :     // Statements }Strings in switchYes, we can use a switch statement with Strings in Java. While doing so you need ... Read More

What happens when you add a double value to a String in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

2K+ Views

The “+” operator with a String acts as a concatenation operator.Whenever you add a String value to a double using the “+” operator, both values are concatenated resulting a String object.In-fact adding a double value to String is the easiest way to convert a double value to Strings.Exampleimport java.util.Scanner;    public class StringsExample {       public static void main(String args[]){          Scanner sc = new Scanner(System.in);          System.out.println("Enter a double value: ");          double d = sc.nextDouble();          System.out.println("Enter a String value: ");       ... Read More

What are the rules to be followed while using varargs in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

225 Views

Since JSE1.5 you can pass a variable number of values as argument to a method. These arguments are known as var args and they are represented by three dots (…)Syntaxpublic myMethod(int ... a) { // method body }Rules to follow while using varargs in JavaWe can have only one variable argument per method. If you try to use more than one variable arguments a compile time error is generated.ExampleIn the following Java example we are trying to accepts two varargs from the method sample().public class VarargsExample{ void demoMethod(int... ages), String... names) { ... Read More

Explain about Varargs in java?

Deepti S
Updated on 29-Aug-2023 15:41:22

274 Views

A method can accept a variable number of parameters thanks to a Java feature called variable arguments, or varargs. This can be helpful for methods that need to be able to manage an unknown or variable number of inputs. We had two ways to handle methods that needed to take a variable number of arguments prior to introduction of varargs in Java 5. One way was to use overloaded methods. The method has to be developed in numerous iterations, each with a different number of arguments. If the process had many arguments, it might become complicated and challenging ... Read More

What is variable shadowing in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

4K+ Views

In Java you can declare three types of variables namely, instance variables, static variables and, local variables.Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.Class (static) variables − Class variables are variables declared within a class, ... Read More

Explain about field hiding in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

953 Views

Whenever you inherit a superclass a copy of superclass’s members is created at the subclass and you using its object you can access the superclass members.If the superclass and the subclass have instance variable of same name, if you access it using the subclass object, the subclass field hides the superclass’s field irrespective of the types. This mechanism is known as field hiding.But, since it makes code complicated field hiding is not recommended.ExampleIn the following example we have two classes Super and Sub one extending the other. They both have two fields with same names (name and age).When we print ... Read More

How to convert a String to an enum in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ Views

The valueOf() method of the Enum class in java accepts a String value and returns an enum constant of the specified type.ExampleLet us create an enum with name Vehicles with 5 constants representing models of 5 different scoters with their prices as values, as shown below −enum Vehicles { //Constants with values ACTIVA125(80000), ACTIVA5G(70000), ACCESS125(75000), VESPA(90000), TVSJUPITER(75000); //Instance variable private int price; //Constructor to initialize the instance variable Vehicles(int price) { this.price = price; } ... Read More

How to iterate the values in an enum in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

15K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }You can iterate the contents of an enumeration using for loop, using forEach loop and, using java.util.stream.Using for loopYou can retrieve the contents of an enum using the values() method. This method returns an array containing all the values. Once you obtain the array you can iterate it using the for loop.ExampleFollowing java program iterates ... Read More

How to use an enum with switch case in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

1K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }You can also define an enumeration with custom values to the constants declared. But you need to have an instance variable, a constructor and getter method to return values.Enum with switch caseLet us create an enum with 5 constants representing models of 5 different scoters with their prices as values, as shown below −enum Scoters { ... Read More

Can we create an enum with custom values in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

8K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values (Strings in general). You can use enumerations to store fixed values such as days in a week, months in a year etc.enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Custom values to the constantsInstead of declaring just string constants in an enum, you can also have values to these constants as −enum Vehicles { ACTIVA125(80000), ACTIVA5G(70000), ACCESS125(75000), VESPA(90000), TVSJUPITER(75000); }Whenever, you need to assign custom values to the constants of an enum −To hold the value of each ... Read More

Advertisements