Results 1 to 18 of 18
Thread: there is no return statement
- 08-03-2007, 02:49 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 41
- Rep Power
- 0
- 08-03-2007, 06:24 PM #2
Every method in java must have a return type in its method signature. The add method has a return type of int which means that the method must return an int. i can call the add method with two arguemnts and get the sum of them back, it will return the sum to me: int x = add(5, 6);Java Code:public static int add(int one, int two) { int c = a + b; return c; }
The type returned from the method must match the return type declared in the method signature.
- 12-02-2008, 11:29 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 85
- Rep Power
- 0
Re:
Hi,
In this method,the Datatype is int.If we use method with datatype means ,we must return any value except void datatype.
public static int add(int one, int two)
{
int c = a + b;
}
- 12-02-2008, 12:03 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-02-2008, 12:58 PM #5
Wrong example...
Deepa ... that is a wrong example:
The above example does NOT return anything. it is lacking the return statement, which is the same problem/example that the OP had. Hardwire's example is correct because it shows how the method returns the requested int.Java Code:public static int add(int one, int two) { int c = a + b; }
gabriel... here's a link of the Sun tutorials that expalins a little bit on the return statement:
Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-02-2008, 01:08 PM #6
Member
- Join Date
- Nov 2008
- Posts
- 85
- Rep Power
- 0
Re:
it's not an eg.This is the method asked by gabriel
- 12-02-2008, 03:46 PM #7
Member
- Join Date
- Dec 2008
- Posts
- 25
- Rep Power
- 0
i'm a little bit confuse with the syntax.
public static void main()
{
int one = 0;
int two = 2;
add(a, b);
}
in this case,a and b is undeclare, right?i think the coding should be
public static void main()
{
int a = 0;
int b = 2;
add(a, b);
}
- 12-02-2008, 04:47 PM #8
you are correct
angelicsign... hahaha ... you are correct... we all went looking for the missing return statement. The same also goes for the add () method:
Wrong:
Right:Java Code:public static int add(int one, int two) { int c = a + b; }
Java Code:public static int add(int [B][COLOR="Red"]a[/COLOR][/B], int [B][COLOR="red"]b[/COLOR][/B]) { int c = a + b; return c; }Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-02-2008, 05:06 PM #9
Member
- Join Date
- Dec 2008
- Posts
- 25
- Rep Power
- 0
if modify his code,i'll do like that.
not want to challenge or what,
just discuss only.
Java Code:public class FirstTest { public static void main([COLOR="red"]String [] args[/COLOR])//red highlight,this is a must for main { //declare int a,b and initialize value to them. int [COLOR="Red"]a[/COLOR] = 0; int [COLOR="Red"]b[/COLOR] = 2; System.out.println(add(a, b)); //for output to the screen,as new user of java,may output to the prompt. } public static int add(int one, int two) //return datatype is int { //because he declare one,two on the top,so we just follow his declaration int c = [COLOR="Red"]one[/COLOR] + [COLOR="Red"]two[/COLOR]; return c; //return datatype is int,so must return an int value } }
- 12-03-2008, 07:14 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So what you want to discuss about this.
In my view, all your comments are fine, but need to have few clarification.
Why you think it is must? What's the effect on un-highlighted code?//red highlight,this is a must for main
What is the difference between prompt and screen? I think you are talking about the command prompt. Actually println() print the output to the command prompt, or the console. Actually the word 'console' refer in IDEs. Most IDE print those output to on a separate window itself.//for output to the screen,as new user of java,may output to the prompt.
May be you are refer 'screen' for UI, is it? On UI you cannot add text using println(). You have to added different controls depend on your implementation and set text there.
- 12-03-2008, 07:44 AM #11
Member
- Join Date
- Dec 2008
- Posts
- 3
- Rep Power
- 0
If we want to call the main method externally without creating an instance of the Class, the
method signature must be as follows :
public static void main(String[] args)
Thats the reason why prevous code highlighted that portion.
_Sujith
- 12-03-2008, 08:33 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you clarify with a simple example? I'm not clear what you are trying to say. As far as I know main method call by the VM.
- 12-03-2008, 12:52 PM #13
Member
- Join Date
- Dec 2008
- Posts
- 3
- Rep Power
- 0
yea, correct.
VM calls the main method, having the Signature previously mentioned.
Can VM call main, with some other signature?
If yes, how.
- 12-03-2008, 01:08 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
No you cannot. Main method is the entry point to any Java application. Only argument you can pass is a String array.
- 12-03-2008, 01:19 PM #15
Member
- Join Date
- Dec 2008
- Posts
- 25
- Rep Power
- 0
First of all, i'm quite new in Java. so if anything wrong,just refine me k?
This is what i mean discussion.mod,you really correct my wrong mind set,
i thought for all main method in Javathis code is a must,because currently i'm using textpad to write my codeJava Code:public static void main([COLOR="Red"]String [] args[/COLOR])
where my lecturer ask us to.
when the highlighted code is missing,
it will prompt an error like this
Exception in thread "main" java.lang.NoSuchMethodError: main
but actually not a must for other editor/IDE??:confused:
Ya,and mod is right,new user should use prompt or console,and not screen for UI(user interface).THANKS mod. :p
but for new user of java, i suggest not to use IDE such netbeans or eclipse,
this is because normally IDE will auto generate the class for user,
it will cause user lack of basic practice.
I mean when the user start with a new blank page,he will not know how to start.:confused::(
We are inside the New To Java Thread,so i think all of us should make our basic strong enough 1st before we go on to advance java.;)Last edited by angelicsign; 12-03-2008 at 01:29 PM.
- 12-03-2008, 01:22 PM #16
Samething...
You both are talking about the same thing...
- If a class is going to have a "main" method (which makes it callable from the VM) it must have a String array as an argument, otherwise you get a nastygram like:
Exception in thread "main" java.lang.NoSuchMethodError: main- prompt, command prompt & screen all refer to the old reliable "DOS Command Prompt" that Windows offers us.
- I probably already knew it, but I would have to agree with Eranga that console refers to the IDE GUI. This way there is a way of differenciating between a Java program run from an IDE and one run from the DOS command prompt.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-03-2008, 01:22 PM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I agreed with you, because I've never advice to work on with any IDE at the start. Best choice is use Command Prompt + Notepad.
- 12-03-2008, 04:55 PM #18
Member
- Join Date
- Dec 2008
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
import statement.
By diRisig in forum New To JavaReplies: 2Last Post: 02-08-2008, 12:34 AM -
Using Prepared Statement
By Java Tip in forum Java TipReplies: 0Last Post: 02-06-2008, 09:22 AM -
Help with if statement
By carl in forum New To JavaReplies: 1Last Post: 08-06-2007, 07:53 AM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM -
If Statement
By aDrizzle in forum New To JavaReplies: 4Last Post: 07-08-2007, 08:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks