Posts

3 Mantras of Object typecasting

  A b = (c) d; Mantra -1 (At Compile Time) : the type of c and d must have some relationship (either parent to child or child to parent or same type), otherwise we will get a compile-time error.  Mantra -2 (At Compile Time): the type of c must be the same or derived type of A, otherwise we will get a compile-time error.  Mantra -3 (At RunTime): the underlying original object type of d must be either the same or derived type of c, otherwise we will runtime exception saying class cast exception.

Type Casting in java

Image
Implicit conversion : The compiler is responsible for Implicit type cast conversion Whenever we need to cast a smaller data type value to a bigger data type variable then implicit conversion is used. Implicit typecast is also know as widening or upcasting. Implicit type casting no loss of information Example  1: int a = 'a'; System.out.prinitln(a);  // 97 In the above Example compiler converts char to int automatically by implicit typecasting. Example 2: double d = 10; System.out.println(d);  // 10.0 In the above Example compiler converts int values to double automatically by implicit typecasting. Below are possible implicit typecasting : Explicit conversion : The programmer is responsible for Explicit type cast conversion Whenever we need to cast a Bigger data type value to a smaller data type variable then Explicit conversion is used. Explicit typecast is also know as narrowing or downcasting. in the Explicit type casting loss of information Example...

Primitive Data Types in java

Image
Primitive Data Types   Expect boolean and char remaining data types are consider assigned data types because we can represent both positive and negative numbers. byte : size : 1 byte  ( 8 bit) max value : 127 min value: 128 Range: -128 to 127 a byte is the best choice,  if we want to handle data in terms of streams either from file or form the network. syntax : byte b = 10; Default value : 0 short: size: 2 bytes (16 bit) Range: - 2 power 15 to 2 power 15 -1 the short data type is best suitable for 16-bit processors like 8085 but these processors are completed outdated and hence corresponding data type is also outdated datatype. syntax : short b = 10; Default value : 0 int : size: 4 bytes (32 bit) Range: -2 power 31 to 2 power 31 -1 syntax :  int b = 10; Default value : 0 long : size: 8 bytes (64 bit) Range : - 2 power 63 to 2 power 63 -1  syntax : long b = 1051541; Default value : 0 All the above data types (byte, short, int, long ) use to represent integral val...

Why Java is not a purely Object-Oriented Language?

  Why Java is not a purely Object-Oriented Language? java is not considered as a pure object-oriented programming language because Servel oop features are not supported by java like operator overloading, multiple inheritances, etc. moreover, we are depending on Primitive datatypes which are non-objects.

Why Java is a strongly typed programming language?

Why Java is a strongly typed programming language?   In java, every variable and every expression has some type each and every data type is clearly defined  every assignment should be checked by the compiler for type Compatibility.  Because of the above reason Java is a strongly typed programming language.

Var-arg methods (Variable number of arguments methods)

 Until the 1.4 version, we cant declared a method with a variable number of arguments if there is a change in the number of arguments composure we should go for a new method it increases the length of the code and reduces readability to overcome this problem SUN people introduces var-args methods in 1.5 version. according to this, we can declare a method that can take a variable number of arguments such type of methods is called the var-arg methods. We can declare methods like sum(int... a){ } We can call this method by passing any number of int values including the 0 number. sum(10); sum(10,20); sum(10,20,30) sum(10,20,30,40); Internally var-arg parameters will be converted into a one-dimensional array hence within the var-arg method by using an index. if we mixed normal parameter with var-arg parameter then var-arg parameter last parameter m1(int a,int... y)

What happened internally when we execute class file java

 When we execute the class file using the "java <class file >" command internally below operations is performed. Start JVM Create and Start the main thread: JVM will create a main thread and start that thread. Locate the class file Load the class file Execute the main method Unload the class terminated main thread Shutdown JVM