Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBrendan Passey
999 PointsIs 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
15,141 PointsA 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).