Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: (.readLine() method) of BufferedReader class

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default (.readLine() method) of BufferedReader class

    Overrided

    whats the difference of this

        String readLine(boolean ignoreLF) throws IOException {
    	StringBuffer s = null;
    	int startChar;


    to this
        public String readLine() throws IOException {
            return readLine(false);
        }

    question:

    1.) what is the first code? a method? why is it doesnt have any modifiers?
    2.) when everytime im calling a .readLine() method. which of the two am i reffering to?


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: (.readLine() method) of BufferedReader class

    You are referring to the second one with no arguments in it.

    The first code block you posted is the actual readLine method which takes a boolean if it should ignore linefeed or not.

    The second code block is a convenience method which just calls the other one with a default value of false passed in.

    // Json

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: (.readLine() method) of BufferedReader class

    but i try to make a method like the first one...


    public class NewClass {
     
        String word() {
     
            return "My Method";
        }
     
        public String word() { //method word is already defined 
     
            return word();
        }
    }

    bit confuse due to some curiosity

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: (.readLine() method) of BufferedReader class

    They are both the same, if you give one of the parameters then they are different. Look up function overloading.

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: (.readLine() method) of BufferedReader class

    public class NewClass {
     
        String word(boolean x) { // i add some parameters here.. 
     
            return "My Method";
        }
     
        public String word() { //method word is already defined
     
            return word();
        }
    }


    ahh ,.

    and i can access both of them... .

    why do we have to define such methods that has different purposes with the same name? or the same identifier?

    and like what have sir Json told me... when everytime im calling the .readLine() method im only calling the second one... (the one with modifier)

    so whats the purpose of the first one?? (the one without modifier).

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: (.readLine() method) of BufferedReader class

    Convenience, thats all

    // Json

  7. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: (.readLine() method) of BufferedReader class

    i change the method name of the public method word() into doIt()

     String word(boolean x) {
     
            return "My Method";
        }
     
        //if i change the method name ....
        public String doIt () { 
     
            return word(); //the method word is asking for an argument
        }
    }

    but if they are the same.. everythings fine....

    hmm..?

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: (.readLine() method) of BufferedReader class

    in your last set of methods, word requires a boolean parameter x.

    Also, when you had this:
    public class NewClass {
     
        String word(boolean x) { // i add some parameters here.. 
     
            return "My Method";
        }
     
        public String word() { //method word is already defined
     
            return word();
        }
    }
    Here, word() will never return because it's always calling itself, not the other word. It should be this:

    public class NewClass {
     
        String word(boolean x) { // i add some parameters here.. 
     
            return "My Method";
        }
     
        public String word() { //method word is already defined
     
            return word(true);   // or false, it doesn't matter in this case
        }
    }

Similar Threads

  1. Help with CoinCounter method
    By abielm_007 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 3rd, 2010, 12:19 PM
  2. Any way to map method calls?
    By Swiftslide in forum Collections and Generics
    Replies: 1
    Last Post: September 21st, 2009, 04:37 AM
  3. [SOLVED] Method declaration in Java
    By mohsendeveloper in forum Object Oriented Programming
    Replies: 4
    Last Post: June 11th, 2009, 03:18 AM
  4. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 06:55 PM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM