Found 9320 Articles for Object Oriented Programming

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

199 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

564 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).

What are valid identifiers in Java?

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

679 Views

A valid identifier in java – Must begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). Can have any combination of characters after the first character. Cannot be a keyword. Example Following example shows various possible identifiers used to declare a variable in Java. Live Demo public class VariableTest { public static void main(String args[]) { // Declaring a variable named num int num = 1; ... Read More

What is the difference between /* */ and /** */ comments in Java?

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

560 Views

Multiline comments (/* */) are used to comment multiple lines in the source code. Example Live Demo public class CommentsExample { /* Following is the main method here, We create a variable named num. And, print its value * */ public static void main(String args[]) { //Declaring a variable named num int num = 1; ... Read More

How to write single line comment in Java?

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

455 Views

To comment a particular line just place ‘double back slash (//)’ before the line as shown below. // Hello this line is commented Example Following example demonstrates the usage of single line comments in Java. Live Demo public class CommentsExample { public static void main(String args[]) { //Declaring a variable named num int num = 1; //Printing the value of the variable num System.out.println("value if the variable num: "+num); } } Output value if the variable num: 1

Is there a need to import Java.lang package while running Java programs?

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

522 Views

The java.lang package is the default package in Java, by default, it will be imported. Therefore, there is no need to import this package explicitly. i.e. without importing you can access the classes of this package. Example If you observe the following example here we haven’t imported the lang package explicitly but, still, we are able to calculate the square root of a number using the sqrt() method of the java.lang.Math class. Live Demo public class LangTest { public static void main(String args[]) { int num = 100; ... Read More

Advertisements