Categories: Keyword in Java

The default keyword in Java programming language

The default keyword in Java programming language

In this tutorial, we will discuss The default keyword in Java programming language

The default is used in two ways in Java.

The default is used to add a default ‘case’ in switch statements in Java

The default is used to add default methods in interfaces since Java 8.

The default is an optional part of switch statements which are used only if none of the above cases is matched

Default keyword in switch statements

Declaration

The syntax of the default keyword

default;

Program

class Best_color{
public static void main (String args[]){
int colour=3;
String print_colour;
switch (colour){
case 1:print_colour="Red";
break;
case 2:print_colour="Blue";
break;
case 3:print_colour="Green";
break;
case 4:print_colour="Yello";
break;
default ://default keyword 
print_colour="No selection";
break;
}
System.out.println(print_colour);
}
}

 

When the above code is compiled and executed, it will produce the following results

Green

default method in the interface

default keyword is used to create default method inside the interface

Declaration

The syntax of the default method in Java

default void method_name(){
//some code here
}

Example

Example of default method in Java

interface Getable{
//default method
default void getSalary(){ 
//some code here 
}
}

//default method declaration inside the interface in Java language

Program 1

interface DemoDefault{
void myMarks();
default void display()
{
System.out.println("This is the default method");

}
}

class DefaultKey implements DemoDefault{
    public void myMarks(){
        System.out.println("implementation of myMarks");
    }
}

public class DemoInterface{
    public static void main(String args[]){
        DemoDefault obj=new DefaultKey();
        obj.display();
        obj.myMarks();
    }
    
    
}

When the above code is compiled and executed, it will produce the following results

This is the default method

implementation of myMarks

 

There are other Java language keywords that are similar to this keyword

abstract keyword in Java

boolean Keyword in Java

break keyword in Java

Byte keyword in Java 

case keyword in Java 

catch keyword in Java 

char keyword in Java 

class keyword in Java 

continue keyword in Java 

default keyword in Java 

do keyword in Java

double keyword in Java

else keyword in Java

enum keyword in Java

extends keyword in Java

final keyword in Java

finally keyword in Java

float keyword in Java

for keyword in Java

if keyword in Java

static keyword in Java

super keyword in Java

 

Suggested for you

Usage of this statement in the Java language

Keywords in Java language

Keywords in C language

Keywords in Python language

The volatile keyword in java programming language
Usage of Static in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago