Found 4338 Articles for Java 8

How to store a 2d Array in another 2d Array in java?

Abhinanda Shri
Updated on 19-Feb-2020 11:22:53

3K+ Views

Create an array to which you want to store the existing array with the same length. A 2d array is an array of one dimensional arrays therefore, to copy (or, to perform any operation on) the elements of the 2d array you need two loops one nested within the other. Where, the outer loop is to traverse through the array of one dimensional arrays and, the inner loop is to traverse through the elements of a particular one dimensional array.Examplepublic class Copying2DArray {    public static void main(String args[]) {       int[][] myArray = {{41, 52, 63}, {74, ... Read More

Can we overload the main method in Java?

radhakrishna
Updated on 30-Jul-2019 22:30:20

3K+ Views

Yes, we can overload the main method in Java, but When we execute the class JVM starts execution with public static void main(String[] args) method. Example Live Demo public class Sample{ public static void main(){ System.out.println("This is the overloaded main method"); } public static void main(String args[]){ Sample obj = new Sample(); obj.main(); } } Output This is the overloaded main method

Why is method overloading not possible by changing the return type of the method only in java?

mkotla
Updated on 30-Jul-2019 22:30:20

2K+ Views

Overloading is the mechanism of binding the method call with the method body dynamically based on the parameters passed to the method call. If you observe the following example, it contains two methods with same name, different parameters and if you call the method by passing two integer values the first method will be executed and, if you call by passing 3 integer values then the second method will be executed.It is not possible to decide to execute which method based on the return type, therefore, overloading is not possible just by changing the return type of the method. Example ... Read More

What is method hiding in Java and how to use it?

Giri Raju
Updated on 30-Jul-2019 22:30:20

2K+ Views

When super class and sub class contains same method including parameters and if they are static. The method in the super class will be hidden by the one that is in the sub class. This mechanism is known as method hiding. Example Live Demo class Demo{ public static void demoMethod() { System.out.println("method of super class"); } } public class Sample extends Demo { public static void demoMethod() { System.out.println("method of sub class"); } public static void main(String args[] ) { Sample.demoMethod(); } } Output method of sub class

What is the difference between method hiding and method overriding in Java?

Sreemaha
Updated on 30-Jul-2019 22:30:20

1K+ Views

When super class and the sub class contains same instance methods including parameters, when called, the super class method is overridden by the method of the sub class. WIn this example super class and sub class have methods with same signature (method name and parameters) and when we try to invoke this method from the sub class the sub class method overrides the method in super class and gets executed. Example Live Demo class Super{ public void sample(){ System.out.println("Method of the Super class"); } } public ... Read More

Why can’t we override static methods in Java?

varma
Updated on 30-Jul-2019 22:30:20

5K+ Views

Overloading is the mechanism of binding the method call with the method body dynamically based on the parameters passed to the method call.Static methods are bonded at compile time using static binding. Therefore, we cannot override static methods in Java.

What is an array data structure in Java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:20

157 Views

Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array. Element − Each item stored in an array is called an element. Index − Each location of an element in an array has a numerical index, which is used to identify the element. Example Live Demo public class ArrayExample { public static void main(String args[]){ int myArray[] = {44, 69, 89, 635}; for (int i = 0; i

What are the types of arrays in Java?

Lakshmi Srinivas
Updated on 30-Jul-2019 22:30:20

15K+ Views

There are two types of arrays in Java they are − Single dimensional array − A single dimensional array of Java is a normal array where, the array contains sequential elements (of same type) − int[] myArray = {10, 20, 30, 40} Example Live Demo public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements ... Read More

What are all the ways an object can be created in Java?

varun
Updated on 16-Jun-2020 09:03:59

138 Views

You can create an objectUsing new keyword.Sample obj = new Sample();Using the newInstance() method and Class.forName() method.Sample obj2 = (Sample) Class.forName("Sample").newInstance();Using the clone() method by implementing Cloneable Interface (marker).Sample obj3 = (Sample) obj1.clone();Using class loader.Object obj4 = Sample.class.getClassLoader().loadClass("Sample");Using the constructor class from lang.reflect.Class cls = Sample.class; Constructor obj = cls.getDeclaredConstructors()[0]; Sample obj5 = (Sample) obj.newInstance();

How to create an object from class in Java?

Prabhas
Updated on 19-Feb-2020 07:39:44

2K+ Views

An object is created from a class using the new keyword. There are three steps when creating an object from a class −Declaration − A variable declaration with a variable name with an object type.Instantiation − The 'new' keyword is used to create the object.Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.Examplepublic class Sample {    public static void main(String args[]){       Sample s = new Sample();    } }

Advertisements