Home > J2SE > Core Java Question Answers

Core Java Question Answers

  • Static members:-
  • Certain member should only belong to class, and not be part of any object created from the class.
  • Static member is initialized when a class is loaded at run time.
!Note
  • Java enforce a rule that at the most, one class in a java file has public accessibility.
  • A Class is compiled to Java byte code.
  • Reserved literals in Java: null, true, false
  • int i = 010; system.out.println(“i = ” + i); Output => 8
Primitive data types
  • Integral type: int 16 0, long 32 0L, byte 8 0, short 16 0, unsigned char 16 ‘\u0000′
  • Float type: float 23; double 64
  • Boolean type: boolean, default is false.
  • Reference type are default to null
  • Initialization of primitive variable are checked at compile time
main()
  • public static void main(String args[]) throws Exception works
  • public : so that it is accessible to jvm
  • static : so that class object is not needed to call it.
  • if void or static not present, we get run time error “Exception in thread “main” java.lang.NoSuchMethodError: main”
Cast Oprator
  • We need casting when narrowing down in the hierarchic

Arithmetic

  • Integer Arithmetic always return a value which is in the range, other than divide by zero which gives Arithmetic Exception.
  • Floating point Arithmetic can result in value that are out of range
  • When divide by zero, Infinity and -Infinity; 0.0F and -0.0F, NaN(0.0/0,0)
Parameter passing
  • Primitive: Values are copied.
  • Object Reference: Can only change the state not the value (can not change to null … hence always call by value)
  • Passing array reference is same as Object reference passing

Static

  • Static code can not access non static members by there simple name
  • static code is not executed in the context of an object, hence this and supper are not available.
Compiling
  • To put class files in package directory
  • javac -d . MainTests.
Abstract Class
  • Can not be instantiated
  • must be abstract if it has one+ abstract method
  • Sub class not implementing abstract from supper, must also be abstract
Interface
  • just specify method prototype and implicitly abstract.
  • abstract interface abc, -> Redundent
final class
  • can not be extended
  • class whose definition is complete
Other modifiers for members
static member
final member
  • primitive final variable can not change its value once initialized
  • reference type final variable once initialized can not change its reference, but can change the state of the object it refers.
abstract method
  • Only an instance method can be declared as abstract
  • Static method can not be overridden, declaring an abstract static method make no sense.
  • MainTests.java:4: illegal combination of modifiers: abstract and static
  • method specified in an interface is implicitly abstract
synchronized method
native method
  • Implementation is defined in other language
transient fields
  • such field will not be stored to persistent storage when object will be stored.
  • Static are by default transient.
volatile field
  • stop compiler from optimizing the field by caching its value, preventing unpredictable results when retrieving the value in a multi-threaded environment.
Switch statment
  • one default label
  • all labels after the matched label are executed until u add break statement
Exception
  • All exception are derived from java.lang.Throwable
  • getMessage(), printStackTrace() are two method in it
  • New exception usually extends Exception or one of its checked sub-classes, there by making the new exception as checked exception.
  • after a catch block is executed, control is transferred to finally block as long as one is present, regardless of whether the catch block itself throws an exception.
  • compiler complains if a super class shadows sub class exception catch block.
  • say, if finally block throes an exception, the new exception overrules any previous exception
  • return or break from finally block will determine further control flow, irrespective of how try block or any catch block were executed… it will
  • if no catch block handle the exception, exception are handled by default exception handler.
throw
  • a program can throw an exception using “throw” statement
  • throw <object of type Throwable class>, when<> null throw null pointer
throws
  • checked exception thrown by method are part of throws clause in method prototype
  • sub class can be thrown once supper class is in throws clause (bad prog style).
  • method overriding can specify a subset of checked exception in throws clause, because client may be ill prepared for newly added checked exception.
Inharitence
  • Private, override, hidden members are not inherited (they cant be accessed in base class without supper keyword)
  • constructor & initializer blocks are not member of a class, they are not inherited
  • Down-casting require type-casting.
Overriding Method within classes or interfaces
  • non static method only
  • same signature: method name, parameter list in order & same return type
  • parameter being final is at discretion of sub class.
  • Accessibility can not be narrow
  • throws can not be widen
  • static method can not be overridden in sub class – compiler error, however can be hidden
  • final method can not be overridden – compiler error
  • private method can not be overridden, however can be as exactly same prototype in sub class.
  • all above no override cases, early binding is done!
Field Hiding
  • sub class can not override field, it can hide field
  • use super to access super class fields including the ones hidden in sub class
  • static fields of supper class can also be hidden
  • only name has to be same to be hidden, rest all is ok!
supper
  • unlike this, it can not cast to other references or cast to other references type.
  • super.super .. not allowed
  • typecasting the “this” only changes its type, but its reference remains the same.
Constructor
  • chaining by this(), this() being the first statement in constructor.
  • super() constructor call must be the fist statement in sub class constructor
  • super() and this() can not be used together in a constructor.
  • if no explicit call to super(), default super() call is inserted implicitly.
  • in such case, if super dont have default constructor, compiler error occur.
Interface
  • sub interface extends base interface
  • all member variable are public static final and hence initialization is must.
  • all method/class/interface declaration are public and abstract.
  • class implementing interface method, have to keep method as public
  • class can not narrow access and can not broaden throws exception
Extending Interface
  • One interface can extend several interfaces (supper interfaces)
Subclass
  • one nested interface
  1. static member interfaces
  • Four categories of nested classes
  1. static member class
  2. non static member class
  3. local classes
  4. anonymous classes
  • last three classes are called inner classes.
  • instance of an inner class may be associated with (iei) immediately enclosing instance.
  • instance of an inner class can also access members of iei by there simple name.
  • static member class can be instantiated as any top level class using its full name.
  • no iei is required.
  • interface are only at top level or static member, nothing else!
  • instance of a non static member can be defined like other member of a class, such class always have iei instance associated with it.
  • local classes are defined just as local member variable.
  • Anonymous class can be defined as expressions and instantiated on the fly.
  • A nested class can not have the same name as any of its enclosing classes.
  • Top level class can only be public or default.
Garbage Collection
  • Any object not accessible by a live thread is candidate for GC.
  • gc can be facilitated by nullifying the reference.
  • Program can request garbage collection, but no way it can be forced. GC call is not guaranteed so is finalizer call.
  • finalizer:- from the Object class, protected void finalize() throws Throable{super.finalize();} get called before the object get gc.
  • System.gc(); -> request GC
  • System.runFunalizer(); -> request to run any pending
Access Specifiers:-
Modifier    | Class | Package | Subclass | World

public      |  Y    |    Y    |    Y     |   Y

protected   |  Y    |    Y    |    Y     |   N

no modifier |  Y    |    Y    |    N     |   N

private     |  Y    |    N    |    N     |   N

Difference between equals and ==
  • equals compare value
  • == compares reference

 Equals and Hashcode

  • if two objects are equal, they must have same hashcode.
  • opp. is not necessarly true.

public boolean equals(Object obj)

public int hashCode()

  •  this method is supported for the benifit of the hash based collection classes

http://www.technofundo.com/tech/java/equalhash.html

Categories: J2SE
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.