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

Asked

Viewed 822 times

3

I was in doubt in a class when the teacher said that a guy String was considered as primitive! But I learned in Object-Oriented Programming that are only primitive:

Byte: 1 byte.
Short: 2 bytes.
Int: 4 bytes.
Long: 8 bytes.
Float: 4 bytes.
Double: 8 bytes.
  • 1

    String is rather primitive type in numerous languages, including some of relatively low level as Delphi/Object Pascal. The types above are the primitive types of the C language, although with different names. C++ still has Boolean and wchar as primitive types.

4 answers

4

Understand that in classes, materials and mainly on the internet people make simplifications. Probably your teacher made a simplification. Or he learned wrong, and he’s moving on, I don’t know :P

Every definition of terms needs a context. When you use a term in one context it can mean one thing, in another context it can mean something else, it cannot be the opposite, but there will be differences. The Java language could define what it regards as primitive types. It is her prerogative to define this in its context.

Even if it wasn’t this, still Java could say it is, if it wanted to.

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

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

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

Some people don’t like this terminology. C# and other languages don’t even use it.

What can be considered primitive type is when the type is by value, but it depends on context, it is not so common.

The guy string is not a type by value itself. He is a pointer for the string, so it is a type by reference. But for all intents and purposes and it works as if it were a type by value, it has its own identity and it is not possible to change only a part of it, it is immutable.

Maybe that’s where the confusion comes from.

I don’t know if I should pass this but there’s an answer that helps understand the difference between types by value and by reference, but in C#. Don’t confuse it, Java is a little 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 one can also help (or confuse more, I don’t know).

I’ve talked about this in What is considered primitive in a programming language?.

  • I clearly know the difference between a primitive type and a type of reference like a String (string sequence) !! Decidedly in java programming even if it is for the web the primitive types of the language have already been decided, how would a String be a primitive type ? I don’t agree!!! But I’ll check the links!! Thanks for not giving up on me!!!

  • Here nobody has anything personal against anyone, everything is a question of quality of the question. This is not good, but it is reasonable, I could answer.

  • Yes sir!! Now I will take a look at the links above!!

3


This is the first time I’ve read this, so maybe your teacher can explain for sure what he meant. But by kicking, it may be connected to the parameters of a request come as String in Java, regardless of the type 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, first you should 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...
    }
}
  • That’s right!! That’s how I’ve been thinking!!

  • 1

    Tbm found this, that http only uses plain text, there is no int and other types, a conversion must be done upon receiving the form values.

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 unreadable if every time we went to work 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 those who are learning, maybe that’s why explanations are simplified.

0

Language-dependent!

For example, in C# there is the primitive type string and the Object String but in the java only exists the object String.

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.