Results 1 to 12 of 12
- 04-07-2012, 09:06 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
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
- 04-07-2012, 09:12 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Re: What is the error in the program???
You haven't read the Java tutorials covering the basics, have you?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-07-2012, 09:19 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
What error?I cannot understand the error in the below program.
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.
- 04-07-2012, 09:34 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
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);
^
- 04-07-2012, 09:52 AM #5
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-07-2012, 10:18 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: What is the error in the program???
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.ooops!!!! sorry.....didn't know how to use code tags.
-----
I agree.you're passing two int parameters to a method that doesn't have an overload that accepts two int parameters
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.
- 04-07-2012, 10:24 AM #7
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
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???
- 04-07-2012, 10:34 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: What is the error in the program???
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".)can u pls tell me whr am wrong?
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.
- 04-07-2012, 10:45 AM #9
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
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
- 04-07-2012, 10:56 AM #10
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
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.
- 04-07-2012, 12:03 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 04-08-2012, 12:19 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: What is the error in the program???
You're welcome - and well done!I was able to compile the code successfully and run it. Thank you very much.
The example you constructed is a very good example of call by value. What we see is:The above code gives an example of Call by Value in java. However, I do not understand the concept of Call by Reference.
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).Java Code:Called value of x: 70 Called value of y: 50 Calling value of x: 50 Calling value of y: 70
But what about so-called "reference values"? After all they have "reference" in their name! It's easy to check using your code:
We see the same thing: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); } }
Values of variables within the method are swapped, but no change to the variables declared outside the method occurs.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]
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
-
i got an error while run jdbc program..
By kvk in forum Advanced JavaReplies: 4Last Post: 02-28-2012, 01:41 PM -
What's the error in this program...
By bsudhir6 in forum New To JavaReplies: 6Last Post: 09-12-2010, 06:23 AM -
Program Error Help!!!
By arrech326 in forum New To JavaReplies: 6Last Post: 11-18-2009, 12:16 PM -
I can't fix this runtime error in my program
By Sinnergy in forum New To JavaReplies: 5Last Post: 02-25-2009, 04:29 PM -
CRC check program 1 error
By javakid9000 in forum New To JavaReplies: 1Last Post: 03-19-2008, 05:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks