开发者

java beginner "Hello World"

I am trying to learn Java.

I don't understand why this code won't work.

It won't output "Hello World" from test() function.

What a开发者_如何学Cm I doing wrong?

public class Main {

    public test(args) {
        System.out.println(args);
    }

    public static void main(String[] args) {
        test('Hello World');
    }
}


Firstly:

public test(args) {
    System.out.println(args);
}

You need a type to go with a parameter - Java is a strongly typed language and thus you always need to specify a type. As to what the type is here, System.out.println() can actually take anything, so you could set the type to String, Object or whatever you like (since Object has a toString() method and it has lots of overloads to deal with all the primitives.) Bear in mind this is unusual though, most of the methods you come across will just take something of a specific type!

Since you're only calling test from the main method here, and you're passing a string to it, you may as well set the type of args to String.

The second problem with this is that there's no return type specified. You always need to specify a return type, in this case nothing is returned so the type is void. If you don't do this then the compiler has no way of knowing whether what you wrote was meant to be a method or a constructor.

The third problem is that test is an instance method but you're calling it statically. test() needs to be static as well, otherwise it belongs to instances of Main and not the Main class. Why does this matter? Well, there could potentially be thousands of instances of Main, so what instance should the method run on? The compiler has no way of knowing.

Next:

public static void main(String[] args) {
    test('Hello World');
}

You're passing a string here, which needs to be in double quotes. Java treats quotes differently to PHP, single quotes are used for single character literals and double quotes are used for strings. So you can never enclose a string in single quotes like this, it has to be double.

Putting it all together:

public class Main {

    public static void test(String args) { //Add return type and parameter type, make test static
        System.out.println(args);
    }

    public static void main(String[] args) {
        test("Hello World"); //Change single quotes to double quotes
    }
}


In Java, Strings are always using double-quotes, never single-quotes. That's reserved for char types.

test("Hello World");

Also, your test function doesn't have a return type (not even void), and it's a method while main is static, so you'd need to instantiate Main or make test static as well. You also need to specify the type for each argument.

Try this:

public static void test(String args) {
    System.out.println(args);
}


The test() method has syntax errors

test() method lacks a return type, and the type of the argument. Change it to e.g.

public void test(String args) {
    System.out.println(args);
}

Invalid syntax for 'Hello World'

Strings are need to be enclosed in double quotes, so change 'Hello World' to "Hello World"

You can't call an instance method from a class method.

Your test() method is an instance member of the Main class, while main() is a class method. You'll need an instance of the Main class to call test() on. e.g.

public static void main(String[] args) {
        new Main().test("Hello World");
}

Alternativly, change test() to a class method

You could also make test() a class method, that way you can call it from main()

public static void test(String args) {
    System.out.println(args);
}


create an object for the class and access in the main method or make the method declaration as static


compare to:

public class Main {

    public static void test(String args) {
        System.out.println(args);
    }

    public static void main(String[] args) {
        test("Hello World");
    }
}


public class HelloWorld {
public void test(String str) {
    System.out.println(str);
}
public static void main(String[] args) {
    HelloWorld helloWorld = new HelloWorld();
    helloWorld.test("Hello World");
}
}
  • Methods in Java have a return type or void before their name. eg: void in above code.
  • Method arguments should have the data type before their name. eg: String in above code.
  • Either you have to create an instance of your class and call the method using instance.methodName or declare the method as static and call it using Classname.methodName.
  • As a good practice, your class name starts with Uppercase and method name starts with lower case letter.


test should be a static method. from a static method u can invoke only static methods or create an instance of Main and invoke as like

Main m = new Main() m.test("hello world");

further hello world should be double quotes.


Simple step to start with java

  1. Create a temporary folder as testjava in c:
  2. Using Notepad or any other editor create a small file in testjava with name Hello.java with the text as

    class Hello {
        public static void main(String[] args) {
            System.out.println("Hello, world");
        }
    }
    
  3. Go to the path from cmd c:\Program Files (x86)\Java\jdk1.6.0_14\bin>
  4. Compile the code you have written javac c:\testjava\Hello.java a class file with name Hello.class should be created in testjava
  5. Go to the path c:\testjava>
  6. Run the command as java Hello
  7. It should print as Hello, world


Instead of creating and using a test method, you can put it all in the main method so it would look like this:

public class HelloWorld {
    public static void main(String[] args){
        System.out.println("Hello World!");
    }
}

Note that the double quotes are used for strings, as single quotes are normally used for chars :)


public class Main {

 // You always have to have a return type for a function, at least void
 //Also, the datatype of every variable inside the parantheses () of a function must be defined

    public void test(String args) {
        System.out.println(args);
    }

    public static void main(String[] args) {
        test("Hello World"); //Strings are always passed between " and ", not ' and '
    }
}

Now your code should work.


You can write that code into one method.

For example:

class Test {
     public static void main(String[] args){
        System.out.println("Hello, world!");
     }
}

This is how I learned it in my class! Hope this helps!


You have to specify a return type for your test method. You also must use double quotes (") for Strings. Single quotes (') can be used for chars.

public void test(String args){
  System.out.println(args);
}

public static void main(String[] args){
  test("Hello World");
}


I fixed your code:

public class Main {

    private void test(String args) {
        System.out.println(args);
    }

    public static void main(String[] args) {
        Main TestObject = new Main();
        obj.test("Hello World");
    }
}

Ok, so let's see why your code didn't work:

  1. Your test() method had args as a parameter. Great, but you didn't say what is the type of this args variable. Int? String? Char? You need to define that.

  2. For strings, you should use the double quotes:

    String name = "Adam";
    

    For a char, you would use the single quotes:

    char key = 'A';
    
  3. Java is an object oriented language, meaning that there are objects with states(variables), and things they can do(methods). When calling the test() method, you didn't call it off an object.

  4. What does the test() method return when you call it? Even if it returns nothing, you have to write a return type, void.

One last tip, when ever you fall in trouble, just use a debugger, and go through every step in your program to see what the problem is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜