Skip to content

Notes

IT Notes → Java @ December 22, 2020

  • Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore (_).
  • Class members are given default values equivalent to binary 0s and objects null.
  • An assignment in if is fine if we assign a Boolean.
  • Modifiers:
    Default: Default has scope only inside the same package.
    Public: Public scope is visible everywhere.
    Protected: Protected has scope within the package and all sub classes.
    Private: Private has scope only within the classes.
    Final modifier is used to declare a field as final i.e. it prevents its content from being modified. Final field must be initialized when it is declared.
    Static Modifiers are used to create class variable and class methods which can be accessed without instance of a class. Let’s study how it works with variables and member functions. Static block is used to initialize static data member. Static block executes before main() method.
    Synchronised is used when a method is synchronized it can be accessed by only one thread at a time. We will discuss it in detail in Thread.
    Volatile modifier tells the compiler that the volatile variable can be changed unexpectedly by other parts of your program. Volatile variables are used in case of multithreading program. It keeps the local copy in sync with the main variable copy.
    Transient modifier is used for variables which need to be skipped for serialization.
  • Interfaces can be implemented by a class or extended by another interface whereas classes can only be extended.
  • Polymorphism can be done by overloading or overriding.
  • An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass’s method.
    1 We cannot widen and then box (int -> Long).
    2 We can box and then widen (int -> Object, via Integer).
    3 We can combine var args with either widening (byte -> int) or boxing (int -> Integer).
    4 Widening preferred over boxing.
    5 Widening preferred over var args.
    6 Boxing preferred over var args.
  • Overriding, the implementation given in base class is replaced with that in sub class.
    1 Return types cannot be more general.
    2 Access type cannot be more specific.
  • Constructor should always have a call to super or this. if we give our constructor default constructor not created. So, if we gave a constructor in base, we have to provide super in derived. The compiler generates a super() call in default constructors.
  • Static methods called by class name and instance by objects.
  • An instance cannot be passed to a function object because the Java passes variables by value that is even in case of objects the stack value referencing copy is passed.
  • Nested Classes … It is very important to state that the instance of the Inner class doesn’t exist in isolation, rather it is bounded by and associated with an instance of their enclosing top-level-class, and this impose a slight restriction on the inner nested classes which is that they can’t have any static members that normal top-level and static nested classes can. Anonymous inner classes come in handy when an object instance is required on the fly that extends a certain class or implement a certain interface without the need to write a complete top-level-class for that. Static nested classes exist in isolation from within the top-level-class that they are defined within. On the contrary to the non-static nested classes, static nested classes can’t directly access the members of the class that they are defined in (simply because they are static members, and in static class members can only access other static members directly), but they can do so using an object instance of the enclosing top-level-class.
  • Strings use same objects if same value and hence immutable. StringBuffer for thread safety and StringBuilder otherwise are faster for concatenations.
  • Serialization the object is stored in serialized fashion. transients are not stored and also static. ReadObject and Write object used.
  • Garbage collector finally calls Finalize method if no termination. It removes garbage in non-deterministic fashion.
  • In try-catch-finally finally is always executed and we have to catch the most specific exception first or compile time error. The exceptions are cascaded down. We can throw exceptions. If checked needs to be declared by throws keyword.
  • Throwable is at the top off all exceptions. Underneath Throwable you have Error and Exception. Underneath Exception you have RuntimeException. Unchecked exceptions represent defects in the program (bugs) – often invalid arguments passed to a non-private method. They are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException. A method is not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so). Unchecked exceptions represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files) are subclasses of Exception a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow) Errors are at same level as unchecked exceptions.  You should not catch an Error unless you really know what you are doing.
  • List, Sets, Maps and Ques.
  • Linked is ordered, Tree is sorted.
  • Vector is thread safe.
  • Equals and Hashcode can be overridden. It is better both uses same variables.
  • Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package, which very much says that.
  • Comparator should be used as an utility to sort objects which Comparable should be provided by default. Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Comparator can be used to produce different sorting.
  • Threads can be inherited from Thread class or using an instance of Runnable interface. Wait can wait till Notify occurs on the object or NotifyAll. Notify means inform any object waiting for the lock and NotifyAll means notify all objects. Yield means yielding thread execution and can result in the same thread execution if of highest priority. Sleep sleeps for required period of time and can result in InterruptedExcpetion if awaken before. Start will start thread and Run can be used to execute Run method. Join mean the joined thread is added infront for thread execution. If you call T1.join() from T2 it will wait for T1 to die (finish).IllegalMonitorStateException occurs if object is in illegal state.
  • Assertions (by way of the assert keyword) were added in Java 1.4. They are used to verify the correctness of an invariant in the code. Assetion trigger AssetionException if condition false and can be used to print message provided after: (colon).
  • The normal import declaration imports classes from packages, so that they can be used without package reference. Similarly, the static import declaration imports static members from classes and allowing them to be used without class reference.
  • JARs checked (1) …/jre/lib/ext with JAR files (2) the commandline classpath if overriden (3) Classpath variables if not overridden.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x