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
classAdditionAndAverage {public static voidmain(String [] args) {intfirstNum = 1234;intsecondNum = 5678;intsum = firstNum + secondNum;// Adding two numberintavg = (firstNum + secondNum)/2;// Calculating average of two numberSystem.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
classMultiplicationAndDivision {public static voidmain(String [] args) {intfirstNum = 100;intsecondNum = 5;intresult = firstNum * secondNum;// Multiplying two numberintresult2 = firstNum / secondNum;// Dividing two numberdoubleresult3 = (double)200 / 6;// Division which returns decimal type valueSystem.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
classCalculateArea {public static voidmain(String [] args) {floatradius = 10.5f;floatpi = 22/7f;intlength = 10;intwidth = 20;floatcircleArea = pi*radius*radius;// Calculating area of circleintrectArea = length*width;// Calculating area of rectangleSystem.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
inttype value, if result needs to be assigned into any other integer data type(eg.byteorshort) variable, it must be casted explicitly. - The value in a
charvariable must be a single character, assigning multiple character will result in compilation error. - A
booleanvariable value must be given astrueorfalse. 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
classVariableAndDataType {public static voidmain(String [] args) {bytea = 20;byteb = 10;bytesum = (byte)(a+b);// casting explicitly// char ch = 'abc';// Can not assign multiple characterscharch = 'a';// boolean b2 = 'false' , c ="false";// Can not use '', or ""booleanb2 =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
.0in 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
0xwhile binary values are prefixed with0b.
Java hexaDecimal program | Java binary representation program
classVariableAndDataTypeExample {public static voidmain(String [] args) {floatf = 15;byteb = 125;doubled = b;inthexVal = 0x18;// Hexadecimal representationintbinVal = 0b101010;// Binary representationSystem.out.println("f = "+f); System.out.println("d = "+d); System.out.println("hexVal = "+hexVal);// Prints the decimal value of hexVal variableSystem.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.


