Jeanne Boyarsky wrote:Joson,
It's still a good book and all the examples will work with Java 8 SE.
After you finish the book, you'll want to read an upgraded book on the Java 7/8 features.
Tim Cooke wrote:Is it just one class file? If so why not just copy it in the same way you would any other file?
If it's for a school class then I'm sure your teacher is going to want to see the source code too, which is the .java file.
Paweł Baczyński wrote:
Joson Mathew wrote:However, I need my 'method' to remain void as I need to use System.out.println inside the method, although that is not shown in the program here. I can't use that in your method can I? What are my options?
Have you tried calling System.out.println from a method that returns int? What happened?
Often the best way to find out if you can or can't do something is to try it.
Further, if you are going to make such bold statement, you might want to provide some supporting evidence - i.e. "My textbook says..." or "According to the Java Language Specification..."
When you do try it, and "it doesn't work", post your code showing what you tried, and tell us exactly what happened.
Jesper de Jong wrote:Welcome to the Ranch.
Arguments to methods are passed by value in Java. That means that if you call a method, for example doSomething(n); in line 5 of your second code snippet, then only the value of the variable 'n' is passed to the method 'doSomething' - not the variable 'n' itself. Inside the 'doSomething' method, the value is assigned to a new variable (called 'i' in your code). If you change the value of the variable 'i', then the value of 'n' in the main method is not affected, because 'n' and 'i' are different variables.
You can't directly do what you want to do in Java. Instead of trying to modify the value of a variable that you pass in to the method, make the method return the desired value, and assign it in the 'main' method.