0% found this document useful (0 votes)
2 views17 pages

java 5

The document provides comprehensive notes on advanced Java concepts for the DSSSB TGT Computer Science exam, covering topics such as the Object class, wrapper classes, important Java keywords, memory management, and input methods. It includes examples and explanations of key features like static variables, final and abstract keywords, and string comparison. Additionally, it outlines exam strategies focusing on output tracing, inheritance, and exception flow.

Uploaded by

Arpit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

java 5

The document provides comprehensive notes on advanced Java concepts for the DSSSB TGT Computer Science exam, covering topics such as the Object class, wrapper classes, important Java keywords, memory management, and input methods. It includes examples and explanations of key features like static variables, final and abstract keywords, and string comparison. Additionally, it outlines exam strategies focusing on output tracing, inheritance, and exception flow.

Uploaded by

Arpit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

JAVA NOTES FOR DSSSB TGT COMPUTER

SCIENCE EXAM
PART 5: ADVANCED CONCEPTS, KEYWORDS,
WRAPPER CLASSES AND FINAL REVISION

1. Object Class in Java


Object is the root class of Java class hierarchy.

Every class directly or indirectly inherits from:


Object

Example:
class Student
{

Internally:
class Student extends Object
{

2. Important Methods of Object Class


1. toString()
Returns string representation of object.
Default output:
Class name + @ + hashcode
Example:
class Test
{
public static void main(String args[])
{
Test t=new Test();

[Link](t);
}
}

Output:
Test@15db9742

2. equals()
Compares objects.
Default behavior:
Compares references.
Example:
class Test
{
public static void main(String args[])
{
String a=new String("Java");
String b=new String("Java");

[Link]([Link](b));
}
}

Output:
true

Reason:
String overrides equals().

3. hashCode()
Returns hash value of object.
Used in:
• HashMap
• HashSet

4. getClass()
Returns runtime class information.
Example:
[Link]([Link]());
5. clone()
Creates copy of object.
Requires:
Cloneable interface

6. finalize()
Called before garbage collection.
Note:
Deprecated in modern Java versions.

3. Wrapper Classes
Wrapper classes convert primitive data types into objects.
Primitive → Wrapper

Primitive Wrapper
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

4. Need for Wrapper Classes


Collections store only objects.
Example:
Wrong:
ArrayList<int> list;

Correct:
ArrayList<Integer> list;
5. Autoboxing
Automatic conversion:
Primitive → Object
Example:
int x=10;

Integer i=x;

6. Unboxing
Object → Primitive
Example:
Integer i=20;

int x=i;

7. Parsing Methods
Convert String to primitive.
Example:
int x=[Link]("100");

Output:
100

Important methods:

Method Conversion
[Link]() String to int
[Link]() String to double
[Link]() String to boolean

8. Java Keywords Important for DSSSB


Java has:
50 reserved keywords
Important keywords:
• class
• interface
• extends
• implements
• package
• import
• public
• private
• protected
• static
• final
• abstract
• this
• super
• new
• null
• try
• catch
• finally
• throw
• throws
• synchronized
• volatile
• transient

9. static Keyword
static belongs to class rather than object.
Used with:
1. Variable
2. Method
3. Block
4. Class

10. Static Variable


Shared among all objects.
Example:
class Student
{
static int count=0;

Student()
{
count++;
}
}

All objects share one count variable.

11. Static Method


Can be called without object.
Example:
class Test
{
static void show()
{
[Link]("Hello");
}

public static void main(String args[])


{
show();
}
}

12. Static Method Restrictions


A static method cannot directly access:
• Non-static variables
• Non-static methods
Example:
Wrong:
class Test
{
int x=10;

static void show()


{
[Link](x);
}
}

13. Static Block


Executes when class loads.
Example:
class Test
{

static
{
[Link]("Static Block");
}

public static void main(String args[])


{
[Link]("Main");
}

Output:
Static Block
Main

14. final Keyword


Used to restrict modification.

final Variable
Cannot change value.
Example:
final int x=10;
final Method
Cannot be overridden.

final Class
Cannot be inherited.
Example:
final class A
{

15. abstract Keyword


Used for incomplete classes and methods.
Example:
abstract class Shape
{
abstract void draw();
}

16. Interface Keyword


Used to create interfaces.
Example:
interface A
{

17. instanceof Operator


Checks object type.
Returns:
boolean
Example:
String s="Java";
[Link](s instanceof String);

Output:
true

18. String Advanced Concepts

String Constant Pool


Java stores string literals in a special memory area.
Example:
String a="Java";
String b="Java";

Both refer to same object.

19. String Comparison Questions


Example 1
class Test
{
public static void main(String args[])
{
String a="Java";
String b="Java";

[Link](a==b);
}
}

Output:
true

Example 2
class Test
{
public static void main(String args[])
{
String a=new String("Java");
String b=new String("Java");

[Link](a==b);
}
}

Output:
false

Example 3
class Test
{
public static void main(String args[])
{
String a=new String("Java");
String b=new String("Java");

[Link]([Link](b));
}
}

Output:
true

20. StringBuffer vs StringBuilder


StringBuffer StringBuilder
Mutable Mutable
Thread safe Not thread safe
Slower Faster

21. Java Memory Management


Java memory areas:
1. Heap
2. Stack
3. Method Area
4. PC Register
5. Native Method Stack
22. Heap Memory
Stores:
• Objects
• Instance variables
Managed by:
Garbage Collector

23. Stack Memory


Stores:
• Local variables
• Method calls
• References
Each thread has its own stack.

24. Garbage Collection


Automatic memory cleanup mechanism.
Object becomes eligible when:
1. Reference set to null
Example:
Student s=new Student();

s=null;

2. Reference reassigned
Example:
Student a=new Student();

a=new Student();

3. Object created anonymously


Example:
new Student();

25. [Link]()
Requests garbage collector.
Example:
[Link]();

Note:
It does not guarantee execution.

26. Java Input Methods


Scanner Class
Package:
[Link]

Example:
Scanner sc=new Scanner([Link]);

int x=[Link]();

BufferedReader
Package:
[Link]

Example:
BufferedReader br=
new BufferedReader(new InputStreamReader([Link]));

27. Command Line Arguments


Arguments passed during program execution.
Example:
class Test
{
public static void main(String args[])
{
[Link](args[0]);
}
}

Execution:
java Test Hello

Output:
Hello

28. Important Java Output Programs

Program 1
class Test
{
public static void main(String args[])
{
int x=10;

[Link](x++);

[Link](++x);
}
}

Output:
10
12

Explanation:
x++ prints first, then increases.

Program 2
class Test
{
public static void main(String args[])
{
int a=5;

[Link](a>3?"Yes":"No");
}
}
Output:
Yes

Program 3
class Test
{
public static void main(String args[])
{
int x=1;

while(x<=3)
{
[Link](x);
x++;
}
}
}

Output:
123

Program 4
class Test
{
public static void main(String args[])
{
for(int i=0;i<3;i++)
{
[Link](i);
}
}
}

Output:
0
1
2

Program 5
class Test
{
public static void main(String args[])
{
String s="Java";
[Link]("Programming");

[Link](s);
}
}

Output:
Java

Reason:
String is immutable.

Program 6
class Test
{
public static void main(String args[])
{
int a=10;

if(a=5)
{
[Link]("Hello");
}
}
}

Output:
Compilation Error
Reason:
Assignment returns int, not boolean.

Program 7
class Test
{
public static void main(String args[])
{
int x=10;

{
int y=20;

[Link](y);
}

[Link](y);
}
}
Output:
Compilation Error
Reason:
y is block scoped.

Program 8
class Test
{
public static void main(String args[])
{
[Link](10+20+"Java");
[Link]("Java"+10+20);
}
}

Output:
30Java
Java1020

Reason:
String concatenation works from left to right.

29. Java vs C++


Java C++
Platform independent Platform dependent
Uses JVM Direct machine execution
No pointer arithmetic Supports pointers
Automatic garbage collection Manual memory management
Multiple inheritance through interfaces Multiple inheritance through classes

30. Java Important One-Liners for DSSSB


1. Java was developed by James Gosling.
2. Java was released in 1995.
3. Java source file extension is .java.
4. Bytecode file extension is .class.
5. JVM executes bytecode.
6. JDK contains JRE.
7. JRE contains JVM.
8. String objects are immutable.
9. StringBuffer is synchronized.
[Link] is faster than StringBuffer.
[Link] is the root class.
[Link] belongs to class.
[Link] prevents modification.
[Link] represents incomplete implementation.
[Link] supports multiple inheritance.
[Link] has no return type.
[Link]() is static because JVM calls it directly.
[Link] collection is automatic.
[Link] indexing starts from 0.
[Link] is both compiled and interpreted.

END OF JAVA COMPLETE NOTES


DSSSB TGT Exam Strategy:
Focus maximum on:
• Output tracing
• String comparison
• Constructor execution order
• Inheritance output
• static/final/this/super
• Exception flow
• Thread methods
• Collection behavior
• JVM memory concepts

You might also like