Is there something like the C # namespaces in Java?

5

I noticed the size of the command to put a message on the screen in Java, I wonder if it has any way to decrease? As in C # the programmer puts the word using "namespace"; and you can use the "shortened" command.

Eg to print the Hello S would look something like:

System.Console.WriteLine("Olá S"); //sem adicionar o using System

Already with using System would be:

Console.WriteLine("Olá S");

This in C #, as it is in Java? Would you like to do something similar for the programmer to put only println("Olá S") ?

    
asked by anonymous 13.08.2018 / 01:04

1 answer

4

Depends on what you call similar. Java has packages , which can be mistaken for something similar (in fact it also contains namespace ), after all they put a front of the types. In Java you have:

System.out.println()

The last is the method, out is the type and System is the package.

C # uses the namespace as a surname for the type , but nothing else of this. It separates into packages called assemblies . For Java the package is your namespace , so it is different in many ways.

You can still do this in C #:

using static System.Console;
WriteLine("Olá Mundo");

In Java it has a significant semantic difference because in C # we only use using as a shortener so we do not need to use the full name of the type, in Java what is done is a import of the package so that it becomes available for use, the name has nothing to do with it. Java makes some% automatic% s so you can use the most important types.

Note that what you are using when doing a simple Hello World in Java is using the class import and not the package or namespace System . It's completely different from C #. See the documentation . It is part of the package System that is automatically importing in every application (must have some configuration to avoid this). In fact, the exact mechanism is part of another class . / p>

If you do not want to use the full name you have to do the same as the example I quoted for C # (but it's still less convenient):

import static java.lang.System.*;
out.println("olá");
    
13.08.2018 / 01:13