Results 1 to 6 of 6
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Return vs Println

    What are the differences in return and System.out.println()? Do they do the same things? And I can never get the return keyword to actually work.

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

    Default Re: Return vs Println

    The return statement returns (sic) from the currently running method; the System.out.println( ... ) method prints out the String representation of its argument (if any), followed by a new line. Those two have almost nothing in common and if you're having trouble with the return statement, feel free to post the code you're having trouble with.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Return vs Println

    Java Code:
    class Bicycle {
    	private int gear;
    	private int speed;
    	private int id;
    	private static int numberOfBicycles = 0;
    	
    	public void Bicycle1(int startingSpeed, int startingGear) {
    			gear = startingGear;
    			speed = startingSpeed;
    			id = ++numberOfBicycles; 
    			System.out.println("Starting...");
    	}
    	public int getID() {
    			return id;
    	}
    	}
    class Test {
    	public static void main(String[] args) {
    	Bicycle objBicycle = new Bicycle();
    	objBicycle.Bicycle1(10,2);
    	System.out.println(objBicycle.getID());
    	}
    }
    Error:
    Java Code:
    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    
    C:\Users\Lenovo>cd Desktop\Java
    
    C:\Users\Lenovo\Desktop\Java>javac noobs.java
    
    C:\Users\Lenovo\Desktop\Java>java -cp . noobs
    MattIX4noob
    C:\Users\Lenovo\Desktop\Java>javac Test.java
    Test.java:11: error: ';' expected
            System.out.println("Starting...")
                                             ^
    Test.java:18: error: '(' or '[' expected
            Bicycle objBicycle = new Bicycle;
                                            ^
    Test.java:22: error: reached end of file while parsing
    }
     ^
    3 errors
    
    C:\Users\Lenovo\Desktop\Java>javac Test.java
    Test.java:22: error: reached end of file while parsing
    }
     ^
    1 error
    
    C:\Users\Lenovo\Desktop\Java>javac Test.java
    Test.java:21: error: cannot find symbol
            System.out.println(objBicycle.getid());
                                         ^
      symbol:   method getid()
      location: variable objBicycle of type Bicycle
    1 error
    
    C:\Users\Lenovo\Desktop\Java>java Test.java
    Error: Could not find or load main class Test.java
    
    C:\Users\Lenovo\Desktop\Java>javac Test.java
    Test.java:21: error: cannot find symbol
            System.out.println(objBicycle.getid());
                                         ^
      symbol:   method getid()
      location: variable objBicycle of type Bicycle
    1 error
    
    C:\Users\Lenovo\Desktop\Java>

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

    Default Re: Return vs Println

    The code you showed us is not the code you tried to compile. Please fix that before we can try to help you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Return vs Println

    Hmm? I thought it was, the class is named Test, and that's what I compiled.

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Return vs Println

    1. rename Bicycle1 to Bicycle and delete void, so this will be a constructor.
    2. inside Test replace new Bicycle() with Bicycle(10,2)
    3. delete line 20 in class Test
    4. compile the two class with javac Bicycle.java and javac Test.java
    5. Start your app with java Test

Similar Threads

  1. System.out.println versus return: Perspective?
    By bigsonny in forum New To Java
    Replies: 6
    Last Post: 06-19-2011, 08:42 PM
  2. return list - rather than println()
    By gcclinux in forum New To Java
    Replies: 9
    Last Post: 04-28-2011, 03:41 PM
  3. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 08:52 AM
  4. Need help with println
    By jhetfield18 in forum New To Java
    Replies: 8
    Last Post: 09-18-2009, 08:26 AM
  5. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-18-2008, 11:06 PM

Posting Permissions

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