- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2616 Articles for Java
1K+ Views
Once you initialize a final variable you cannot modify its value further. i.e. You can assign value to a final variable only once. If you try to assign value to a final variable a compile time error will be generated.Example Live Demopublic class FinalExample { final int j = 100; public static void main(String args[]){ FinalExample obj = new FinalExample(); obj.j = 500; System.out.println(obj.j); } }Compile time errorFinalExample.java:6: error: cannot assign a value to final variable j obj.j = 500; ^ 1 errorAchieving the ... Read More
837 Views
Once you declare a variable final, after initializing it, you cannot modify its value further. Moreover, like instance variables, final variables will not be initialized with default values.Therefore, it is mandatory to initialize final variables once you declare them. If not a compile time error will be generated.Example Live Demopublic class FinalExample { final int j; public static void main(String args[]){ FinalExample obj = new FinalExample(); System.out.println(obj.j); } }Compile time errorFinalExample.java:5: error: non-static variable j cannot be referenced from a static context System.out.println(j); ^ 1 errorInitializing the final variableYou can initialize a ... Read More
4K+ Views
The contains() method of the String class accepts Sting value as a parameter, verifies whether the current String object contains the specified String and returns true if it does (else false).The toLoweCase() method of the String class converts all the characters in the current String into lower case and returns.To find whether a String contains a particular sub string irrespective of case −Get the String.Get the Sub String.Convert the string value into lower case letters using the toLowerCase() method, store it as fileContents.Convert the string value into lower case letters using the toLowerCase() method, store it as subString.Invoke the contains() ... Read More
14K+ Views
The get() method of the ArrayList class accepts an integer representing the index value and, returns the element of the current ArrayList object at the specified index.Therefore, if you pass 0 to this method you can get the first element of the current ArrayList and, if you pass list.size()-1 you can get the last element.Example Live Demoimport java.util.ArrayList; public class FirstandLastElemets{ public static void main(String[] args){ ArrayList list = new ArrayList(); //Instantiating an ArrayList object list.add("JavaFX"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); ... Read More
2K+ Views
Using the subList() and the clear() methodsThe subList() method of the List interface accepts two integer values representing indexes of the elements and, returns a view of the of the current List object removing the elements between the specified indices.The clear() method of List interface removes all the elements from the current List object.Therefore, to remove a specific sub list of an array list you just need to call these two methods on your list object by specifying the boundaries of the sub list you need to remove as −obj.subList().clear();Example Live Demoimport java.util.ArrayList; public class RemovingSubList { public static void ... Read More
278 Views
Using copyOf() methodThe copyOf() method of the Arrays class (java.util package) accepts two parameters −an array (of any type).an integer value representing length.And copies the contents of the given array from starting position to given length and returns the new array.Example Live Demoimport java.util.Arrays; public class CopyingSectionOfArray { public static void main(String[] args) { String str[] = new String[10]; //Populating the array str[0] = "Java"; str[1] = "WebGL"; str[2] = "OpenCV"; str[3] = "OpenNLP"; str[4] = "JOGL"; ... Read More
4K+ Views
To validate a string for alphabets you can either compare each character in the String with the characters in the English alphabet (both cases) or, use regular expressions.Example1The following program accepts a string value (name) from the user and finds out whether given string is a proper name by comparing each character in it with the characters in the English alphabet. Live Demoimport java.util.Scanner; public class ValidatingString { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter your name: "); String str = sc.next(); boolean ... Read More
3K+ Views
To sort the string that contains numbers in java −Get the String.Create an empty integer array.The split() method of the string class accepts a string representing a delimiter, splits the current string into an array of tokens. Using this method split the given string into an array of tokens.The parseInt() method of the Integer class accepts a String value and converts it into an integer. Convert each element in the String array obtained in the previous step into an integer and store in into the integer array.The sort() method of the Arrays class accepts an array, sorts the contents of ... Read More
10K+ Views
The Boolean class of the lang package provides two method namely parseBoolean() and valueOf().parseBoolean(String s) − This method accepts a String variable and returns boolean. If the given string value is "true" (irrespective of its case) this method returns true else, if it is null or, false or, any other value it returns false.valueOf(String s) − This method accepts a String value, parses it and returns an object of the Boolean class based on the given value. You can use this method instead of the constructor. If the given String value is "true" this method returns true or, it returns ... Read More
792 Views
Using the Runtime classJava provides a class named java.lang.Runtime, using this class you can interface with the current environment.The getRunTime() (static) method of this class returns a Runtime object associated with the current application.The exec() method accepts a String value representing the command to execute a process in the current environment (system) and executes it.Therefore, to execute an external application using the Runtime class −Get the run time object using the getRuntime() method.Execute the required process by passing the path of it as a String value to the exec() method.Exampleimport java.io.IOException; public class Trail { public static void main(String ... Read More