munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.87k stars 1.04k forks source link

Just A Question #1074

Closed Blqtent closed 2 years ago

Blqtent commented 2 years ago

in this code snippet: package com.craftinginterpreters.lox; class Token { final TokenType type; final String lexeme; final Object literal; final int line; Token(TokenType type, String lexeme, Object literal, int line) { this.type = type; this.lexeme = lexeme; this.literal = literal; this.line = line; } public String toString() { return type + " " + lexeme + " " + literal; } }

what is the line final Object literal supposed to mean? Like is there a class called Object? I don't see it, if there is. Pls respond ASAP, thank you :)

chrisjbreisch commented 2 years ago

I don't know Java, but in other similar languages there's a built-in 'Object' (or sometimes 'object') class that all other classes implicitly inherit from. Everything is an 'Object' (or 'object'). And in some languages it is literally everything. Even value types like 'int' or 'float' inherit from 'Object'.

I assume that's the case here. I did the book in C#, rather than Java. And C# has an 'object' class, which is what I usually used to replaces the book's 'Object'.

munificent commented 2 years ago

Like is there a class called Object?

Yes, Object is built in to the Java core library as the root class of the entire class hierarchy.