Found 9320 Articles for Object Oriented Programming

How to populate an array one value at a time by taking input from user in Java?

Priya Pallavi
Updated on 19-Feb-2020 11:29:27

6K+ Views

To read data from user create a scanner class. Read the size of the array to be created from the user using nextInt() method. Create an array with the specified size. In the loop read the values from the user and store in the array created above.Exampleimport java.util.Arrays; import java.util.Scanner; public class PopulatingAnArray {    public static void main(String args[]) {       System.out.println("Enter the required size of the array :: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];     ... Read More

How do I write package names in Java?

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

800 Views

While choosing a package name you need to keep the following points in mind. The name of the package should be in small letters. It is suggested to start the name of the package with the top level domain level followed by sub domains, ex: com.example.tutorialspoint. Example You can declare a package as in. package com.mypackage.tutorialspoint; Public class Sample { Public static void main(String args[]) { System.out.println("Welcome to Tutorialspoint"); } } Executing a package You need to compile the file with packages using –d ... Read More

How to store the contents of an URL in java?

Nikitha N
Updated on 19-Feb-2020 11:27:38

613 Views

The openStream() method of the URL class opens a connection and returns an InputStream. Using this stream, you can you read contents from that connection.Exampleimport java.io.InputStream; import java.net.URL; import java.util.Scanner; public class ReadingURL {    public static void main(String args[]) throws Exception {       URL url = new URL("http://www.tutorialspoint.com/");       InputStream inputStream = url.openStream();       Scanner s = new Scanner(inputStream);       while(s.hasNext()) {          System.out.println(s.nextLine());       }    } }OutputLearn CouchDB Learn DB2    Learn DocumentDB SQL Learn DocumentDB Learn DynamoDB    Learn H2 Database Learn HSQLDB Learn IMS DB …………………………………………………………………………………………. ………………………………………………………………………………………………… ………………………………………………………………………………………………

How do I write interface names in Java?

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

542 Views

While writing an interface/class names you need to keep the following points in mind. First letter of the interface/class name should be capital and remaining letters should be small (mixed case). interface Sample Likewise, first letter of each word in the name should be capital an remaining letters should be small. interface MYInterface Keeping interface names simple and descriptive is suggestable. Better not to use acronyms while writing interface/class names. Example Live Demo interface MyInterface { void sample(); void demo(); } ... Read More

How do I write constructor names in Java?

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

961 Views

Declaring a constructor is in Java is similar to methods. While naming the constructor of a class in Java you need to keep the following points in mind. The name of the constructor should be the same (same case) as the class name (along with the access specifier). A constructor should not have any return type. Constructor cannot be static, final, abstract and, synchronized. Example Following Java program is an example of a constructor. Live Demo public class Sample { Sample() { System.out.println("This is an example of ... Read More

Why the main method has to be in a java class?

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

306 Views

Main method is the entry point of the execution in Java. When we execute a class JVM searches for the main method and execute the contents of it line by line. If you observe the following example you can compile a this program but if you try to execute it you will get an error saying “Main method not found”. Example abstract class SuperTest { public abstract void sample(); public abstract void demo(); } public class Example extends SuperTest{ public void sample(){ System.out.println("sample method ... Read More

Can a method return multiple values in Java?

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

9K+ Views

You can return only one value in Java. If needed you can return multiple values using array or an object. Example In the example given below the calculate() method accepts two integer variables performs the addition subtraction, multiplication and, division operations on them stores the results in an array and returns the array. public class ReturningMultipleValues { static int[] calculate(int a, int b){ int[] result = new int[4]; result[0] = a + b; result[1] = a - b; ... Read More

Can you have a method in Java with varying number of arguments?

Prabhas
Updated on 16-Jun-2020 09:34:09

1K+ Views

Yes, we can write a method using variable arguments once you use variable arguments as a parameter method while calling you can pass as many numbers of arguments to this method (variable number of arguments) or, you can simply call this method without passing any arguments.ExampleLive Demopublic class Sample{    void demoMethod(String... args) {       for (String arg : args) {          System.out.println(arg);       }    }    public static void main(String args[] ){       new Sample().demoMethod("ram", "rahim", "robert");       new Sample().demoMethod("krishna", "kasyap");       new Sample().demoMethod();    } }Outputram rahim robert krishna kasyap

Are there inline functions in Java?

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

3K+ Views

If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. Any change to an inline function could require all clients of the function to be recompiled because compiler would need to replace all the code once again otherwise it will continue with old functionality. No, Java does not provide inline functions it is typically done by the JVM at execution time.

What is the difference between non-static methods and abstract methods in Java?

radhakrishna
Updated on 19-Dec-2019 06:36:11

1K+ Views

Following are the notable differences between non-static methods and abstract methods.Non-static (normal) methodsAbstract methodsThese methods contain a body.Abstract methods don’t have body these are ended with a semicolonYou can use normal method directly.You cannot use abstract methods directly, to use them you need to inherit them and provide body to these methods and use them.Example:public void display() {    System.out.println("Hi"); }Example:public void display();

Advertisements