📖 Scenario: You are creating a simple program to store and display information about a book. This will help you understand how reference data types work in Java.
🎯 Goal: Build a Java program that creates a Book object with a title and author, then prints the book details.
📋 What You'll Learn
Create a class called Book with two String fields: title and author.
Create an instance of Book with specific title and author values.
Use a variable to hold the Book object reference.
Print the book's title and author using the object reference.
💡 Why This Matters
🌍 Real World
Reference data types like objects are used to model real-world things in programs, such as books, people, or products.
💼 Career
Understanding how to create and use objects is fundamental for Java programming jobs, especially in software development and application design.
Progress0 / 4 steps
1
Create the Book class with fields
Write a class called Book with two public String fields named title and author. Do not add any methods yet.
Java
Hint
Use public String title; and public String author; inside the class.
2
Create a Book object with title and author
In the Main class, create a variable called myBook of type Book. Then create a new Book object and assign it to myBook. Set myBook.title to "The Java Journey" and myBook.author to "Alex Reader".
Java
Hint
Use Book myBook = new Book(); to create the object, then set fields with myBook.title = ....
3
Use the myBook reference to access fields
Add a String variable called bookInfo that combines myBook.title and myBook.author in this format: "Title: The Java Journey, Author: Alex Reader".
Java
Hint
Use string concatenation with +"Title: " + myBook.title + ", Author: " + myBook.author.
4
Print the book information
Use System.out.println to print the bookInfo variable.
Java
Hint
Use System.out.println(bookInfo); to display the text.
Practice
(1/5)
1.
Which of the following is a reference data type in Java?
int, double, String, boolean
easy
A. String
B. int
C. double
D. boolean
Solution
Step 1: Identify primitive vs reference types
int, double, and boolean are primitive types storing actual values.
Step 2: Recognize String as a reference type
String stores a reference to an object containing text data.
Final Answer:
String -> Option A
Quick Check:
Reference type = String [OK]
Hint: Remember: String is an object, others are primitives [OK]
Common Mistakes:
Confusing String as primitive
Thinking int is a reference type
Assuming all data types are reference types
2.
Which of the following is the correct way to create a new String object in Java?
?
easy
A. String s = new string("hello");
B. String s = new String("hello");
C. String s = String("hello");
D. String s = new String["hello"];
Solution
Step 1: Check correct constructor syntax
Use 'new' keyword with class name and parentheses: new String("hello")
Step 2: Verify case sensitivity and brackets
Class names are case-sensitive (String, not string), and arrays use [] differently.
Final Answer:
String s = new String("hello"); -> Option B
Quick Check:
Correct object creation syntax = String s = new String("hello"); [OK]
Hint: Use 'new ClassName()' with exact case for objects [OK]
Common Mistakes:
Using lowercase 'string' instead of 'String'
Missing 'new' keyword
Using array brackets [] incorrectly
3.
What will be the output of the following code?
String a = "hello";
String b = a;
b = b + " world";
System.out.println(a);
medium
A. world
B. hello world
C. hello
D. Compilation error
Solution
Step 1: Understand reference assignment
Variable b initially points to the same String object as a ("hello").
Step 2: String concatenation creates new object
b = b + " world" creates a new String object; a still points to original "hello".
Final Answer:
hello -> Option C
Quick Check:
Strings are immutable; original stays same [OK]
Hint: String concatenation creates new object, original unchanged [OK]