VOOZH about

URL: https://www.geeksforgeeks.org/java/java-file-class-compareto-method-with-examples/

⇱ Java File Class compareTo() Method with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java File Class compareTo() Method with Examples

Last Updated : 2 Feb, 2022

The compareTo() function compares two pathnames lexicographically. In Java, the method may be used to sort files. This type of activity is dependent on the system on which JVM is installed. When comparing pathnames on Unix systems, the alphabetic case matters, while it doesn't on Windows.

Syntax:

public int compareTo(File pathname)

Parameters:

  • pathname - The abstract pathname against which this abstract pathname will be compared.

Returns: This method returns 0 if the argument is equal to this abstract pathname, a negative value if the abstract pathname is lexicographically less than the argument, and a value larger than 0 if the abstract pathname is lexicographically greater than the argument.

Example:

Output:

👁 Image
Comment
Article Tags: