-1

I'm new to Java and had some very basic questions:

Why must the main method always take in a String[]? --> public static void main(String[] args)

With regards to primitives, what is the difference between a float and a double?

4
  • 5
    Try to copy-paste both of your questions into Google and read through the results. Commented Mar 12, 2013 at 3:30
  • Partial Duplicate: stackoverflow.com/a/10083070/804773 Commented Mar 12, 2013 at 3:31
  • 2
    Fairly unrelated questions like these two should be posted separately. Commented Mar 12, 2013 at 3:34
  • Hi Chris, welcome to Stack Overflow. Please take a moment to look through the FAQ, in order to get a better feel for the sort of questions that are appropriate here. Commented Mar 12, 2013 at 3:35

4 Answers 4

3

The main method takes String[] as a parameter because it holds the program's command-line arguments.

$ javac Args.java
$ java Args hello goodbye
hello
goodbye

public class Args {
  public static void main(String[] args) {
    for (String s : args) {
      System.out.println(s);
    }
  }
}

For difference between float and double, http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Sign up to request clarification or add additional context in comments.

8 Comments

Actually it's not correct
@ZagorulkinDmitry isn't it? Please elaborate.
What, is args[0] the program name? If so you're being a bit pedantic. And why the double down-vote?
@ktm5124 +1 to balance.
Input: what is the main method. Args[0]=what ... Args[4]=method
|
1

The array of strings represents any command line arguments which are passed into your program from the system.

float and double are similar except that double uses more memory and has more precision (it may also be slower than float for add, subtract, multiply, divide etc).

Comments

1

Why must the main method always take in a String[]? --> public static void main(String[] args)

When you run a Java program from the command line (terminal), the syntax is

java SomeClass [list of arguments, space-separated]

This means that you can invoke your program with different options. The args variable contains the command-line arguments. If you don't care about them (which often you won't), just don't use the variable.

Here are some things you could do with the argument(s):

  1. treat them as a path to a file and load it
  2. treat them as options (e.g., verbose, silent, log errors, etc.)
  3. treat them as input to the program in some other way (e.g., the user's name and birthday)

If you wanted to use the arguments, you could do so as follows.

public static void main(String[] args) {
    String first, last;
    if (args.length >= 2) {
        // The user provided a first and last name.
        first = args[0];
        last = args[1];
    } else {
        // [ prompt user for name ]
    }
}

With regards to primitives, what is the difference between a float and a double?

A double has twice (double) the precision of a float. Consequently, it also takes up twice as much memory.

Comments

0

String[] because the parameters of your program will always be read as strings.

Float is a 32 bit floating point number. Double is double precision: 64 bit. (This has nothing to do with your system architecture. Float will always be 32 bit and Double will always be 64 bit in Java).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.