0% found this document useful (0 votes)
10 views

Java notes

My own notes for java
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java notes

My own notes for java
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Objects in java store the value of the reference.

All objects store inside a heap.


an array is always an object no matter the data type.

Instance variables are assigned value by default, even if you don’t set them explicitly.

Write you code by following:


Prep Code
Test Code
real Code

Write test code before you implement methods.

Normally reference type and object type must be same but with polymorphism , reference type and object
type can be different.
With polymorphism, the reference type can be a superclass of the actual object type.

With polymorphism, you can write code that doesn’t


have to change when you introduce new subclass
types into the program.

# When classes cant be subclassed:

A non-public class can be subclassed only by classes in the same package as the class.

A final class means that it’s the end of the inheritance line. Nobody, ever, can extend a final
class.

The third issue is that if a class has only private constructors, it can’t be subclassed.

If you want to protect a specific method from being overridden, mark the method with the final modifier.

To avoid anyone from instantiating a class, use the abstract keyword before it . The compiler won’t let you
instantiate an abstract class.
A class that’s not abstract is called a concrete class.

you can abstract a method as well.An abstract method means the method must be overridden.
public abstract void eat();

If you declare an abstract method, you MUST mark the class abstract as well. You can’t have an abstract
method in a non-abstract class.
Abstract methods don’t have a body; they exist solely for polymorphism.

Every class in Java extends class Object. Object is a non-abstract class


Some Methods of the class Object are:
1. boolean equals()
2. Class getClass()
3. int hashcode()
4. String toString()

if you want to use a refeernce variable of child class to an object of child class whose reference is Parent
class then you have to type cast like:
Dog myDog= (Dog)animal; (animal is a Superclass reference)

You can call a method on an object only if the class of the reference variable has that method.

An "Interface" is a pure abstract class where all the methods are abstract. It helps us in avoid Deadly
Diamond of Death.

use Serializable interface if you want an object to be able to save its state to a file.

where things get stored:


1. Instance variables live within the object they belong to, on the Heap.
2. If the instance variable is a reference to an object, both the reference and the object it refers to are on
the Heap.
3. All local variables live on the stack, int the frame corresponding to the method where the variables are
declared.
4. if the local variable is a reference, it goes on the stack.
5. All object live in the heap, regardless of whether the reference is a local or instance variable.

Instance variables are assigned a default value.

Important -> The call to super() must be the first statement in each constructor!

A constructor can have a call to super() OR this(), but never both!

Final variable -> can’t change its value


Final Method -> can’t override it
Final Class -> can’t extend it

Note -> floating-point literals are assumed


to be doubles unless you add the ‘f.’

Format Specifiers in String.format();


d -> decimal
f -> float numbers
x -> hexadecimal
c -> character

toString() method is called each time an object is printed.

public <T extends Animal> void takeThing(ArrayList<T> list) -> using generic type which is not included in
the class declaration.
In generics extends means "extends or implements". So Animal in the above code can be both an
interface or a class.

The sort method of the objects only takes comparable objects. And comparable is an interface. So only
the object that implement comparable<T> can run .sort() method.

Comparable interface only has one method which is "int compareTo(T o)" where T is a generic.

There is another interface called Comparator<T> which has method "int compare(T o1,T o2);".
you can use it like "List.sort(comparator)" or "Collections.sort(List,comparator)". In this , the sort methods
uses the compare method of the comparator instead of the compare method of the comparable.

to use comparator. First create a class that implements the comparator<T> interface and make an object
of that. Then pass that object to the List.sort(Object_you_created)

# Lambda

If an interface has only one method to implement , it can be implemented as Lambda Expression.

# Object Equality:

Reference Equality and Object equality are different things.

Reference equality is the comparison of hashcodes and the object equality is the object’s .equals()
method.

most versions of Java assign a hashcode based on the object’s memory address on the heap, so no two
objects will have the same hashcode.

Method of comparing two objects -> first the hashcode


are compared and then the equals method.
That’s why if you override equals(), you MUST override hashCode().

By default -> a.equals(b) => a.hashCode() == b.hashCode() . So by default the equals method just
compares the references of the objects.

Tip -> use TreeSort for the sorted Set. You can pass in a comparator too.

You might also like