Java Articles

Page 43 of 450

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 746 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 244 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 245 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 916 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 173 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 431 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

Get file extension name in Java

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

The file extension is the suffix that is attached to the computer file and it denotes the format of the file. A program that demonstrates getting the file extension name is given as follows −Exampleimport java.io.File; public class Demo {    private static String fileExtension(File file) {       String name = file.getName();       if(name.lastIndexOf(".") != -1 && name.lastIndexOf(".") != 0)          return name.substring(name.lastIndexOf(".") + 1);       else          return "";    }    public static void main(String[] args) {       File file = new File("demo1.txt");   ...

Read More

File Path with double slash in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

The double slash in the file path is required as to create the ’\’ character , another ‘\’ needs to be added to escape it. A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       File file = new File("C:\jdk11.0.2\demo1.txt");       System.out.println(file);    } }The output of the above program is as follows −OutputC:\jdk11.0.2\demo1.txtNow let us understand the above program.The file path of the file is provided with double slash and then it is printed. A code snippet that demonstrates this is given ...

Read More
Showing 421–430 of 4,498 articles
« Prev 1 41 42 43 44 45 450 Next »
Advertisements