Difference between primitive type and object in Java

1

In Java, we have so-called primitive types and so-called objects. What's the difference between the two?

    
asked by anonymous 05.02.2018 / 17:47

2 answers

5

Let's understand In programming, what is an object? . Therefore all data are objects.

There is a misconception that there are only objects in object-oriented programming. If this were true, what would you call what is not an object?

In C # "everything" is object in the sense of OOP, that is, "everything" is derived from Object , there is no idea of primitive types. And the next Java will begin to abandon that idea as well (there will still be primitive types, but it will only be a specialization of value types that will also derive from Object ).

Primitive types would be those that are not derived from Object . A wrong idea for a language that says it's totally OO.

So let's differentiate what is derived from Object and what an object is generally.

In Java today, only classes can derive from Object , but this will change. Anyway all data is objects. Some have a taxonomy that indicates the name Object in their hierarchy, others do not have this.

Taxonomy is hierarchical, everything has several levels of classification. Each individualized data is an object, no matter specific characteristics. Some conform to Object . Object and object are different things, at different levels, they define different characteristics, they only have the same name.

It is often said that

  

The two most difficult problems of computing are cache override and naming things

     

- Phil Karlton.

When we have difficulty using the correct names, in the correct contexts we have difficulty understanding the problems correctly. Because OOP is so difficult, it requires you to sort everything correctly to work well. And the internet, or even other sources, is full of wrong nomenclatures. Only right understanding leads us on the right path.

I'm not saying that my understanding is correct, anyone can question it, but it's what we usually use in our environment when you think about what you're using.

The term object has always been used in all languages long before OOP becomes fashionable or even exists. We can not ignore this term because fashion has decided to appropriate the term for itself. Worse still if a language has been poorly conceptualized and is now having difficulty explaining what is right and implementing the necessary mechanism for what it idealized from the outset works.

The class-based type generates an object that is derived from Object . If a primitive type does not generate an object, does it generate what then?

To give more context, the question originated in the my answer (see comment too) in another question from AP. So every object only exists when it is instantiated, and what changes in instantiation only depends on whether the type is by value (the bad primitive term) or by reference. The allocation can not define whether it is an object or not, it always is, it does not matter whether it is in the or heap , whether it has a reference to it or not. >     

05.02.2018 / 17:58
0

Java has two types of data that are divided by value ( tipos primitivos ) and by reference ( tipos por referência ).

Primitive types are boolean, byte, char, short, int, long, float e double . Types by reference are classes that specify the object types Strings, Primitive Arrays, and Objects.

A variable of the primitive type can store exactly one value of its declared type at a time, when another value is assigned to that variable, its initial value will be replaced.

Instance variables of the primitive type are initialized by default, variables of types byte, char, short, int, long, float e double are initialized to 0, and variables of type boolean are initialized to false. These types can specify their own initial value for a variable of the primitive type by assigning the variable a value in its declaration.

Java provides two primitive types to store floating-point numbers in memory, type float and double.

The difference between them is that double variables can store numbers with greater magnitude and more detail, that is, it stores more digits to the right of the decimal fraction point, than float variables. Float variables represent simple precision floating point numbers and can represent up to 7 digits.

Variables of double type represent double-precision floating-point numbers, where they require twice the amount of memory of float variables providing 15 digits, which is double the precision of float variables. Double-type values are known as floating-point literals. For precise floating-point numbers, Java provides the BigDecimal class (java.math package).

Listing 2: Example of primitive type sizes

public class DataTypes {

public static void main(String[] args) {
    System.out.println("Tipos de dados em Java: \n" +
            "\nMenor Byte: " + Byte.MIN_VALUE +
            "\nMaior Byte: " + Byte.MAX_VALUE +
            "\nMenor Short Int: " + Short.MIN_VALUE +
            "\nMaior Short Int: " + Short.MAX_VALUE +
            "\nMenor Int: " + Integer.MIN_VALUE +
            "\nMaior Int: " + Integer.MAX_VALUE +
            "\nMenor Long: " + Long.MIN_VALUE +
            "\nMaior Long:" + Long.MAX_VALUE +
            "\nMenor Float: " + Float.MIN_VALUE +
            "\nMaior Float: " + Float.MAX_VALUE +
            "\nMenor Double: " + Double.MIN_VALUE +
            "\nMaior Double: " + Double.MAX_VALUE);

}

} Listing 3: Declaration of primitive types

public class Tipos_Primitivos {
    public static void main(String[] args) {
          byte tipoByte = 127;
          short tipoShort = 32767;
          char tipoChar = 'C';
          float tipoFloat = 2.6f;
          double tipoDouble = 3.59;
          int tipoInt = 2147483647;
          long tipoLong = 9223372036854775807L;
          boolean tipoBooleano = true;
          System.out.println("Valor do tipoByte = " + tipoByte);
          System.out.println("Valor do tipoShort = " + tipoShort);
          System.out.println("Valor do tipoChar = " + tipoChar); 
          System.out.println("Valor do tipoFloat = " + tipoFloat);
          System.out.println("Valor do tipoDouble = " + tipoDouble);
          System.out.println("Valor do tipoInt = " + tipoInt);
          System.out.println("Valor do tipoLong = " + tipoLong);
          System.out.println("Valor do tipoBooleano = " + tipoBooleano);
    }
}

In Listing 3 the type declared as char is always declared with single quotation marks because the size is only 1 character. The float types will always have the character "f" at the end of the value for their identification, the same thing with the long type only that the "L" character is inserted.

Types by Reference Programs use reference type variables to store object locations in the computer's memory. These objects that are referenced can contain multiple instance variables and methods within the pointed object.

To bring your instance methods into an object, you need to reference some object. The reference variables are initialized with the value "null" (null).

For example, ClasseConta acao = new ClasseConta() creates an object of class ClasseConta and the action variable contains a reference to that ClasseConta object, where it can invoke all its class methods and attributes. The new keyword asks the system memory to store an object and initializes the object.

Listing 4: Example accessing an object method

public class AcessaMetodo {

    public void imprime(){
        System.out.println("Bem Vindo ao Java!");
    }

    public static void main(String[] args) {
        AcessaMetodo acessa = new AcessaMetodo ();
        acessa.imprime();

    }

}

The output of this code above will be reproduced through the access.imprime () action, because the method of the object that was initialized with the variable defined as "access" is being accessed.

Final considerations The variables of tipos por valor não referenciam objetos , these types of variables can not be used to invoke methods. Remember that local variables are not initialized by default (variables within methods). Primitive type variables can not be initialized as an object reference.

Source: link

Wrapper Classes in Java

    
05.02.2018 / 17:58