What is Nested Class in Java?
What is Nested Class in Java?
Read lessSign up to our innovative Q&A platform to pose your queries, share your wisdom, and engage with a community of inquisitive minds.
Log in to our dynamic platform to ask insightful questions, provide valuable answers, and connect with a vibrant community of curious minds.
Forgot your password? No worries, we're here to help! Simply enter your email address, and we'll send you a link. Click the link, and you'll receive another email with a temporary password. Use that password to log in and set up your new one!
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What is Nested Class in Java?
What is Nested Class in Java?
Read lessWhat is the difference between whitespace and unicode space character.
What is the difference between whitespace and unicode space character.
Read lessKey Difference Term Whitespace Unicode Space Character Definition Any character that creates "blank" space in text (invisible characters that separate words or lines). Specific space-like characters defined in the Unicode standard. Scope A broad category that includes a variety of invisible characteRead more
Key Difference
Term Whitespace Unicode Space Character
Definition Any character that creates “blank” space in text (invisible characters that separate words or lines). Specific space-like characters defined in the Unicode standard.
Scope A broad category that includes a variety of invisible characters like spaces, tabs, and newlines. A subset of Unicode characters that are defined as various types of space.
Examples ‘ ‘ (space), \n (newline), \t (tab), \r (carriage return) U+0020 (Space), U+00A0 (No-Break Space), U+2003 (Em Space), U+2009 (Thin Space), etc.
In Java / Programming Identified by Character.isWhitespace() Each Unicode space has a specific code point, width, and behavior in rendering.
1. Whitespace Characters
These are general characters that create space but are often interpreted by programming languages or parsers.
In Java, Character.isWhitespace(c) returns true for:
Standard space ‘ ‘ (U+0020)
Tab \t (U+0009)
Newline \n (U+000A)
Carriage return \r (U+000D)
Vertical tab \u000B
Form feed \u000C
All Unicode characters categorized as whitespace.
2. Unicode Space Characters
Unicode defines many space characters explicitly, each with a specific purpose or width. Here are a few notable ones:
Unicode Name Width/Use
U+0020 Space Standard space character
U+00A0 No-Break Space Same as space but prevents line breaks
U+2000 En Quad Space equal to 1 en
U+2001 Em Quad Space equal to 1 em
U+2002 En Space Narrower than em space
U+2003 Em Space Wider space for typesetting
U+2009 Thin Space Very narrow space
U+202F Narrow No-Break Space Narrower than no-break space
U+3000 Ideographic Space Used in East Asian scripts, full-width
These characters may not be detected by simple string manipulations unless Unicode-aware methods are used.
Important Distinctions
All Unicode space characters are whitespace, but not all whitespace characters are Unicode space characters.
Some whitespace characters (like \n, \t) are control characters, not printable spaces.
Unicode spaces may have width, non-breaking behavior, or typographic purpose.
Summary
Concept Includes
Whitespace Spaces, tabs, newlines, form feeds, etc.
Unicode Space Characters Precisely defined space characters like U+00A0, U+2002, U+2003, etc.
In Java programming sum(5,6) will call for which of these functions in a class sum(double a, int b) or sum(int a, int b) ?
In Java programming sum(5,6) will call for which of these functions in a class sum(double a, int b) or sum(int a, int b) ?
Read lessIn Java, the method that will be called when you write sum(5, 6) depends on method overloading resolution, which considers the most specific match based on the types of the arguments. Given: sum(5, 6); Here, both arguments are integers (int literals). And you have two overloaded methods: sum(int a,Read more
In Java, the method that will be called when you write sum(5, 6) depends on method overloading resolution, which considers the most specific match based on the types of the arguments.
Given:
sum(5, 6);
Here, both arguments are integers (int literals).
And you have two overloaded methods:
sum(int a, int b)
sum(double a, int b)
Resolution:
Java will choose the most specific method that matches the argument types without needing conversion.
sum(int a, int b) matches exactly.
sum(double a, int b) would require widening the first int to a double.
Therefore, sum(int a, int b) will be called.
Summary:
In Java, when overloading methods:
Java prefers exact matches.
Widening conversions (like int to double) are only used if no exact match is found.
So:
sum(5, 6); // calls sum(int a, int b)
See lessWhat is the difference between next() and nextLine()?
What is the difference between next() and nextLine()?
Read less1. next() Function: Reads the next token (word) from input. Delimiter: Stops reading when it encounters whitespace (space, tab, or newline). Ignores: Leading whitespace before the token. Use case: Good for reading single words. Example: Scanner sc = new Scanner(System.in); System.out.print("Enter yoRead more
next()
Function: Reads the next token (word) from input.
Delimiter: Stops reading when it encounters whitespace (space, tab, or newline).
Ignores: Leading whitespace before the token.
Use case: Good for reading single words.
Example:
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.next();
System.out.println("You entered: " + name);
Input:
Rahul Sharma
Output:
You entered: Rahul
👉 It only captures "Rahul"
because next()
stops at the first space.
nextLine()
Function: Reads the entire line of input (until Enter/\n
).
Delimiter: Stops only when the newline character is encountered.
Use case: Good for reading sentences or full lines with spaces.
Example:
Scanner sc = new Scanner(System.in);
System.out.print("Enter your full name: ");
String name = sc.nextLine();
System.out.println("You entered: " + name);
Input:
Rahul Sharma
Output:
You entered: Rahul Sharma
👉 Here it captures the whole line, including spaces.
Feature | next() | nextLine() |
---|---|---|
Reads up to | Whitespace (space, tab, newline) | End of line (\n ) |
Can read spaces? | ❌ No (stops at space) | ✅ Yes (includes spaces) |
Best for | Single words/tokens | Full sentences / whole line |
In Java, consider the following code snippet:Scanner sc = new Scanner(System.in); System.out.print("Enter your age: "); int age = sc.nextInt(); System.out.print("Enter your full name: "); String name = sc.nextLine(); System.out.println("Age: " + age); System.out.println("Name: " + name);When ...Read more
In Java, consider the following code snippet:
Scanner sc = new Scanner(System.in);System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.print("Enter your full name: ");
String name = sc.nextLine();
System.out.println("Age: " + age);
System.out.println("Name: " + name);
When the input is:
20
Rahul Sharma
The output is:
Age: 20
Name:
Explain why the nextLine()
method appears to “skip” input in this case.
The nextLine() method appears to skip input because after executing nextInt(), the newline character (\n) from pressing Enter is still left in the input buffer. When nextLine() is called immediately after, it reads this leftover newline character instead of waiting for new user input. As a result, iRead more
The nextLine()
method appears to skip input because after executing nextInt()
, the newline character (\n
) from pressing Enter is still left in the input buffer.
When nextLine()
is called immediately after, it reads this leftover newline character instead of waiting for new user input. As a result, it returns an empty string and seems to “skip” the input.
To fix the issue, insert an extra sc.nextLine();
after nextInt()
to consume the leftover newline character.
Scanner sc = new Scanner(System.in);
System.out.print(“Enter your age: “);
int age = sc.nextInt();
sc.nextLine(); // consume the leftover newline
System.out.print(“Enter your full name: “);
String name = sc.nextLine();
System.out.println(“Age: ” + age);
System.out.println(“Name: ” + name);
Now, if the input is:
20
Rahul Sharma
The output will be:
Age: 20
Name: Rahul Sharma
How is Nested Class different from Inheritance?
How is Nested Class different from Inheritance?
Read lessA nested class is not the same as an inherited class. Let’s see why with simple examples. Nested Classes A nested class is just a class declared inside another class. (a) Static Nested Class class School { static class Student { void showDetails() { System.out.println("I am a student of the school."Read more
A nested class is not the same as an inherited class. Let’s see why with simple examples.
A nested class is just a class declared inside another class.
(a) Static Nested Class
class School {
static class Student {
void showDetails() {
System.out.println(“I am a student of the school.”);
}
}
}
public class Demo {
public static void main(String[] args) {
School.Student s = new School.Student();
s.showDetails();
}
}
(b) Inner Class (Non-static)
class School {
class Teacher {
void display() {
System.out.println(“I am a teacher of the school.”);
}
}
}
public class Demo {
public static void main(String[] args) {
School school = new School();
School.Teacher t = school.new Teacher();
t.display();
}
}
Inheritance happens when one class extends another.
class Person {
void displayInfo() {
System.out.println(“I am a person.”);
}
}
class Student extends Person {
void showDetails() {
System.out.println(“I am a student.”);
}
}
public class Demo {
public static void main(String[] args) {
Student s = new Student();
s.displayInfo(); // inherited from Person
s.showDetails(); // defined in Student
}
}
A nested class is a member of its enclosing class. It establishes a structural relationship where one class is entirely contained within the declaration of another. This allows the nested class to be closely associated with the functionality of the outer class and can even grant it special access prRead more
A nested class is a member of its enclosing class. It establishes a structural relationship where one class is entirely contained within the declaration of another. This allows the nested class to be closely associated with the functionality of the outer class and can even grant it special access privileges (especially inner classes) to the outer class’s private members.
See less