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 4 of 4

Thread: Help to understand the code!

  1. #1
    Junior Member
    Join Date
    Jan 2023
    Location
    Fortaleza, CE - BRAZIL
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Help to understand the code!

    Hello, everyone! I'm learning JAVA for applying to a job exam and I'd like to know what is really happening (output) in the code below that was used in a previous exam. Thank you so much for your attention and participation.

    [code = Java]
    public class Va1 {
    public static String getStr() {
    return “abcdefghijklmnop“;
    }
    public String ini(String s, int cpr) {
    return s.substring(0, cpr);
    }
    public String fin(String s, int cpr) {
    return ini(s, cpr)+s.substring(s.length()-cpr, s.length());
    }
    }
    public class Va2 extends Va1 {
    public static String getStr() {
    return “0123456789ABCDEF“;
    }
    public String ini(String s, int cpr) {
    return s.substring(s.length()-cpr, s.length());
    }
    public static void main(String[] args) {
    Va1 o=new Va2();

    System.out.println(o.fin(o.getStr(), 5));
    }
    }
    [/code]

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help to understand the code!

    what is really happening (output)
    Can you also post the output and add some questions to it where you do not understand.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2023
    Location
    Fortaleza, CE - BRAZIL
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help to understand the code!

    what is happening here? :

    ...
    public static void main(String[] args) {
    Va1 o=new Va2();
    ...

    I thought the getStr() would return the string "0123456789ABCDEF" instead of "abcdefghijklmnop", but I was wrong.
    Last edited by Norm; January 5th, 2023 at 06:45 PM. Reason: Fixed code tag

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help to understand the code!

    what is happening here?
    That code defines the static method main
    Inside the method a reference variable(Va1) is declared and assigned a value (a reference to an instance of the Va2 class).

    getStr is declared as a static method. static methods should be called using the name of the class, not with a reference variable: Va1.getStr() or Va2.getStr()
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Could someone help break this code down for me so i can understand it more?
    By soggla92 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 24th, 2020, 03:03 PM
  2. Can't understand where should I use longpress in my code.
    By fallen01 in forum Android Development
    Replies: 10
    Last Post: January 26th, 2019, 02:36 PM
  3. Replies: 4
    Last Post: October 1st, 2014, 12:01 PM
  4. Help me understand this code?
    By Farkuldi in forum Java Theory & Questions
    Replies: 11
    Last Post: November 12th, 2013, 03:37 PM

Tags for this Thread