Results 1 to 12 of 12
  1. #1
    kdiswired is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default What is the error in the program???

    I cannot understand the error in the below program. Need some help here.
    Java Code:
    class callvalue
    {
    	public static void main(String a[])
    	{
    		int x=50;
    		int y=70;
    		callvalue cv=new callvalue();
    		cv.interchange(x,y); 
    		System.out.println(x,y);
    	}
    
    	void interchange(int x1, int y1)
    	{
    		int z1;
    		z1=x1;
    		x1=y1;
    		y1=z1;
    		System.out.println(x1,y1);	
    	}
    }
    Last edited by pbrockway2; 04-07-2012 at 09:15 AM. Reason: code tags added

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: What is the error in the program???

    You haven't read the Java tutorials covering the basics, have you?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: What is the error in the program???

    Hi kdiswired, welcome to the forums!

    When you post code it's a good idea to use the "code" tags. Put [code] at the start of the code and [/code] at the end - that way the code will be readable when it appears here.

    I cannot understand the error in the below program.
    What error?

    Specifically, does the code compile? If not and you can't understand the compiler's messages, post them and someone is sure to be able to explain what they mean.

    Or does the code not do at runtime what you expect or intend? In that case say what you expected and what happened. If there is a runtime exception copy and post the error message exactly as you see it.

  4. #4
    kdiswired is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: What is the error in the program???

    ooops!!!! sorry.....didn't know how to use code tags.

    However, the code does not compile and the error I am getting is:

    callvalue.java:10:cannot find symbol
    symbol:method println(int,int)
    location:class java.io.PrintStream
    System.out.println(x,y);
    ^

    callvalue.java:19:cannot find symbol
    symbol:method println(int,int)
    location:class java.io.Printstream
    System.out.println(x1,y1);
    ^

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: What is the error in the program???

    That's right. you're passing two int parameters to a method that doesn't have an overload that accepts two int parameters.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: What is the error in the program???

    ooops!!!! sorry.....didn't know how to use code tags.
    That's OK! I'm applying a sort of sliding scale to these things: I'm happy to add them to a first post, I'll start directing people to the site faqs after that (becoming increasingly less polite), and then, after some number of posts, I'll just not respond to what I find hard to read.

    -----

    you're passing two int parameters to a method that doesn't have an overload that accepts two int parameters
    I agree.

    passing, parameters, overload, accept. There's so much (absolutely unavoidable) jargon here. If it's not clear your first port of call is your notes and textbooks etc. After that, say what you've looked at and what examples of println() you've found and ask db what he's getting at.

  7. #7
    kdiswired is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: What is the error in the program???

    Actually....I am trying to check the call by value in java with the help of this program.....can u pls tell me whr am wrong????
    How do I make the program error free???

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: What is the error in the program???

    can u pls tell me whr am wrong?
    I take it that's a frustrated attempt at "Can you please tell me where I am wrong?". Please take the time to spell words out: there are many here coping with the many oddities of English and they don't need cryptic abbreviations as an additional burden. Further, a professional manner will encourage people to respond. (Their logic is something along the lines of "you should be as serious as my time is valuable".)

    You have been told what's wrong. "System.out.println(x,y)" makes no sense. The compiler is saying that there is simply no such method: no println() method that takes two int arguments like that.

    If you want to print two ints, do it in two statements. Or think of some way of combining them into a single string.

  9. #9
    kdiswired is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: What is the error in the program???

    Of course, I completely get it now. I was able to compile the code successfully and run it. Thank you very much. I have mentioned the correct code below.


    Java Code:
    import java.lang.*;
    class callvalue
    {
    	public static void main(String a[])
    	{
    		int x=50;
    		int y=70;
    		callvalue cv=new callvalue();
    		cv.interchange(x,y); 
    		System.out.println("Calling value of x: "+x);
    		System.out.println("Calling value of y: "+y);
    	}
    
    	void interchange(int x1, int y1)
    	{
    		int z1;
    		z1=x1;
    		x1=y1;
    		y1=z1;
    		System.out.println("Called value of x: "+x1);
    		System.out.println("Called value of y: "+y1);	
    	}
    }
    Last edited by kdiswired; 04-07-2012 at 10:48 AM. Reason: wrapping code tags

  10. #10
    kdiswired is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: What is the error in the program???

    The above code gives an example of Call by Value in java. However, I do not understand the concept of Call by Reference. Need some help here.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: What is the error in the program???

    Quote Originally Posted by kdiswired View Post
    The above code gives an example of Call by Value in java. However, I do not understand the concept of Call by Reference. Need some help here.
    Java doesn't implement the call by reference mechanism so there's no need to try to use it; it can't be done.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: What is the error in the program???

    I was able to compile the code successfully and run it. Thank you very much.
    You're welcome - and well done!

    The above code gives an example of Call by Value in java. However, I do not understand the concept of Call by Reference.
    The example you constructed is a very good example of call by value. What we see is:

    Java Code:
    Called value of x: 70
    Called value of y: 50
    Calling value of x: 50
    Calling value of y: 70
    In particular the values of the argument variables get swapped within the method, but the values of the variables used as arguments do not change as a result of the method call. For me this is "call by value" semantics. No variable declared in the caller's context (x, y or anything else declared there) is going to change its value as a result of the method call interchange(x,y).

    But what about so-called "reference values"? After all they have "reference" in their name! It's easy to check using your code:

    Java Code:
    import java.awt.Point;
    class callvalue
    {
        public static void main(String a[])
        {
            Point x = new Point(0, 50);
            Point y = new Point(0, 70);
            callvalue cv=new callvalue();
            cv.interchange(x,y); 
            System.out.println("Calling value of x: "+x);
            System.out.println("Calling value of y: "+y);
        }
     
        void interchange(Point x1, Point y1)
        {
            Point z1;
            z1=x1;
            x1=y1;
            y1=z1;
            System.out.println("Called value of x: "+x1);
            System.out.println("Called value of y: "+y1);   
        }
    }
    We see the same thing:

    Java Code:
    Called value of x: java.awt.Point[x=0,y=70]
    Called value of y: java.awt.Point[x=0,y=50]
    Calling value of x: java.awt.Point[x=0,y=50]
    Calling value of y: java.awt.Point[x=0,y=70]
    Values of variables within the method are swapped, but no change to the variables declared outside the method occurs.

    A consequence of this pass by value semantics is that although methods can change all sorts of things, the only way a variable can change its value is through something being assigned to it somewhere within its scope: either straight forwardly or via the increment/decrement operators.

    -----

    The following from JavaRanch are worth reading: Cup Size -- a story about variables and Pass-by-Value Please (Cup Size continued).
    Last edited by pbrockway2; 04-08-2012 at 12:22 AM.

Similar Threads

  1. i got an error while run jdbc program..
    By kvk in forum Advanced Java
    Replies: 4
    Last Post: 02-28-2012, 01:41 PM
  2. What's the error in this program...
    By bsudhir6 in forum New To Java
    Replies: 6
    Last Post: 09-12-2010, 06:23 AM
  3. Program Error Help!!!
    By arrech326 in forum New To Java
    Replies: 6
    Last Post: 11-18-2009, 12:16 PM
  4. I can't fix this runtime error in my program
    By Sinnergy in forum New To Java
    Replies: 5
    Last Post: 02-25-2009, 04:29 PM
  5. CRC check program 1 error
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 05:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •