• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

return from void method..

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more question which returns from void method..


public class Test045
{
int Method1(int i) { return 1; } //1
//void Method2(int i) { return i + 1; } //2
int Method3(int i) { return i + 1; } //3
void Method4(int i) { return; } //4
public static void main(String args[]){
Test045 demo = new Test045();
System.out.println(demo.Method1(1));
//System.out.println(demo.Method2(2));
System.out.println(demo.Method3(3));
// WHy does it give error,Can not convert int to char[].........?
System.out.println(demo.Method4(2));
}
}


Thanx
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swati, I don't get the same error.The only error I get is regarding the last call that is bcoz of void type return declared.
System.out.println(demo.Method4(2));
Thanks.
[This message has been edited by Bindesh Vijayan (edited September 08, 2001).]
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bindesh,
I dont seem to get any errors on both the cases
1. int method(int i) {
return i+1;
}
+ has both operands as integers
2. void method4(int a) {
return;
}
void can have return
just that it cant return any value thats all
Let me know
Thankx
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Let me make it very simple and clear.


public class Test045
{
void Method4(int i) { return; } //4
public static void main(String args[]){
Test045 demo = new Test045();
System.out.println(demo.Method4(2));
// When I call a method with integer literal,it gives error as ,Can not convert void to char[].........?
}
}


Hope it will be clear now,and u cld help me.
Thanx
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by swati bannore:
Hi,
Let me make it very simple and clear.
Hope it will be clear now,and u cld help me.
Thanx


swati,
When i try to compile this code , yes i do get error
But the error message is different
C:\SCJP\Vest.java:8: 'void' type not allowed here
System.out.println(demo.Method4(2));
^
1 error
Tool completed with exit code 1
Its becoz, there is no signature in System class that can
accept void as a parameter
From JDK doc:
public static final PrintStream out
The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.
For simple stand-alone Java applications, a typical way to write a line of output data is:
System.out.println(data)
But I am sorry swati, I dont know why and how you getting that error? Let me know if you discover it
Thankx
Ragu


 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too get the same error Swati,as Raghu reports.
Thanks
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am using JDK1.2.2 , cld the error differ cause of that..?
Anyways..Main doubt is when you have return statement in void method.WHat's the ponit in having return ststamenet ,if compiler is to give error when you call them.
Thanx anyways
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swati I think you mistook the error.What I think, the problem is not bcoz you have return a statement, but the problem is that you are trying to print the value returned from the method in println statement.
System.out.println takes a valid arg but since your method is not returning any thing,bcoz simply saying return in any function means to return from that position.This means to say that if you havae a return statement in between the function then after reading that return statement it will return from the function without executing the other statements below the line.
This is the use of return .Here is a small example

public void ret()
{
if(true)
{
System.out.println("Swati1");
return;
}
else
{
System.out.println("Swati2");//this will not be printed
}
}

Thanks.
[This message has been edited by Bindesh Vijayan (edited September 09, 2001).]
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a Bunch Bindesh.....
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry Swati I was wrong there.You know it happens sometime .Actually I was wrong here

This means to say that if you havae a return statement in between the function then after reading that return statement it will return from the function without executing the other statements below the line.


This statement is true regarding C but not in Java.Iam sorry again.I was too quick.
But Iam cent percent correct with my logic behind the error.
As too you real question,
WHat's the ponit in having return statemnt

I'll try to find an answer for it or may be some one will answer it, before I do.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, swati, I think the problem is that Java does not provide the following method:
<code>
System.out.println(void);
</code>
The following code gives compile error:

error is:

Originally posted by swati bannore:


Regards,

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Test049
The answer should be 12 instead of 18. Take note of the seek(4)
before r.writeLong(5).
BTW, Sun, I gained a lot from your contribution. Keep the excellent work ! Appreciated.
Regards,
Jin
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made the same error jin, but read carefully the question. It says how many bytes are written and not how many byte are actually contained in the file !! So the answer is correct (18)
Val
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Valentin for your nice feedback.
Good luck to your SCJP tour!

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹²³ at my homepage.
 
reply
    Bookmark Topic Watch Topic
  • New Topic