Base64 is encoding scheme that can represent binary data in ASCII string format. This is useful whenever arbitrary data needs to be passed across systems as textual data. This ensures that binary data which may have special meaning to the transport layer remains intact during transport. Encoding and storing image binary data as an XML text node is a typical usage scenario.
Original Text: This is a test string Encoded String: VGhpcyBpcyBhIHRlc3Qgc3RyaW5n
The following Java program generates a base64 encoded string for an input string. This example uses sun.misc.BASE which is part of the Java SDK but is not officially supported by Oracle.
Base64 Encoding Example in Java
// Base64 encoding in Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import sun.misc.BASE64Encoder;
public class Base64Encoder {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter a string to encode : ");
String source = reader.readLine();
BASE64Encoder encoder = new BASE64Encoder();
System.out.println("Encoded string is : "+encoder.encode(source.getBytes()));
}
}
Another option is to use the commons codec library which I would recommend for production systems.
Posted in Java | Comments Off on Base64 String Encoding in Java
The following Java program finds the largest number in a given array. Initially the largest variable value is set as the smallest integer value (Integer.MIN_VALUE) and whenever a bigger number is found in array, it is overwritten with the new value. We iterate through the entire array with the above logic to find the largest number.
Print the Largest Number in an Array Using Java
// Java program to find largest number in an array
public class FindLargest {
public static void main(String[] args) {
int[] numbers = {88,33,55,23,64,123};
int largest = Integer.MIN_VALUE;
for(int i =0;i<numbers.length;i++) {
if(numbers[i] > largest) {
largest = numbers[i];
}
}
System.out.println("Largest number in array is : " +largest);
}
}
Posted in Java | Comments Off on Find Largest Number in an Array Using Java
The following Java program prints all alphabets in English language. This program uses the Java character variable and built-in ordering of the character variable values. The following program prints both capital letters and small letters.
Write a Java Program to Print All English Alphabets
// Java program to print all english alphabets
public class AllAlphabets {
public static void main(String[] args) {
System.out.print("Capital alphabets in english are : ");
for(char c='A'; c<='Z';c++) {
System.out.print(c+",");
}
System.out.println();
System.out.print("Small alphabets in english are : ");
for(char c='a'; c<='z';c++) {
System.out.print(c+",");
}
System.out.println();
}
}
Posted in Java | Comments Off on Java Program to Print All Alphabets
The formula for finding area of a circle is,
area = PI * radius * radius
Where PI is a constant (approximately 3.1416) and radius is the radius of the circle.
Calculate Area of a Circle in Java
The following Java program calculates the area of a circle given the radius of the circle. This program demonstrates a number of key concepts in Java programming,
- How to obtain user input from console
- How to write output to console
- How to use IO classes and decorator pattern
- Using library classes such as Math, BufferedReader, Double etc.
- Performing mathematical calculations
// Java program to calculate area of a circle
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CircleArea {
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter the radius of the circle : ");
double radius = Double.parseDouble(reader.readLine());
double area = Math.PI * radius * radius;
System.out.println("Area of the circle with radius "+radius+" is : "+area);
}
}
Posted in Java | Comments Off on Java Program to Find Area of a Circle
The formula for finding the area of a rectangle is,
area = length * width
Calculate Area of a Rectangle in Java
The following program calculates area of a rectangle in Java. The program asks the user for length and width values and then outputs the result in the console. We use BufferedReader to read a line of string and then use the parseDouble method of the Double class to convert the string to a number.
This program also illustrates the decorator pattern. We first decorate the console input stream (System.in) with a InputStreamReader and then again decorate it with BufferedReader. BufferedReader enables us to read a line of data from an input stream.
// Java program to find area of a rectangle
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RectangleArea {
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter length of the rectangle : ");
double length = Double.parseDouble(reader.readLine());
System.out.print("Please enter width of the rectangle : ");
double width = Double.parseDouble(reader.readLine());
System.out.println("Area of the rectangle : "+length*width);
}
}
Posted in Java | Comments Off on Find Area of a Rectangle in Java