Ternary Operator in Java

In Java, the ternary operator is a type of Java conditional operator. In this section, we will discuss the ternary operator in Java with proper examples.

The meaning of ternary is composed of three parts. The ternary operator (? 🙂 consists of three operands. It is used to evaluate Boolean expressions. The operator decides which value will be assigned to the variable. It is the only conditional operator that accepts three operands. It can be used instead of the if-else statement. It makes the code much more easy, readable, and shorter.

Example.

Ternary Operator in Java

Syntax: 

variable = Expression1 ? Expression2: Expression3

Flowchart

   
      
Ternary Operator Java

Example,

class Ternary {

    public static void main(String[] args)

    {

        // variable declaration

        int n1 = 5, n2 = 10, max;

        System.out.println("First num: "+ n1);

        System.out.println("Second num: "+ n2);

        // Largest among n1 and n2

        max = (n1 > n2) ? n1 : n2;

        // Print the largest number

        System.out.println("Maximum is = "+ max);

    }

}

Reference.

https://www.geeksforgeeks.org/java-ternary-operator-with-examples/

https://www.javatpoint.com/ternary-operator-in-java

https://www.programiz.com/java-programming/ternary-operator

Multilevel Inheritance in Java

The multi-level inheritance includes the involvement of at least two or more than two classes. One class inherits the features from a parent class and the newly created sub-class becomes the base class for another new class.

When a class extends a class, which extends anther class then this is called multilevel inheritance. For example class C extends class B and class B extends class A then this type of inheritance is known as multilevel inheritance.

Multilevel Inheritance

It’s pretty clear with the diagram that in Multilevel inheritance there is a concept of grand parent class. If we take the example of this diagram, then class C inherits class B and class B inherits class A which means B is a parent class of C and A is a parent class of B. So in this case class C is implicitly inheriting the properties and methods of class A along with class B that’s what is called multilevel inheritance.

Example.

class Shape {
   void display() {
      System.out.println("Inside display");
   }
}
class Rectangle extends Shape {
 void area() {
      System.out.println("Inside area");
   }
}
class Cube extends Rectangle {
   void volume() {
      System.out.println("Inside volume");
   }
}
   public static void main(String[] arguments) {
      Cube cube = new Cube();
      cube.display();
      cube.area();
      cube.volume();
   }
}

Reference.

https://www.tutorialspoint.com/Multilevel-inheritance-in-Java

https://beginnersbook.com/2013/12/multilevel-inheritance-in-java-with-example/

https://www.codingninjas.com/codestudio/library/multilevel-inheritance-in-java

Top Five Inspiration People in Tech

RICHARD STALLMAN

Image result for Richard Stallman

Stallman Achievements:

1. The person who creat the GNU project and give a clarification for peoples     to know their freedom in software world.
 
2. Founded the Free Software Foundation (FSF) in October 1985.

3. Stallman pioneered the concept of copyleft, which uses the principles of copyright law to preserve the right to use and the author of free software license. 

4. Most Notable thing is the GNU General Public License (GPL), the most widely used free software license.

Linus Torvalds

1. Torvalds was born in Helsinki, Finland.

2. The first Linux prototypes were publicly released in late 1991.

3. when another Swedish-speaking computer science student, Lars Wirzenius, took him to the University of Technology to listen to free software guru Richard Stallman's speech.

4. Torvalds used Stallman's GNU General Public License version 2 (GPLv2) for his Linux kernel. 

5. Although Torvalds believes "open source is the only right way to do software", he also has said that he uses the "best tool for the job", even if that includes proprietary software.

6. The Linux Foundation currently sponsors Torvalds so he can work full-time on improving Linux.

Muthu Annamalai

1. He is the person who invent the ezhil programming language and its fully open source language.

2. Using Ezhil you can run a computer using instructions written in Tamil script.

3. Ezhil includes both Tamil and English identifiers.

4. Ezhil is intended to be useful for Tamil-speaking students who donot learn English early, but wish to learn programming for first time. Ezhil is released under GNU GPL license. 

5. The reason he creat the ezhil is if a person want learn the progmming he know about english because the keyword of those languages  are in english.

6. After ezhil the college and school studnet learn programming via tamil language.
                                                            

Siva Ayyadurai

1. He is an Indian-American engineer.

2. He has become known for promoting conspiracy theories, pseudoscience and unfounded medical claims.

3. Ayyadurai claimed he invented email, as a teenager; in August 1982 he registered the copyright on an email application he had written.

4. Ayyadurai sued Gawker Media and Techdirt for defamation for disputing his account of inventing email. Both lawsuits were settled out of court.

5. Ayyadurai and Techdirt agreed to Techdirt's articles remaining online with a link to Ayyadurai's rebuttal on his own website.

6. He is also submited the paper for Genetically modified food.

Dennis Ritchie

1. He is an american computer scientist.

2. Inventor of famous C programming language along with his colleage Ken  Thomson both or working in Bell Labs.

3.  Ritchie began working at the Bell Labs Computing Sciences Research Center, and in 1968.

4. During the 1960s, Ritchie and Ken Thompson worked on the Multics operating system at Bell Labs.

5. Thompson then found an old PDP-7 machine and developed his own application programs and operating system from scratch, aided by Ritchie and others.

6. In 1970, Brian Kernighan suggested the name "Unix", a pun on the name "Multics". To supplement assembly language with a system-level programming language, Thompson created B. Later, B was replaced by C.

INTERFACE

Another way to achieve abstarction in Java, is with interfaces.

What is interface?

Interfaces form a contract(Set of rules) between the class and the outside world, and this contract is enforced at build time by the compiler.

An interface(completely “abstract class“) is a group of related methods with empty bodies.

To implements this interface, the name of your class would change , and you’d use the implements keyword in the class declaration.

Notes on interface:

  • Like abstract classes, interfaces cannot be used to create objects.So, it cannot contain a constructor .
  • Interface methods do not have a body – the body is provided by the “implement” class.
  • On implementation of an interface, you must override all of its methods.
  • Interface methods are by default abstract and public.
  • Interface attributes are by default public, static and final.
  • Why And When To Use Interfaces?
  • 1) To achieve security – hide certain details and only show the important details of an object (interface).
  • 2) Java does not support “multiple inheritance” (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces.

Java Interface Example

In this example, the Printable interface has only one method, and its implementation is provided in the A6 class.

  1. interface printable{  
  2. void print();  
  3. }  
  4. class A6 implements printable{  
  5.  void print()
  6. {
  7. System.out.println(“Hello”);
  8. }    
  9. public static void main(String args[]){  
  10. A6 obj = new A6();  
  11. obj.print();  
  12.  }  
  13. }  

Reference,

https://www.w3schools.com/java/tryjava.asp

https://www.geeksforgeeks.org/interfaces-in-java/

https://www.javatpoint.com/interface-in-java

Constructors in Java

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called.

It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.

Example

Create a constructor:


 class Main {
  int x;  
   Main() {
    x = 5;  
  }

  public static void main(String[] args) {
    Main myObj = new Main();
    System.out.println(myObj.x); 
  }
}

Different from Constructors and Methods in Java? 

  • Constructors must have the same name as the class within which it is defined while it is not necessary for the method in Java.
  • Constructors do not return any type while method(s) have the return type or void if does not return any value.
  • Constructors are called only once at the time of Object creation while method(s) can be called any number of times.

The rules for writing constructors are as follows.

  • Constructor(s) of a class must have the same name as the class name in which it resides.
  • A constructor in Java can not be abstract, final, static, or Synchronized.
  • Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor.

Types of Constructor

In Java, constructors can be divided into 3 types:

Non -Parameterized Constructor

Parameterized Constructor

Default Constructor

Non -Parameterized Constructor

Similar to methods, a Java constructor may or may not have any parameters (arguments).

If a constructor does not accept any parameters, it is known as a no-argument constructor. For example,

class Main {

int i;

// constructor with no parameter
Main() {
i = 5;
System.out.println(“Constructor is called”);
}

public static void main(String[] args) {

// calling the constructor without any parameter
Main obj = new Main();
System.out.println("Value of i: " + obj.i);

}
}

Parameterized Constructor

A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors (constructor with parameters).

Example : Parameterized constructor

class Main {

  String languages;

  Main(String lang) {
    languages = lang;
    System.out.println(languages + " Programming Language");
  }

  public static void main(String[] args) {

    Main obj1 = new Main("Java");
    Main obj2 = new Main("Python");
    Main obj3 = new Main("C");
  }
}

Default Constructor

If we do not create any constructor, the Java compiler automatically create a no-arg constructor during the execution of the program. This constructor is called default constructor.

Example : Default Constructor

class Main {

  int a;
  boolean b;

  public static void main(String[] args) {

    // A default constructor is called
    Main obj = new Main();

    System.out.println("Default Value:");
    System.out.println("a = " + obj.a);
    System.out.println("b = " + obj.b);
  }
}

Super() 

Whenever a child class constructor gets/is invoked, it implicitly invokes the constructor of the parent class. You can say that the compiler inserts a super(); statement at the beginning of the child class constructor.

Constructor overloading in Java

In Java, we can overload constructors like methods. The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task.

class Demo{
      int  value1;
      int  value2;
      /*Demo(){
       value1 = 10;
       value2 = 20;
       System.out.println("Inside 1st Constructor");
     }*/
     Demo(int a){
      value1 = a;
      System.out.println("Inside 2nd Constructor");
    }
    Demo(int a,int b){
    value1 = a;
    value2 = b;
    System.out.println("Inside 3rd Constructor");
   }
   public void display(){
      System.out.println("Value1 === "+value1);
      System.out.println("Value2 === "+value2);
  }
  public static void main(String args[]){
    Demo d1 = new Demo();
    Demo d2 = new Demo(30);
    Demo d3 = new Demo(30,40);
    d1.display();
    d2.display();
    d3.display();
 }
}

Reference

https://www.javatpoint.com/java-constructor

https://www.programiz.com/java-programming/constructors

https://www.geeksforgeeks.org/constructors-in-java/

https://beginnersbook.com/2013/03/constructors-in-java/

Recursion

Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.

Recursion may be a bit difficult to understand. The best way to figure out how it works is to experiment with it.

A condition must be specified to stop recursion; otherwise it will lead to an infinite process.

In case of recursion, all partial solutions are combined to obtain the final solution.

Advantages:

i. The main benefit of a recursive approach to algorithm design is that it allows programmers to take advantage of the repetitive structure present in many problems.

ii. Complex case analysis and nested loops can be avoided.

iii. Recursion can lead to more readable and efficient algorithm descriptions.

iv. Recursion is also a useful way for defining objects that have a repeated similar structural form.

 Disadvantages:

i. Slowing down execution time and storing on the run-time stack more things than required in a non recursive approach are major limitations of recursion.

ii. If recursion is too deep, then there is a danger of running out of space on the stack and ultimately program crashes.

iii. Even if some recursive function repeats the computations for some parameters, the run time can be prohibitively long even for very simple cases.

Example,

public class Main {
  public static void main(String[] args) {
    int result = sum(10);
    System.out.println(result);
  }
  public static int sum(int k) {
    if (k > 0) {
      return k + sum(k - 1);
    } else {
      return 0;
    }
  }
}

Reference.

https://www.w3schools.com/java/tryjava.asp?

https://www.javatpoint.com/recursion-in-java

Method Overriding

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.

In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding

  • Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
  • Method overriding is used for runtime polymorphism

Rules for Java Method Overriding

  1. The method must have the same name as in the parent class
  2. The method must have the same parameter as in the parent class.
  3. There must be an IS-A relationship (inheritance).
  1. //Creating a parent class  
  2. class Vehicle{  
  3.   void run(){System.out.println(“Vehicle is running”);}  
  4. }  
  5. //Creating a child class  
  6. class Bike extends Vehicle{  
  7.   public static void main(String args[]){  
  8.   //creating an instance of child class  
  9.   Bike obj = new Bike();  
  10.   //calling the method with child class instance  
  11.   obj.run();  
  12.   }  
  13. }  

Method Overloading

If a classhas multiple methods having same name but different in parameters, it is known as Method Overloading.

If we have to perform only one operation, having same name of the methods increases the readability of the program.

Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs.

So, we perform method overloading to figure out the program quickly.

Advantage of method overloading

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java

  1. By changing the data type
  2. By changing number of arguments

OOPS CONCEPT

Java – What is OOP?

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.

Object-oriented programming has several advantages over procedural programming:

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP helps to keep the Java code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
  • OOP makes it possible to create full reusable applications with less code and shorter development time

Java – What are Classes and Objects?

Classes and objects are the two main aspects of object-oriented programming.

Look at the following illustration to see the difference between class and objects:

class

Fruit

objects

Apple

Banana

Mango

class

Car

objects

Volvo

Audi

Toyota

So, a class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the variables and methods from the class.

Java Classes/Objects

Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.

A Class is like an object constructor, or a “blueprint” for creating objects.


Create a Class

To create a class, use the keyword class

Create a class named “Main” with a variable x:

public class Main {
  int x = 5;
}

Create an Object

In Java, an object is created from a class. We have already created the class named Main, so now we can use this to create objects.

To create an object of Main, specify the class name, followed by the object name, and use the keyword new:

Example

Create an object called “myObj” and print the value of x:

public class Main {
  int x = 5;

  public static void main(String[] args) {
    Main myObj = new Main();
    System.out.println(myObj.x);
  }
}

Multiple Objects

You can create multiple objects of one class:

Example

Create two objects of Main:

public class Main {
  int x = 5;

  public static void main(String[] args) {
    Main myObj1 = new Main();  // Object 1
    Main myObj2 = new Main();  // Object 2
    System.out.println(myObj1.x);
    System.out.println(myObj2.x);
  }
}

Java Break/continue

Java Break

The Java break statement is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop. We can use Java break statement in all types of loops such as for loop, while loop and do-while loop.

The break keyword is used to break out a for loop, a while loop or a switch block.

The break statement can also be used to jump out of a loop.

The break statement in Java terminates the loop immediately, and the control of the program moves to the next statement following the loop. It is almost always used with decision-making statements .

This example stops the loop when i is equal to 4:

Example

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    break;
  }
  System.out.println(i);
}


Java Continue

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.

This example skips the value of 4:

Example

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    continue;
  }
  System.out.println(i);
}

Design a site like this with WordPress.com
Get started