Have something similar to C# namespaces in Java?

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

Ex.: to print the Hello S would be something like:

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

Already with using System it would be:

Console.WriteLine("Olá S");

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

Author: Maniero, 2018-08-12

1 answers

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

System.out.println()

C # uses the namespace as a surname for the type, nothing more than that. It separates into packages called assemblies. For Java the package is your namespace, so it is different in various aspects.

You can still do this in C#:

using static System.Console;

WriteLine("Olá Mundo");

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

Note that in a simple "Hello World" in Java you are using class System and not the package or namespace System. It is completely different from C#. See documentation . It is part of the package java.lang which is automatically imported into every application (must have some configuration to avoid this). Actually the exact mechanism is part of another class .

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

import static java.lang.System.*;

out.println("olá");

I put on GitHub for future reference.

 4
Author: Maniero, 2020-07-29 16:50:34