When a String is considered a primitive type in web development?

3

I was in doubt in a class when the teacher said that a String type was considered primitive! But I've learned in Object Oriented Programming that they're just primitive:

Byte: 1 byte.
Short: 2 bytes.
Int: 4 bytes.
Long: 8 bytes.
Float: 4 bytes.
Double: 8 bytes.
    
asked by anonymous 29.08.2015 / 23:21

4 answers

3

It is the first time I read this, maybe your teacher can explain with certainty what he meant. But by kicking, it may be bound to the parameters of a request to come as String in Java, regardless of the type that is specified, for example, in an HTML form.

For example, consider a form that accepts numbers between a range:

<form action='meuservlet' method='post'>
    <input type='number' min='0' max='100' name='meu-parametro-numerico'/>
    <button type='submit'>Enviar</button>
</form>

If you want to get the value of meu-parametro-numerico on the server, you should first receive it as String and then make a conversion to the correct type, for example:

// Omitindo imports.

@WebServlet(urlPatterns={"/meuservlet"})
public class MeuServlet extends HttpServlet {

    @Override protected void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {

        // Obtém o valor como uma string.
        String valorDoParametroNumerico = request.getParameter("meu-parametro-numerico");

        // Converte para um inteiro.
        // Omitindo o tratamento de exceções e verificação/validação
        // se o parâmetro não é nulo ou vazio.
        Integer valorNumerico = Integer.parseInt(valorDoParametroNumerico);

        // Faz algo com o valor numérico...
    }
}
    
29.08.2015 / 23:54
4

Understand that in classes, materials and especially on the internet people make simplifications. Probably your teacher did a simplification. Or learned wrong, and is moving forward, I do not know: P

Each definition of terms needs a context. When you use a term in one context it can mean one thing, in another context it may mean something else. The Java language could define what it considers as primitive types. It is her prerogative to define this in context.

Even if it were not for this, Java could still say it is, if you wanted to.

What you learned in another discipline applies to that discipline, to that context. Probably in this context that you learned only the types in which most processors usually have dedicated instructions to manipulate them are considered primitive.

In the Java documentation there is a definition that string is part of the language and supported directly by the JVM and the compiler, that's all. But it is not defined as primitive. You can read more about this in Oracle's "official" tutorial . Often materials use wrong terms. And people reproduce it. And then others learn wrong.

If you know English you can read more about these types on Wikipedia .

Some people do not like this terminology. C # and other languages do not even use it.

What can be considered primitive type is when type is by value.

The string type is not a value type per se. It is a pointer for the string, so it is a type by reference. But for all purposes and it works as if it were a kind by value. It has its own identity and it is not possible to change only part of it.

Maybe the confusion will come from there.

I do not know if I should pass this but there is an answer that helps me understand the difference between types by value and by reference, but is in C # . Do not confuse, Java is a bit different. The basic idea is the same but Java, at least until version 8 does not allow you to create your own types by value. This can also help (or confuse more, I do not know).

    
29.08.2015 / 23:40
0

A very simple explanation is that String is an array of char, so it is an object. However, to facilitate the programmer's life the string behaves as a primitive type and also has object characteristics. The codes would be much larger and almost illegible if every time we were working with text we had to create vectors or lists to store them. At the beginning of object orientation learning this can be very confusing for anyone learning, so perhaps the explanations are simplified.

    
31.08.2015 / 15:23
0

It depends on the language!

Example, in C# there is the primitive type string and the Object String but in% w_th of% there is only the% w_th object.

C #:

string tipoString = "casa";
String objetoString = "casa";
String objetoString = new String("casa");

JAVA:

String tipoString = "casa";
String objetoString = new String("casa");//Porem não funciona passagem por referencia como os demais objetos.
    
03.09.2015 / 18:39