0% found this document useful (0 votes)
13 views

Base 64 Encoding Decoding-DefaultMethods

Uploaded by

vishal46k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Base 64 Encoding Decoding-DefaultMethods

Uploaded by

vishal46k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Basic Type Base64 Encoding and Decoding

Base 64 is an encoding scheme that converts binary data into text format so that encoded textual
data can be easily transported over network un-corrupted and without any data loss.

The Basic encoding means no line feeds are added to the output and the output is mapped to a set
of characters in A-Za-z0-9+/ character set and the decoder rejects any character outside of this set.

Encode simple String into Basic Base 64 format

String BasicBase64format= Base64.getEncoder().encodeToString(“actualString”.getBytes());

Decode Basic Base 64 format to String

byte[] actualByte= Base64.getDecoder().decode(encodedString);


String actualString= new String(actualByte);

import java.u l.*;


public class GFG {
public sta c void main(String[] args)
{
String sample = "India Team will win the Cup";
System.out.println("Sample String:\n"+ sample);
String BasicBase64format = Base64.getEncoder().encodeToString(sample.getBytes());
System.out.println("Encoded String:\n"+ BasicBase64format);
}
}
Sample String:
India Team will win the Cup
Encoded String:
SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw
import java.u l.*;
public class GFG {
public sta c void main(String[] args)
{
String encoded = "SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw";
System.out.println("Encoded String:\n"+ encoded);
byte[] actualByte = Base64.getDecoder().decode(encoded);
String actualString = new String(actualByte);
System.out.println("actual String:\n"+ actualString);
}
}
Encoded String:
SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw
actual String:
India Team will win the Cup
Default Methods
Before Java 8, interfaces could have only abstract methods. The implementa on of these methods
has to be provided in a separate class. So, if a new method is to be added in an interface, then its
implementa on code has to be provided in the class implemen ng the same interface. To overcome
this issue, Java 8 has introduced the concept of default methods which allow the interfaces to have
methods with implementa on without affec ng the classes that implement the interface.
interface TestInterface
{
public void square(int a);
default void show()
{
System.out.println("Default Method Executed");
}
}
class TestClass implements TestInterface
{
public void square(int a)
{
System.out.println(a*a);
}
public sta c void main(String args[])
{
TestClass d = new TestClass();
d.square(4);
d.show();
}
}
16
Default Method Executed

The default methods were introduced to provide backward compa bility so that exis ng interfaces
can use the lambda expressions without implemen ng the methods in the implementa on class.
Sta c Methods:
The interfaces can have sta c methods as well which is similar to sta c method of classes.
interface TestInterface
{
public void square (int a);
sta c void show()
{
System.out.println("Sta c Method Executed");
}
}
class TestClass implements TestInterface
{
public void square (int a)
{
System.out.println(a*a);
}
public sta c void main(String args[])
{
TestClass d = new TestClass();
d.square(4);
TestInterface.show();
}
}
16
Sta c Method Executed

Default Methods and Mul ple Inheritance


interface TestInterface1
{
default void show()
{
System.out.println("Default TestInterface1");
}
}

interface TestInterface2
{
default void show()
{
System.out.println("Default TestInterface2");
}
}
class TestClass implements TestInterface1, TestInterface2
{
public void show()
{
TestInterface1.super.show();
TestInterface2.super.show();
}
public sta c void main(String args[])
{
TestClass d = new TestClass();
d.show();
}
Default TestInterface1
Default TestInterface2

You might also like