Posts

Java 8 features

 Java 8 features: Lambda Expression Functional Interface Default method and static method Predefined functional interface Predicate, Function, Consumer, Supplier Double colon (::) Operator: Method Reference, Constructor Reference Streams Date and time API Operational Class Nashorn Javascript Engine

Lambda Expression

 Lambda Expression: Lambda Expression First introduced in LISP Programming Language in 1930 lambda Expression already in Python,LISP,C,C++,Ruby,Scala. java strongly supports OOP but java does not support functional programming. brings benefits of functional programming in java they introduced Lambda Expression. Lambda Expression is an anonymous function. i.e function without a name, return type, and without modifiers called an anonymous function. Normal Code: public class Test { public static int squar(int a) { return a*a; } public static void main(String[] args) { System.out.println("Squar of 4 : " + squar(4)); System.out.println("Squar of 6 : " + squar(6)); } } Lambda Expression :   public class Test { public static void main(String[] args) { java.util.function.Function<Integer,Integer> f = i->i*i; System.out.println("Squar of 4 : " + f.apply(4)); System.out.println("Squar of 6 : " + f.apply(6)); } } Rules...

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.