This tutorial contains basic programs like sum, average, circle and rectangle area etc in java. The tutorial also covers some key concepts about variables and data types, let's see the basic programs first followed by the key concepts.
The program below shows how to add two number and also shows how to find average of two number. Similarly you can develop your own program to add multiple numbers or find the average of multiple numbers.
Addition program in java | Average program in java
class
AdditionAndAverage {public static void
main(String [] args) {int
firstNum = 1234;int
secondNum = 5678;int
sum = firstNum + secondNum;// Adding two number
int
avg = (firstNum + secondNum)/2;// Calculating average of two number
System.out.println("sum = "
+sum); System.out.println("Average = "
+avg); } }
Output:
sum = 6912
Average = 3456
The program below shows how to multiply and divide two numbers in java. Be careful to use the correct data type for storing the result of these operations. For example if division
of two number results as a decimal(eg. 20.25, 245.3 etc) type value, then store it in any of the float(float
, double
) data types.
Multiplication program in java | Java division program
class
MultiplicationAndDivision {public static void
main(String [] args) {int
firstNum = 100;int
secondNum = 5;int
result = firstNum * secondNum;// Multiplying two number
int
result2 = firstNum / secondNum;// Dividing two number
double
result3 = (double
)200 / 6;// Division which returns decimal type value
System.out.println("multiplication = "
+result); System.out.println("Division = "
+result2); System.out.println("result3 = "
+result3); } }
Output:
multiplication = 500
Division = 20
result3 = 33.333333333333336
Program below shows how to find area of a circle for given radius and area of rectangle for given length and width.
Area of circle java program | Area of rectangle java program
class
CalculateArea {public static void
main(String [] args) {float
radius = 10.5f;float
pi = 22/7f;int
length = 10;int
width = 20;float
circleArea = pi*radius*radius;// Calculating area of circle
int
rectArea = length*width;// Calculating area of rectangle
System.out.println("area of circle = "
+circleArea); System.out.println("area of rectangle = "
+rectArea); } }
Output:
area of circle = 346.5
area of rectangle = 200
Some key points about variables & data types
Following are some of the key points that we should be aware of :
- By default, operations on integer variables (eg 5+15, 2+3*5) returns an
int
type value, if result needs to be assigned into any other integer data type(eg.byte
orshort
) variable, it must be casted explicitly. - The value in a
char
variable must be a single character, assigning multiple character will result in compilation error. - A
boolean
variable value must be given astrue
orfalse
. it should not be like'true'
,"true"
,"false"
etc.
What do \n and \t do in java program ?
The \n and \t are part of escape sequence characters in java. In java \n is used to insert a newline in the text at this point which means anything that you write after \n in a string that will be printed in a new line. While \t is used to insert a tab in the text at this point which means anything written after \t in a string will be printed after a tab space. Refer the program below.
Along with \n and \t there are several more escape sequence characters in java, you can refer this link to get details about them. Let's see all above points in the program below :
Java program of \n and \t escape character
class
VariableAndDataType {public static void
main(String [] args) {byte
a = 20;byte
b = 10;byte
sum = (byte
)(a+b);// casting explicitly
// char ch = 'abc';
// Can not assign multiple characters
char
ch = 'a';// boolean b2 = 'false' , c ="false";
// Can not use '', or ""
boolean
b2 =false
; System.out.println("sum = "
+sum); System.out.println("ch = "
+ch); System.out.println("b2 = "
+ b2); System.out.println("First line \nSecond line"
); System.out.println("refresh \t java"
); System.out.println("Displaying \" in string."
); } }
Output:
sum = 30
ch = a
b2 = false
First line
Second line
refresh java
Displaying " in string.
Let's see few more points that we should be aware of while writing the program in java :
- If an integer value is assigned to any float data types, java will automatically convert that integer
number to float number by appending
.0
in value of that variable. - Values of integer data types can be given in three different number system - Decimal, HexaDecimal and Binary.
Binary number system is supported from java SE 7 and above. HexaDecimal values are
given with a prefix
0x
while binary values are prefixed with0b
.
Java hexaDecimal program | Java binary representation program
class
VariableAndDataTypeExample {public static void
main(String [] args) {float
f = 15;byte
b = 125;double
d = b;int
hexVal = 0x18;// Hexadecimal representation
int
binVal = 0b101010;// Binary representation
System.out.println("f = "
+f); System.out.println("d = "
+d); System.out.println("hexVal = "
+hexVal);// Prints the decimal value of hexVal variable
System.out.println("binVal = "
+binVal);// Prints the decimal value of binVar variable
} }
Output:
f = 15.0
d = 125.0
hexVal = 24
binVal = 42
How are hexadecimal numbers represented in Java ?
Hexadecimal numbers are represented via a prefix 0x
. If a number starts with 0x
,
it means it's a hexadecimal number. So anything after 0x
is considered as hexadecimal number. For example in assignment int
val = 0x18;
18 will be considered as hexadecimal number, in decimal it represents number 24. Refer the program example given above.
How are binary numbers represented in Java ?
Binary numbers are represented via a prefix 0b
. If a number starts with 0b
,
it means it's a binary number. So anything after 0b
is considered as binary number. For example in assignment int
val = 0b101010;
101010 will be considered as binary number, in decimal it represents number 42. Refer the program example given above.