Saturday, February 12, 2011

Java byte Example


  1. /*
  2. Java byte Example
  3. This Java Example shows how to declare and use Java primitive byte variable
  4. inside a java class.
  5. */
  6.  
  7. public class JavaByteExample {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. /*
  12. * byte is smallest Java integer type.
  13. * byte is 8 bit signed type ranges from –128 to 127.
  14. * byte is mostly used when dealing with raw data like reading a
  15. * binary file.
  16. *
  17. * Declare byte varibale as below
  18. *
  19. * byte = ;
  20. *
  21. * here assigning default value is optional.
  22. */
  23.  
  24. byte b1 = 100;
  25. byte b2 = 20;
  26.  
  27. System.out.println("Value of byte variable b1 is :" + b1);
  28. System.out.println("Value of byte variable b1 is :" + b2);
  29. }
  30. }
  31.  
  32. /*
  33. Output would be
  34. Value of byte variable b1 is :100
  35. Value of byte variable b1 is :20
  36. */

Java boolean Example


  1. /*
  2. Java boolean Example
  3. This Java Example shows how to declare and use Java primitive boolean variable
  4. inside a java class.
  5. */
  6.  
  7. public class JavaBooleanExample {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. /*
  12. * boolean is simple Java type which can have only of two values; true or false.
  13. * All rational expressions retrun this type of value.
  14. *
  15. * Declare boolean varibale as below
  16. *
  17. * boolean = ;
  18. *
  19. * here assigning default value is optional.
  20. */
  21.  
  22. boolean b1 = true;
  23. boolean b2 = false;
  24. boolean b3 = (10 > 2)? true:false;
  25.  
  26. System.out.println("Value of boolean variable b1 is :" + b1);
  27. System.out.println("Value of boolean variable b2 is :" + b2);
  28. System.out.println("Value of boolean variable b3 is :" + b3);
  29. }
  30. }
  31.  
  32. /*
  33. Output would be
  34. Value of boolean variable b1 is :true
  35. Value of boolean variable b2 is :false
  36. Value of boolean variable b3 is :true

Basic Java Programs


Calculate Circle Area


  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. public class CalculateCircleAreaExample {
  5. public static void main(String[] args) {
  6. int radius = 0;
  7. System.out.println("Please enter radius of a circle");
  8. try
  9. {
  10. //get the radius from console
  11. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  12. radius = Integer.parseInt(br.readLine());
  13. }
  14. //if invalid value was entered
  15. catch(NumberFormatException ne)
  16. {
  17. System.out.println("Invalid radius value" + ne);
  18. System.exit(0);
  19. }
  20. catch(IOException ioe)
  21. {
  22. System.out.println("IO Error :" + ioe);
  23. System.exit(0);
  24. }
  25. /*
  26. * Area of a circle is
  27. * pi * r * r
  28. * where r is a radius of a circle.
  29. */
  30. //NOTE : use Math.PI constant to get value of pi
  31. double area = Math.PI * radius * radius;
  32. System.out.println("Area of a circle is " + area);
  33. }
  34. }
  35. /*
  36. Output of Calculate Circle Area using Java Example would be
  37. Please enter radius of a circle
  38. 19
  39. Area of a circle is 1134.1149479459152
  40. */

Calculate Circle Perimeter



  1. /*
  2. Calculate Circle Perimeter using Java Example
  3. This Calculate Circle Perimeter using Java Example shows how to calculate
  4. Perimeter of circle using it's radius.
  5. */
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. public class CalculateCirclePerimeterExample {
  10. public static void main(String[] args) {
  11. int radius = 0;
  12. System.out.println("Please enter radius of a circle");
  13. try
  14. {
  15. //get the radius from console
  16. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  17. radius = Integer.parseInt(br.readLine());
  18. }
  19. //if invalid value was entered
  20. catch(NumberFormatException ne)
  21. {
  22. System.out.println("Invalid radius value" + ne);
  23. System.exit(0);
  24. }
  25. catch(IOException ioe)
  26. {
  27. System.out.println("IO Error :" + ioe);
  28. System.exit(0);
  29. }
  30. /*
  31. * Perimeter of a circle is
  32. * 2 * pi * r
  33. * where r is a radius of a circle.
  34. */
  35. //NOTE : use Math.PI constant to get value of pi
  36. double perimeter = 2 * Math.PI * radius;
  37. System.out.println("Perimeter of a circle is " + perimeter);
  38. }
  39. }
  40. /*
  41. Output of Calculate Circle Perimeter using Java Example would be
  42. Please enter radius of a circle
  43. 19
  44. Perimeter of a circle is 119.38052083641213
  45. */