📖 Scenario: Imagine you are organizing a small library. You want to keep track of the number of books and the details of each book separately.
🎯 Goal: You will create variables to store the number of books (a simple number) and the details of each book (like title and author) using objects. This will help you understand how Java stores simple values and objects differently.
📋 What You'll Learn
Create a primitive variable to store the number of books
Create a class called Book with two fields: title and author
Create an object of Book with specific values
Print the number of books and the book details
💡 Why This Matters
🌍 Real World
Understanding how Java stores simple data and objects helps when managing data in apps, games, or websites.
💼 Career
Knowing primitive vs reference storage is essential for Java developers to write efficient and bug-free code.
Progress0 / 4 steps
1
Create a primitive variable for the number of books
Create an int variable called numberOfBooks and set it to 3.
Java
Hint
Use int to store whole numbers like the count of books.
2
Create a Book class with title and author fields
Create a class called Book with two String fields: title and author.
Java
Hint
Define a class outside Library with two String fields.
3
Create a Book object with specific title and author
Inside the main method, create a Book object called myBook. Set its title to "Java Basics" and author to "Alice".
Java
Hint
Create the object with new Book() and assign values to fields using dot notation.
4
Print the number of books and book details
Use System.out.println to print the numberOfBooks, then print the title and author of myBook each on a new line.
Java
Hint
Print the primitive variable and then the object's fields using System.out.println.
Practice
(1/5)
1. Which of the following correctly describes how primitive types are stored in Java?
easy
A. They store values as strings internally.
B. They store the actual value directly in the variable.
C. They store the memory address of the variable.
D. They store a reference to an object in memory.
Solution
Step 1: Understand primitive storage
Primitive types like int, double, and boolean store the actual data value directly in the variable's memory location.
Step 2: Compare with reference types
Reference types store addresses pointing to objects, not the actual data value.
Final Answer:
They store the actual value directly in the variable. -> Option B
Quick Check:
Primitive = actual value [OK]
Hint: Primitives hold values; references hold addresses [OK]
Common Mistakes:
Thinking primitives store memory addresses
Confusing reference storage with primitive storage
Assuming primitives store objects
2. Which of the following is the correct way to declare a reference variable for a String in Java?
easy
A. int s = new String();
B. boolean s = new String();
C. String s = 10;
D. String s = new String();
Solution
Step 1: Check variable type and assignment
String is a reference type, so it can be assigned with new String() to create a new object.
Step 2: Verify type compatibility
int and boolean are primitive types and cannot be assigned a String object.