I recently started learning Java and I had a few questions.

  1. It is not clear to me why in Java there is no simple mechanism (method) for input / output of anything. That is, when in Pascal it is write\read or Python - print\input . In Java, there is a standard output tool, System.out.println , but input needs to be implemented via Scanner import.
    Tell me something I don’t understand or is there really no such basic methods in such a powerful language?
    Also, as I understand it, Scanner is only suitable for text, but what if I need to enter numbers? What to use? You can completely write how it will look.

  2. Please explain what the arguments in this command mean.

     public static void main(String[] args) 
  3. You can explain the principle of creating an instance of a class. For example:

     String variable = new String ("Text"); 

    Why is why the String class is called twice at the beginning and at the time of creation?
    I understand that you can just accept everything, but I don’t understand why this is an action, is there an explanation for this, or is it done just like that?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

4 answers 4

  1. Java is an object-oriented language, unlike Pascal, and its rich stream-based I / O class system allows you to use the same mechanisms for any situation: work with the console, work with files, work with the network, work with archives.

    Once we are talking about the class Scanner - refer to the documentation .

    The class provides a bunch of methods for different situations:

    • String next(String pattern) - returns a string if it matches the pattern;

    • boolean nextBoolean() - returns the value of a boolean type;

    • int nextInt() - returns an integer value;

    • float nextFloat() - returns a floating point value of type float .

    This is not all, the Scanner can, but now you know where to look.


  1. This is not a team. This declaration is a public ( public - accessible to all) static ( static - accessible without creating an instance of the class) method main , which takes an array of strings ( String[] args ) and returns nothing ( void ).

    According to the specification, a method with such a signature is the entry point to your program. That is, when you start the java MyClass command line, the program will start with this method in the MyClass class. As an array of args will be passed arguments from the command line. For example, when you start java MyClass foo bar args array will have the value ["foo", "bar"] .


  1. why the String Class is called twice at the beginning and at the creation

     String variable = new String ("Text"); 

    The class is not "called"; the class cannot be "called". In this case, there are three actions:

    • A variable type String declared.

    • A new instance of the String type is created through the constructor with one argument "Text" .

    • Variable variable assigned a link to the created object.

    In the case of strings, this could be reduced to the form:

     String variable = "Text"; 

Ps. I highly recommend starting to learn Java, armed with one of the primers, there are answers to such questions: Books on Java and other literature


Pps. Next time, please follow the rules and ask one question at a time.

  • Thank you all for your responsiveness. Now I understand why String needs 2 times. I read about the PLO, the general principles are clear to me. But to be honest, the books on Java are very confusingly explained, but I did not find the book normal for newbies. Sorry to have broken the rules with a lot of questions. - Alex King
  • @AlexKing, try Katie Sierra, Bert Bates - Learning Java (Head First Java) . There, everything is extremely detailed and just chewed. - Nofate
  1. In java, all I / Os are based on streams and their implementations for the console, file, etc. Reading / writing to the console is a special case of working with threads. Scanner is a class that has the implementation of reading something from a stream. But from the console you can read not only with it.
  2. Arguments String [] args are those parameters that will be passed to the method when it is called from the command line. Those. if there is a java MyClass param1 param2 call, then args is a string array containing param1 and param2 strings. If the call occurs without parameters, this array will be empty.

  3. The first String is an indication of the type of the variable being declared. In java static typing takes place, unlike Python. And unlike Pascal, where all variables are first declared, and then they are used, in java it is possible to declare variables in any place, the main thing is that before they are used directly. The second String is a call to the constructor of the String class with a parameter passed to it. As a result, we get an object of class String, which is assigned to variable.

    There are no 1 simple write / read methods in Java. Entering numbers can be done like this:

     Scanner scanner=new Scanner(System.in); String st=scanner.nextLine(); int i=Integer.parseInt(st); 

    2 are the command line arguments with which your application runs.

    3 The first word String sets the variable type; new String ("Text") creates a new String object. In general, you can immediately and not initialize the variable:

     String variable; /...какой-то код.../ variable=new String("text"); 
      1. The problem with the conclusion is not clear. To output something to the console just write

         System.out.println(ТУТ_ЧТО_ТО); 

        As an argument (TUT_CHTO_TO) you can substitute either a primitive or an object. In the latter case, it will be automatically converted from the string and output to the console.

      2. An array of strings is passed to the main method, which are the parameters for running the program. For example, by running from the command line, you can pass something and get it as an argument to the main method.

      3. The class is not called twice. First, its type is declared, the name of the reference variable is specified, then an object is created and assigned to this reference variable. Made so and not otherwise. is a strongly typed language.
        For example, in type of the variable is not indicated and you can assign anything to it. And no compiler will pull you down if you try to add the number 1.(3) with the string "Вася" .

      • The author needs to read about OOP in a book on Java, at least just a couple of pages - Gikas