Found 4338 Articles for Java 8

What are generic methods in Java?

Maruthi Krishna
Updated on 09-Sep-2019 07:52:25

606 Views

Similar to generic classes you can also define generic methods in Java. These methods use their own type parameters. Just like local variables, the scope of the type parameters of the methods lies within the method.While defining a generic method you need to specify the type parameter within the angle brackets (< T >). This should be placed before the method's return type.You can have multiple type parameters separated by commas. A type parameter, also known as a type variable, is an identifier that specifies a generic type name.The type parameters can be used to declare the return type and ... Read More

Can we throw an object of generic class in java?

Maruthi Krishna
Updated on 09-Sep-2019 07:48:27

506 Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any data type.Example Live Democlass Student{    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample ... Read More

Why can't Java generics be used for static methods?

Maruthi Krishna
Updated on 09-Sep-2019 07:42:47

3K+ Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Generics for static methodsWe can use generics for static methods Live Demopublic class GenericMethod {    static void sampleMethod(T[] array) {       for(int i=0; i

Can you create an array of Generics type in Java?

Maruthi Krishna
Updated on 09-Sep-2019 07:33:09

1K+ Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Example Live Democlass Student{    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample { ... Read More

Passing primitive values while instantiating a parameterized type (generic) in Java?

Maruthi Krishna
Updated on 09-Sep-2019 07:36:48

428 Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Example Live Democlass Student{    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample { ... Read More

Are Generics applied at compile time or run time?

Maruthi Krishna
Updated on 09-Sep-2019 07:17:53

470 Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Example Live Democlass Student{    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample { ... Read More

Is it possible to instantiate Type-parameter in Java?

Maruthi Krishna
Updated on 09-Sep-2019 07:14:13

4K+ Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Example Live Democlass Student {    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample ... Read More

The readUTF() and writeUTF() methods in Java

Maruthi Krishna
Updated on 06-Sep-2019 14:10:39

3K+ Views

Unicode (UTF) − Stands for Unicode Translation Format. It is developed by The Unicode Consortium. if you want to create documents that use characters from multiple character sets, you will be able to do so using the single Unicode character encodings. It provides 3 types of encodings.UTF-8 − It comes in 8-bit units (bytes), a character in UTF8 can be from 1 to 4 bytes long, making UTF8 variable width.UTF-16-8 − It comes in 16-bit units (shorts), it can be 1 or 2 shorts long, making UTF16 variable width.UTF-32 − It comes in 32-bit units (longs). It is a fixed-width ... Read More

How many ways are there to initialize the instance variables of a class in java?

Maruthi Krishna
Updated on 06-Sep-2019 14:01:45

3K+ Views

You can initialize the instance variables of a class using final methods, constructors or, Instance initialization blocks.Final methodsWhenever you make a method final, you cannot override it. i.e. you cannot provide implementation to the superclass’s final method from the subclass.i.e. The purpose of making a method final is to prevent modification of a method from outside (child class). You can also use these final methods to initialize the instance variables.Example Live Demoimport java.util.Scanner; public class FinalMethods {    int age = getAge();    String name = getName();    static Scanner sc = new Scanner(System.in);    public static final int getAge() { ... Read More

Util.Arrays and Reflect.Array in Java

Maruthi Krishna
Updated on 06-Sep-2019 13:57:13

68 Views

The util.Arrays classThe java.util.Arrays class contains a static factory that allows arrays to be viewed as lists. Following are the important points about Arrays −This class contains various methods for manipulating arrays (such as sorting and searching).The methods in this class throw a NullPointerException if the specified array reference is null.For example, the equals() method of this class accepts two arrays and compares them.Example Live Demoimport java.util.Arrays; public class ComparingArrays {    public static void main(String args[]){       String[] myArray1 = {"JavaFX", "HBase", "OpenCV", "WebGL", "FlexBox"};       String[] myArray2 = {"JavaFX", "HBase", "OpenCV", "WebGL", "FlexBox"};     ... Read More

Advertisements