- /*
- Java byte Example
- This Java Example shows how to declare and use Java primitive byte variable
- inside a java class.
- */
- public class JavaByteExample {
- public static void main(String[] args) {
- /*
- * byte is smallest Java integer type.
- * byte is 8 bit signed type ranges from –128 to 127.
- * byte is mostly used when dealing with raw data like reading a
- * binary file.
- *
- * Declare byte varibale as below
- *
- * byte
= ; - *
- * here assigning default value is optional.
- */
- byte b1 = 100;
- byte b2 = 20;
- System.out.println("Value of byte variable b1 is :" + b1);
- System.out.println("Value of byte variable b1 is :" + b2);
- }
- }
- /*
- Output would be
- Value of byte variable b1 is :100
- Value of byte variable b1 is :20
- */
Saturday, February 12, 2011
Java byte Example
Java boolean Example
- /*
- Java boolean Example
- This Java Example shows how to declare and use Java primitive boolean variable
- inside a java class.
- */
- public class JavaBooleanExample {
- public static void main(String[] args) {
- /*
- * boolean is simple Java type which can have only of two values; true or false.
- * All rational expressions retrun this type of value.
- *
- * Declare boolean varibale as below
- *
- * boolean
= ; - *
- * here assigning default value is optional.
- */
- boolean b1 = true;
- boolean b2 = false;
- boolean b3 = (10 > 2)? true:false;
- System.out.println("Value of boolean variable b1 is :" + b1);
- System.out.println("Value of boolean variable b2 is :" + b2);
- System.out.println("Value of boolean variable b3 is :" + b3);
- }
- }
- /*
- Output would be
- Value of boolean variable b1 is :true
- Value of boolean variable b2 is :false
- Value of boolean variable b3 is :true
Basic Java Programs
Calculate Circle Area
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class CalculateCircleAreaExample {
- public static void main(String[] args) {
- int radius = 0;
- System.out.println("Please enter radius of a circle");
- try
- {
- //get the radius from console
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- radius = Integer.parseInt(br.readLine());
- }
- //if invalid value was entered
- catch(NumberFormatException ne)
- {
- System.out.println("Invalid radius value" + ne);
- System.exit(0);
- }
- catch(IOException ioe)
- {
- System.out.println("IO Error :" + ioe);
- System.exit(0);
- }
- /*
- * Area of a circle is
- * pi * r * r
- * where r is a radius of a circle.
- */
- //NOTE : use Math.PI constant to get value of pi
- double area = Math.PI * radius * radius;
- System.out.println("Area of a circle is " + area);
- }
- }
- /*
- Output of Calculate Circle Area using Java Example would be
- Please enter radius of a circle
- 19
- Area of a circle is 1134.1149479459152
- */
Calculate Circle Perimeter
- /*
- Calculate Circle Perimeter using Java Example
- This Calculate Circle Perimeter using Java Example shows how to calculate
- Perimeter of circle using it's radius.
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class CalculateCirclePerimeterExample {
- public static void main(String[] args) {
- int radius = 0;
- System.out.println("Please enter radius of a circle");
- try
- {
- //get the radius from console
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- radius = Integer.parseInt(br.readLine());
- }
- //if invalid value was entered
- catch(NumberFormatException ne)
- {
- System.out.println("Invalid radius value" + ne);
- System.exit(0);
- }
- catch(IOException ioe)
- {
- System.out.println("IO Error :" + ioe);
- System.exit(0);
- }
- /*
- * Perimeter of a circle is
- * 2 * pi * r
- * where r is a radius of a circle.
- */
- //NOTE : use Math.PI constant to get value of pi
- double perimeter = 2 * Math.PI * radius;
- System.out.println("Perimeter of a circle is " + perimeter);
- }
- }
- /*
- Output of Calculate Circle Perimeter using Java Example would be
- Please enter radius of a circle
- 19
- Perimeter of a circle is 119.38052083641213
- */
Subscribe to:
Posts (Atom)
