Jul 27

Java 7 new feature : Switch statements with strings

Number of View: 121

A switch statement is useful when you need to select one of several alternatives based on the value of an integer, a character, or a String variable.

The purpose of a switch statement is to provide a convenient and simple method of making multi-branch selections based upon integer, enumeration, or String expression Continue reading

Jul 05

Difference between Servlets and CGI

Number of View: 399

Advantages of Servlets over CGI

                CGI                   Servlets
1) In traditional CGI, a new process is started for each HTTP request. 1) In servlets, the Java virtual machine stays running and handles each request with a lightweight Java thread, not a heavyweight operating system process. Continue reading
Jun 14

How to add two BigDecimal values in java?

Number of View: 315

This program demonstrates how to add two big decimal numbers in java.

Method : public BigDecimal add(BigDecimal augend):

Number of arguments : 1

Return value : BigDecimal

import java.math.BigDecimal;

public class BigDecimalAddDemo {

public static void main(String[] args) {

BigDecimal bigDecimal3;

BigDecimal bigDecimal1=new BigDecimal(123.3256);

BigDecimal bigDecimal2=new BigDecimal(345.768);

bigDecimal3=bigDecimal1.add(bigDecimal2);

System.out.println(bigDecimal3);

}

}

Output:

469.0935999999999665988070773892104625701904296875

Jun 04

Write a java program to accept 3 numbers and print the max value

Number of View: 318

Write a program to accept 3 numbers and print the maximum value

This program demonstrates to find greatest of 3 numbers .

public class Greatest
{
public static void main(String[] args)
{
greatestNumber(3, 100, 1);

greatestNumber(111, 12, 5);
}

private static void greatestNumber(int num1, int num2, int num3) {

if(num1>num2)
{
if(num1>num3)
{
System.out.println("num1 is greater "+num1);
}
else
{
System.out.println("num3 is greater "+num3);
}
}
else
{
if(num2>num3)
{
System.out.println("num2 is greater "+num2);
}
else
{
System.out.println("num3 is greater "+num3);
}
}
}
}

Output

num2 is greater 100
num1 is greater 111