Java Articles

Page 42 of 450

How to import all classes in Java?

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 13K+ Views

All the classes in a package can be imported using the import statement along with the character *. For example - All the classes in the package java.util can be imported using import java.util.*;A program that demonstrates this in Java is given as follows:Exampleimport java.util.*; public class Demo {    public static void main(String args[]) {       Stack stack = new Stack();       stack.push("Apple");       stack.push("Mango");       stack.push("Guava");       stack.push("Pear");       stack.push("Orange");       System.out.println("The stack elements are: " + stack);    } }OutputThe stack elements are: ...

Read More

Use static Import for sqrt() and pow() methods in class Math in Java

Fendadis John
Fendadis John
Updated on 11-Mar-2026 1K+ Views

Static import means that the fields and methods in a class can be used in the code without specifying their class if they are defined as public static.The Math class methods sqrt() and pow() in the package java.lang are static imported. A program that demonstrates this is given as follows:Exampleimport static java.lang.Math.sqrt; import static java.lang.Math.pow; public class Demo {    public static void main(String args[]) {       double num = 4.0;       System.out.println("The number is: " + num);       System.out.println("The square root of the above number is: " + sqrt(num));       System.out.println("The ...

Read More

Initializer for final static field in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 1K+ Views

The final static field variable is a constant variable. There is only one copy of this variable available. It is mandatory to initialize the final static field variable explicitly as the default value for it is not provided by the JVM. Also, this variable cannot be reinitialized.A program that initializes the final static field variable using a static initialization block is given as follows:Examplepublic class Demo {    final static int num;    static {       System.out.println("Running static initialization block.");       num = 6;    }    public static void main(String[] args) {       ...

Read More

Finding all words that start with an 'a' in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 718 Views

All the words that start with a can be found in a string by using regular expressions in Java. The regular expressions are character sequences that can be used to match other strings using a specific pattern syntax. The regular expressions are available in the java.util.regex package which has many classes but the most important are Pattern class and Matcher class.A program that finds all words that start with an 'a' is using regular expressions is given as follows:Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) throws Exception {       String str ...

Read More

A Reluctant qualifier in Java Regular Expressions

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 230 Views

The reluctant qualifier starts with the shortest string size as possible. If a match is found by the engine, the process continues to find more matches otherwise the engine adds a character to the searched string section and tries again. This continues until a match is obtained or the string is used up.The regex "B+?" is used to find the match in the string "SkyIsBlue".A program that demonstrates this is given as follows:Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       String regex = "B+?";       String str = ...

Read More

Use a character class in Java Regular Expressions

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 226 Views

A character class is set of characters that are enclosed inside square brackets. The characters in a character class specify the characters that can match with a character in an input string for success. An example of a character class is [a-z] which denotes the alphabets a to z.A program that demonstrates a character class in Java Regular Expressions is given as follows:Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("[a-z]+");       Matcher m = p.matcher("the sky is blue");       System.out.println("The input string ...

Read More

Name validation using Java Regular Expressions

Fendadis John
Fendadis John
Updated on 11-Mar-2026 1K+ Views

The name can be validated using the java.util.regex.Pattern.matches() method. This method matches the regular expression for the name and the given input name and returns true if they match and false otherwise.A program that demonstrates this is given as follows:Examplepublic class Demo {    public static void main(String args[]) {       String name = "John Harry Smith";       String regexName = "\p{Upper}(\p{Lower}+\s?)";       String patternName = "(" + regexName + "){2, 3}";       System.out.println("The name is: " + name);       System.out.println("Is the above name valid? " + name.matches(patternName));    } ...

Read More

Create a directory in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 907 Views

A directory can be created with the required abstract path name using the method java.io.File.mkdir(). This method requires no parameters and it returns true on the success of the directory creation or false otherwise.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("c:\demo1");          file.createNewFile();          boolean flag = file.mkdir();          System.out.print("Directory created? " + flag);       } catch(Exception e) {     ...

Read More

Display File class constants in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 164 Views

The java.io.File class has display constants File.separatorChar and File.pathSeparatorChar mainly. The File.separatorChar is ‘/’ and the File.pathSeparatorChar is ‘:’ for Unix.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          System.out.println("File.pathSeparatorChar = " + File.pathSeparatorChar);          System.out.println("File.separatorChar = " + File.separatorChar);       } catch(Exception e) {          e.printStackTrace();       }    } }The output of the above program is as follows −OutputFile.pathSeparatorChar = : File.separatorChar = /Now let us ...

Read More

List the file system roots in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 416 Views

The method java.io.File.listRoots() is used to list the file system roots in Java. This method requires no parameters. It returns the available file system roots in the form of an array of file objects and if the file system roots cannot be determined, it returns null.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       File[] roots = File.listRoots();       try {          for(File r : roots) {             System.out.println(r);          } ...

Read More
Showing 411–420 of 4,498 articles
« Prev 1 40 41 42 43 44 450 Next »
Advertisements