Found 9344 Articles for Object Oriented Programming

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();

How to replace Digits into String using Java?

Malhar Lathkar
Updated on 20-Jun-2020 13:19:39

1K+ Views

For this purpose, we create an object of HashMap class which is defined in java.util packageMap map = new HashMap();This hashmap object associates each digit with its corresponding word representationmap.put("0", "zero");Initialize an empty string object.String newstr="";Next, run a for loop over the length of given string and extract each character by substring() method of String class.Check if the character exists as a key in map object by containsKey() method. If it does, using it as key, obtain its value component in map and appenf\d to the new string. If not append the character itself to the new string. The complete code ... Read More

How do I write variable names in Java?

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

4K+ Views

While choosing an identifier to declare a variable in Java you need to keep the following points in mind. The name of the variable should begin with either alphabet or, an underscore (_) or, a dollar ($) sign. The identifiers used for variables must not be keywords. No spaces or special characters are allowed in the variable names of Java. Variable names may contain 0 to 9 numbers (if not at the beginning). Variable names are case sensitive i.e. MY_NUM is different from my_num. If you use two words in a identifier then the you should follow camel case ... Read More

How do I write method names in Java?

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

3K+ Views

While writing a method name we should follow the camel case i.e. first letter of the first word should be small and the first letters of the remaining (later) words should be capital. Example public class Test { public void sampleMethod() { System.out.println("This is sample method"); } public void demoMethod() { System.out.println("This is demo method"); } public static void main(String args[]) { Test obj = new Test(); obj.sample(); obj.demo(); } } Output This is sample method This is demo method

How do I write class names in Java?

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

4K+ Views

While writing class names you need to keep the following points in mind. You shouldn’t use predefined or existing class names as the name of the current class. You shouldn’t use any Java keywords as class name (with the same case). The First letter of the class name should be capital and remaining letters should be small (mixed case). class Sample Likewise, first letter of each word in the name should be capital an remaining letters should be small. class Test Keeping interface names simple and descriptive is suggestable. Better not ... Read More

Are identifiers hello and Hello same in Java?

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

177 Views

Identifiers in Java are case-sensitive, therefore, hello and Hello are considered as two different identifiers.

What is the difference between keywords and reserved words in Java?

Prabhas
Updated on 18-Feb-2020 11:11:37

524 Views

KeywordsKeywords in Java convey a special meaning to the compiler therefore, these cannot be used as identifiers. Java provides a set of 50 keywords.abstractcontinuefornewswitchassertdefaultgotopackagesynchronizedbooleandoifprivatethisbreakdoubleimplementsprotectedthrowbyteelseimportpublicthrowscaseenuminstanceofreturntransientcatchextendsintshorttrycharfinallongstrictfpvolatileconstfloatnativesuperwhileReserved wordsAmong the list of key words list mentioned above the key words goto and const are currently not in use. They are reserved words (for the future use).

Advertisements