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: What does this short part of a code do?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default What does this short part of a code do?

      public boolean same_State (Search_State s2) {
        Jugs_State js2= (Jugs_State) s2;
     
        return ((j1==js2.get_j1())&& (j2==js2.get_j2()));
        }

    I just want to know what the part :
    Jugs_State js2= (Jugs_State) s2;
    does.

    I know you can create an object by e.g. Classname object = new Classname() but
    what is this "(Jugs_State)" in the brackets and why is there no "new" and why is
    there a new variable "s2" after that.

    Finally, what is the return doing at the end?

    Thanks a lot, I just need a general answer of what the code is doing
    not exactly what my classes do (that's why I haven't included all the code
    it's long).
    Last edited by Kranti1992; July 28th, 2012 at 04:46 AM.


  2. #2
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: What does this short part of a code do?

    It looks to be 'type casting' your passed in parameter 's2'
    f34r th3 kut3 1z

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: What does this short part of a code do?

    Quote Originally Posted by Kranti1992 View Post
      public boolean same_State (Search_State s2) {
        Jugs_State js2= (Jugs_State) s2;
        return ((j1==js2.get_j1())&& (j2==js2.get_j2()));
    I just want to know what the part :
    Jugs_State js2= (Jugs_State) s2;
    does.
    Jugs_State is apparently a subclass of Search_State. The data type of the function's parameter is Search_State, but the function apparently requires the Jugs_State methods get_j1() and get_j2() to calculate its return value.

    The object s2 already exists so the code in the function declares a reference variable of type Jugs_State named js2. It can't set js2 equal to s2 directly since they are different data types.

    That's why a cast is required. This type of casting is valid since the objects are compatible.

    There is no "new" in the declaration of js2 because it is not creating a new object, it is referring to the object that is the function's parameter.

    Reference: Java Tutorial: Inheritance


    Cheers!

    Z
    Last edited by Zaphod_b; July 28th, 2012 at 10:09 AM.

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: What does this short part of a code do?

    About the return,
    return ((j1==js2.get_j1())&& (j2==js2.get_j2()));
    The function is from type Boolean, so it will either return true or false.
    The && sign is the AND operator. and the == sign is to check if 2 variables are equal to each other.
    j1 and j2 are probably some variables declared somewhere in the code.

    You should be able to draw a conclusion from this. If you are still having trouble finding out what it will return, respond.

Similar Threads

  1. [SOLVED] Adding two byte/ short variables
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 26th, 2012, 04:31 AM
  2. short survey comparing netbeans and eclipse for my dissertation
    By jeremylaks in forum Java Theory & Questions
    Replies: 0
    Last Post: April 2nd, 2011, 11:09 AM
  3. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 03:10 PM
  4. way too hard to explain this in short. please help guys!!
    By humdinger in forum Collections and Generics
    Replies: 4
    Last Post: March 15th, 2010, 05:57 AM
  5. Short java project
    By derky in forum Paid Java Projects
    Replies: 3
    Last Post: October 28th, 2009, 09:48 PM