Java Essentials

final keyword

We can use this keyword with variable, method and class. With a variable, when we want that variable stays constant and its value doesn’t change. With method, when we want that the method doesn’t override. With class, when we want that class doesn’t get inherited.

Exception handling in Java

An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e. at run time, that disrupts the normal flow of the program.

Points

  • There is no sizeof operator in Java.

  • + operator precedence is more than == operator.

  • Operator overloading is not performed in Java.

  • Calling a non-static function inside a static function is not allowed.

  • Parameters are passed by value in Java.

  • Objects are not passed in Java, only references are passed.

  • Unlike C++, we can’t have inline functions in Java.

  • Java doesn’t support default arguments in functions.

  • delete(x,y) deletes the elements from the string at position ‘x’ to position ‘y-1’.

  • str.toUpperCase() returns str in uppercase but does not change the original string ‘str’.

  • str.substring(x, y) returns string from position ‘x’ (inclusive) to position ‘y’ (exclusive).

  • Unlike C++, in Java all non primitive objects must be explicitly allocated and these objects are allocated on heap.

  • In Java, fields of classes and objects that do not have an explicit initializer and elements of arrays are automatically initialized with the default value for their type (false for boolean, 0 for all numerical types, null for all reference types). Local variables in Java must be definitely assigned before they are accessed, or it is a compile error.

  • A method written with the final keyword, cannot be hidden or overridden by subclasses.

  • If there is a static block it gets executed first, then instance method, then constructor method and then other methods.

  • *Can we implement and extend both an interface and a class simultaneously? → Yes (cue: Parallel Interface, Parallel Inheritance)

  • You cannot create an object of Abstract class.

Last updated