Java Java Basics Perfecting the Prototype Censoring Words - Using String Equality

Brendan Passey
Brendan Passey
999 Points

Is the capitolised 'Integer' a class or an object?

In the previous video Craig said each primitive data type has an equivalent boxed type and these are classes. But at 0.35 in this video he refers to them as objects. Can anyone clarify? I'm a little confused by the difference between classes and objects in general. It seems you can expose a method from either with the same format i.e.

String example = console.readLine("is console an object or a class?");

here I know readLine is a method but is console considered a class or object? Like in this format of something.method "something" can either be a class or an object and you can expose a method from either.. Am I missing something?

1 Answer

Adam Pala
Adam Pala
15,141 Points

A class and an object are related concepts. A class definition is a "factory" or "blue print" that defines how an object is created/instantiated (i.e., what data attributes and/or methods the object instance will have). Thus, typically an object of a class is declared in your code when you use the class (e.g., "new Integer(...)" creates an object of the Integer class, which can be assigned to a variable, manipulated with methods, etc.). In general, many such objects can be declared and used in your code, as defined in the one class definition of those objects.

With the Integer class specifically, boxing to Integer produces an object which provides the additional methods (e.g., values, parses, etc.) relative to its primitive data type counterpart int, which lacks these additional methods (but is more perfomant due to less overhead associated with these additional methods, hence why Java provides primitive data types in the first place rather than only relying on having wrapper classes).