Java Articles - Page 176 of 440

What is need of introducing Generics, when Arrays already present in Java?

Maruthi Krishna
Updated on 09-Sep-2019 08:30:28

127 Views

Arrays in Java are used to store homogeneous datatypes, where generics allow users to dynamically choose the type (class) that a method, constructor of a class accepts, dynamically.By defining a class generic you are making it type-safe i.e. it can act up on any datatype. To understand generics let us consider an example −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 {    public static void main(String args[]) {       Student ... Read More

Is it mandatory to use T for type-parameter, while defining Generics classes/methods in Java?

Maruthi Krishna
Updated on 09-Sep-2019 08:26:15

748 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.To define a generic class you need to specify the type parameter you are using in the angle brackets “” after the class name and you can treat this as datatype of the instance variable an ... Read More

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

Are Generics applied at compile time or run time?

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

739 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

5K+ 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

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

4K+ 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

How can we set the border to JComboBox items in Java?

raja
Updated on 03-Jul-2020 11:58:06

741 Views

A JComboBox is a subclass of JComponent class and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate an ActionListener, ChangeListener and ItemListener interfaces when the user actions on a combo box. We can set the border to the items of a JComboBox by rendering a JComboBox which extends the DefaultListCellRenderer class and need to override the getListCellRendererComponent() method.Syntaxpublic Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)Exampleimport java.awt.*; import javax.swing.*; public class JComboBoxTest extends JFrame {    public JComboBoxTest() {       setTitle("JComboBox Test");       ... Read More

How to Get a slice of a primitive array in Java?

Maruthi Krishna
Updated on 15-Oct-2019 09:25:21

976 Views

You can get a part of a Java array in between two specified indexes in various ways.By Copying contents:One way to do so is to create an empty array and copy the contents of the original array from the start index to the endIndex.Example Live Demoimport java.util.Arrays; public class SlicingAnArray {    public static int[] sliceArray(int array[], int startIndex, int endIndex ){       int size = endIndex-startIndex;       int part[] = new int[size];       //Copying the contents of the array       for(int i=0; iarray[i]);       part = stream.toArray();       //Copying the contents of the array       for(int i=0; i

Assigning arrays in Java.

Maruthi Krishna
Updated on 06-Sep-2019 13:03:58

9K+ Views

While creating variables first of all we will declare them, initialize them, assign/re-assign values to them.Similarly, while creating arrays −You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;Assigning values to an arrayWhen we assign primitive values of one type to a variable of other (datatype) implicitly they are converted.But, when you try to assign a higher datatype ... Read More

How to import classes from within another directory/package in Java?

Maruthi Krishna
Updated on 06-Sep-2019 12:37:48

7K+ Views

In Java classes and interfaces related to each other are grouped under a package. The package is nothing but a directory storing classes and interfaces of a particular concept. For example, all the classes and interfaces related to input and output operations are stored in the java.io package.There are two types of packages namely user-defined packages and built-in packages (pre-defined)The import keywordWhenever you need to use the classes from a particular package −First of all, you need to set a classpath for the JAR file holding the required package.Import the required class from the package using the import keyword. While ... Read More

Advertisements