Java Codes

Enhanced for loop in java with example

Enhanced for loop is simple version of for loop for java arrays

Syntax:-

for(datatype variable : array_name)
{
body of loop
}

Example:-
for(int i : value)

variable is required to hold element of array.
datatype is used for declaring variable data type.
array_name is used to defined which array is using enhanced for loop.

Example:-

import java.util.Scanner;

class group{

public static void main(String arng[]){

int value[]= new int[5];

Scanner data = new Scanner(System.in);

float sum =0;

System.out.println("Enter 5 element of array" );

// Enhanced for loop

for(int i : value)
{

i = data.nextInt();

sum = sum+i;

}

System.out.println("Average of array element");
sum = sum/value.length;
System.out.println(sum);

}

}

Here value is array name 




Previous Code:-
Length function for array

List Code:-
Java codes

Next Code:-


Java Programs:-
List of Java Programs

How to find size of array in java with example

If you want to know size or length of array than use inbuilt in java function

Syntax:-

array_name.length

array_name is name of array

length is used to fined size/index value of the array. If you change number of element in array, length function not get effect.

The output of array_name.length is size of array

Note:-
# The output of array_name.length is always integer
# Array_name is variable

Example:-

class group{
public static void main(String argn[]){
int data [] = {45,87,124,78,127,2};

System.out.println("Index value of data array is "+data.length);

for(int i = 0; i < data.length; i++)
System.out.println(data[i]);
}
}

Output:-

Index value of data array is 6
45
87
124
78
127
2




Previous Code:-
Initialization of arrays

List Code:-
Java codes

Next Code:-
Enhanced for loop

Java Programs:-
List of Java Programs

Initialization of arrays in java with example

We can initialize arrays at time of declaration of array.

Syntax:-

datatype variable_name [] = {value1, value2.... valueN};

Example:-

int sum[] = {2, 8, 12, 895, 147};

datatype specifies the datatype of each element present in array. Datatype of each element should be same. Here datatype may be byte, int, long, float, double and char.

variable_name is name of variable.

List of value/data is separated by comma between curly braces. The commas separated the values of the array element.

There is no need to use new operator because the array automatically create size and memory required for element.

Example:-

class group{
public static void main(String arng[]){
int sum[]={5,68,78,84};
System.out.println("index \t value" );
for(int i=0; i < sum.length; i++)
System.out.println(i+ "\t"+sum[i]);
}
}

Output:-

index       value
0               5
1               68
2               78
3               84


Previous Code:-
Arrays

List Code:-
Java codes

Next Code:-
Length function for array

Java Programs:-
List of Java Programs

Array in java with examples

Array is a collection of similar data types in which each element is accessed by its index value and located in separate memory location.

Syntax-

datatype variable_name = new datatype[size];

Example:-

int sum = new int[12];

datatype specifies the datatype of each element present in array. Datatype of each element should be same

Here datatype may be byte, int, long, float, double and char.
variable_name is name of variable.

new is a special operator that allocate memory for array.

size indicates the maximum number of elements that can be store in array.

Here sum is variable name and int is integer datatype.

Note:-

# Index value is start from zero and ends at size-1
# In java all array are dynamically allocated.
# Skip value is set to zero in array
# Boolean datatype is not used in arrays
# If we try to access element outside range of the array than we get run time error.
# index values always positive number

Different types of array declaration

Following syntax also be used to declaration of array

Syntax:-

datatype variable_name [] = new datatype[size];

or

datatype[] variable_name = new datatype[size];

Example

int sum[] = new int[12];

or

int[] sum = new int[12];

Example:-

simple java array program


class group{
public static void main(String arng[]){
int sum[]=new int[5];
sum[0]= 1;
sum[1]= 4;
sum[2]= 27;
sum[3]= 4;
sum[4]= 148;

System.out.println("index \t data");

for(int i=0; i<5;i++)
System.out.println(i+ "\t"+sum[i]);
}
}

Output:-

index   data
0           1
1           4
2          27
3           4
4         148

program for sum of array element

import java.util.Scanner;
class group{
public static void main(String arng[]){
int total = 0;
int sum[]=new int[5];
Scanner data = new Scanner(System.in);

System.out.println("Enter 5 element of array");

for(int i=0; i<5;i++)
 sum[i] = data.nextInt();
 for(int i=0; i < 5;i++)
 total = total +sum[i];
 System.out.println("Sum of array element is:"+total);
}
}

Skip value is set to zero in array

class group{
public static void main(String arng[]){
int sum[]=new int[5];
sum[0]= 420;
sum[1]= 847;
sum[2]= 479;
sum[4]= 147;

for(int i=0; i < 5; i++)
System.out.println(sum[i]);
}
}

Output:-

index5
420
847
479
0
147

Here index 3 is skipped so value is set to zero

Java program for character array

class group{
public static void main(String arng[]){
char sum[]=new char[6];
sum[0]= 'a';
sum[1]= 'b';
sum[2]= 'o';
sum[3]= 'u';
sum[5]= 't';
for(int i=0; i < 6; i++)
System.out.println(sum[i]);
}
}

Output:- 
index6
a
b
o
u

t

Here index 4 value is skipped





Previous Code:-
Return

List Code:-
Java codes

Next Code:-
Initialization of arrays

Java Programs:-
List of Java Programs

Return statement in java with example

Return statement in used to explicitly return from a method. Return causes program control to transfer back to the caller of the method.

The return statement immediately terminates the method in which it is executed.

Syntax:-

return;

Example:-

Program to find sum of 2 number

import java.util.Scanner;

class group{
public static void main(String arng[]){
Scanner data = new Scanner(System.in);
int num1, num2;
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();

total(num1,num2);

}

public static void total(int numx, int numy){
int sum=0;
sum=numx+numy;
if(sum==0)
return;
System.out.println("Sum of 2 number:"+sum);

}
}

Here total method is terminated when sum is zero.

if(sum=0) is necessary in above example because java compiler shows error "unreachable code" to avoid this error i used if condition to demonstration return statement.


Previous Code:-
Goto statement(labeled break)

List Code:-
Java codes

Next Code:-
Arrays

Java Programs:-
List of Java Programs

goto statement in java with example

In java goto keyword is not used. In place of goto we used break in java.

Why we using break in place of goto.
goto provides a way to branch in an arbitrary and unstructured manner. This usually makes goto ridden code hard to understand and hard to maintain. It also prohibits certain compiler optimization so break is used in place of break.

Syntax:-

break label;

label can be any variable but not java keyword. label is followed by colon.

Execution:-

Label break statement is used to transfer control unconditionally from one place to another place in program.

To use break as goto statement, we have to name a block, block are set of java statement. To name a block, put label at start of it.

Example:-

go:{
System.out.println("Hey");
.
.
block
.
.
}

Here go is label.

Once you labeled a block, you can use this label as the target of break statement.
When compiler see break statement with label than compiler transfer execution to at end of labeled block.

Label break statement is used exit from nested loops.

Example:-

Program to find sum of N number

import java.util.Scanner;

class group{
public static void main(String arng[]){
int num, i,sum=0;
go:{
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num=data.nextInt();
for(i=0;i<100;i++)
{
sum=sum+i;
if(i==num)
break go;
}
}
System.out.println("Sum of odd number:"+sum);
}
}

 Here go is label of break statement.
 Following block is go labeled block.
 go:{
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num=data.nextInt();
for(i=0;i<100;i++)
{
sum=sum+i;
if(i==num)
break go;
}
}

Note:-
# Declared variable inside labeled block are visible only inside labeled block.


Previous Code:-
Continue statement in java

List Code:-
Java codes

Next Code:-
Return

Java Programs:-
List of Java Programs

Decrement operator in java with example

The decrement operators used to subtract 1 to previous value. Decrement operator is unary operator because only one variable is used.

There are 2 type in decrement operator. They are
1. Postfix decrement operator
2. Prefix decrement operator

Syntax:-

For postfix

variable--

For prefix

--variable

Both prefix and postfix decrement operator are same thing when they used independently(i-- or --i) ie subtract 1 to previous value. But they behave differently when they used in expression on the right hand side of assignment statement(num=i-- or num=--i)

Postfix decrement operator:-

class group{
public static void main(String arng[]){
int num, i;
i=3;
i--;
num=i--;
System.out.println("i="+i);
System.out.println("num="+num);
}
}

Output:-

i=1
num=2

Execution:-

In postfix decrement operator, fist assign value to left side variable(num) than decrement by 1(i)

Here in our example first i is three(i=3) than i decrement by 1(i--). here i value is 2.

Next statement is num = i--;

Here postfix is used so first i value is assigned to num ie 2 than i is decrement by 1 ie i value becomes 1.

Prefix decrement operator

class group{
public static void main(String arng[]){
int num, i;
i=3;
--i;
num=--i;
System.out.println("i="+i);
System.out.println("num="+num);
}
}


Output:-

i=1
num=1

Execution:-

In prefix decrement operator, first decrement(i) by 1 than result value is assigned to left side variable(num)

Here in our example first i is three(i=3) than i decrement by 1(--i). here i value is 2.

Next statement is num = --i;

Here prefix is used so first i is decrement by 1 ie i value becomes 1. Than value of i is assigned to num ie 1

Example:-

Program to fined sum of N odd number.

import java.util.Scanner;

class group{
public static void main(String arng[]){
int num, i,sum=0;
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num=data.nextInt();
for(i=num;i>=0;i--)
{
if((i%2)==0)
continue;
else
sum=sum+i;
}
System.out.println("Sum of odd number:"+sum);
}
}


Previous Code:-
Increment operator

List Code:-
Java codes

Next Code:-
for loop

Java Programs:-
List of Java Programs

Increment operators in java with example

The increment operators used to adds 1 to previous value. Increment operator is unary operator because only one variable is used.

There are 2 type in increment operator. They are
1. Postfix increment operator
2. Prefix increment operator

Syntax:-

For postfix

variable++

For prefix

++variable

Both prefix and postfix increment operator are same thing when they used independently(i++ or ++i) ie adds 1 to previous value. But they behave differently when they used in expression on the right hand side of assignment statement(num=i++ or num=++i)

Postfix increment operator:-


class group{
public static void main(String arng[]){
int num, i;
i=0;
i++;
num=i++;
System.out.println("i="+i);
System.out.println("num="+num);
}
}

Output:-

i=2
num=1

Execution:-

In postfix increment operator, fist assign value to left side variable(num) than increment by 1(i)

Here in our example first i is zero(i=0) than i increment by 1(i++). here i value is 1.

Next statement is num = i++;

Here postfix is used so first i value is assigned to num ie 1 than i is increment by 1 ie i value becomes 2.

Prefix increment operator


class group{
public static void main(String arng[]){
int num, i;
i=0;
++i;
num=++i;
System.out.println("i="+i);
System.out.println("num="+num);
}
}


Output:-

i=2
num=2

Execution:-

In prefix increment operator, first increment(i) by 1 than result value is assigned to left side variable(num)

Here in our example first i is zero(i=0) than i increment by 1(++i). here i value is 1.

Next statement is num = ++i;

Here prefix is used so first i is increment by 1 ie i value becomes 2. Than value of i is assigned to num ie 2

Example:-

Program to fined sum of N odd number.

import java.util.Scanner;

class group{
public static void main(String arng[]){
int num, i,sum=0;
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num=data.nextInt();
for(i=0;i<=num;i++)
{
if((i%2)==0)
continue;
else
sum=sum+i;
}
System.out.println("Sum of odd number:"+sum);
}
}


Previous Code:-
else if statement

List Code:-
Java codes

Next Code:-
Decrement operator

Java Programs:-
List of Java Programs

Nested loop in java with example

Java allows for, while and do while loops to be nested. that is one loop is inside another loop.

Example:-

class group{
public static void main(String angt[]){
int i,j;
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
System.out.print(".");
System.out.println();
}
}
}

Output:-

.....
....
...
..
.

one type of loop can be nested with other type of loop. for example while loop is inside for loop.

Example:-


class group{
public static void main(String angt[]){
int i,j;
for(i=0;i<2;i++)
{
j=1;
while(j<=5)
{
System.out.println(j);
j++;
}
}
}
}

Previous Code:-
Do-while loop

List Code:-
Java codes

Next Code:-
Math class methods

Java Programs:-
List of Java Programs

Break statement in java with example

Break statement is used to exit from loop

Break statement in loops

Break statement immediately transfer control to the out of loop and statement which is immediately after loop is executed. In other word break statement terminate loop.

Note:-
# Break is terminated only one loop in case of nested loop

Example:-

Java program to count N number using break statement

import java.util.Scanner;

class group{
public static void main(String angt[]){
Scanner data = new Scanner(System.in);
int num,i;
System.out.println("Enter a number below 100");
num=data.nextInt();
for(i=1;i<100;i++)
{
System.out.println(i);
if(i==num)
break;
}
}
}
Example:-

Break statement in nested loop

import java.util.Scanner;

class group{
public static void main(String angt[]){
Scanner data = new Scanner(System.in);
int num,i,j;
System.out.println("Enter a number below 100");
num=data.nextInt();
for(j=0;j<2;j++)
{
for(i=1;i<100;i++)
{
System.out.println(i);
if(i==num)
break;
}
System.out.println("\t");
}
}
}

Break statement in switch

The break statement transfer the control to out of the switch statement and statement which is immediately after switch is executed.

Note:-
# break is optional, if 2 or more case labels belongs to same statement.

Program to accept day number of week and display the corresponding week day

import java.util.Scanner;
class group{
public static void main(String arg[]){
int day;
Scanner week = new Scanner(System.in);
System.out.println("Enter day number of the week ");
day = week.nextInt();
switch(day){
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
}
}
}


Previous Code:-
Trigonometric function in java

List Code:-
Java codes

Next Code:-
Continue statement in java

Java Programs:-
List of Java Programs

Continue statement in java with example

Continue statement tell the compiler to skip the following statament and continue with the next iteration

Syntax:-

continue;

Continue statement in for loop

Here continue causes the control to go to increment section(3rd section in for loop) of the loop than condition statement(2nd section in for loop).

Example:-

Java program for display odd number using continue statement

import java.util.Scanner;

class group{
public static void main(String angt[]){
Scanner data = new Scanner(System.in);
int num,i,j;
System.out.println("Enter a number");
num=data.nextInt();
for(i=1;i<=num;i++)
{
j=i%2;
if(j == 0)
continue;
else
System.out.println(i);
}
}
}

Continue statement in while and do while loop

Here continue causes the control to go to condition statement of loop.

Example:-

Java program display even number using continue statement

import java.util.Scanner;

class group{
public static void main(String angt[]){
Scanner data = new Scanner(System.in);
int num,i;
System.out.println("Enter a number");
num=data.nextInt();
i=0;
while(i<=num)
{
 i++;
if((i%2)==0)
continue;
else
System.out.println(i);
}
}
}

Previous Code:-
Break statement in java

List Code:-
Java codes

Next Code:-

Goto statement(labeled break)


Java Programs:-
List of Java Programs

How to used Trigonometric function in java with example

Math class method is used for Trigonometric function in java. Trigonometric function like cosine, sine hyperbolic sine, arc sine can be used in java using math class methods

Syntax:-

Math.method(Variable)

method can be replaced by any Trigonometric function

List Trigonometric function used in java

Method Operation Datatype used
Math.sin(variable) sine Double
Math.cos(variable) cosine Double
Math.tan(variable) tan Double
Math.sinh(variable) hyperbolic sine Double
Math.cosh(variable) hyperbolic cosin Double
Math.tanh(variable) hyperbolic tan Double
Math.asin(variable) arc sine Double
Math.acos(variable) arc cosine Double
Math.atan(variable) arc tan Double

Convert radians to degrees in java

Syntax:-

toDegrees(variable)

toDegrees Converts an angle measured in radians to an approximately equivalent angle measured in degrees.

Convert degrees to radians in java

Syntax:-

toRadians(variable)

Converts an angle measured in degrees to an approximately equivalent angle measured in radians.

Example:-

import java.util.Scanner;

class group{
public static void main(String angt[]){
Scanner data = new Scanner(System.in);
double num,num1;
System.out.println("Enter a number");
num1=data.nextDouble();

num=Math.cos(num1);
System.out.println("Answer:"+num);

num=Math.sinh(num1);
System.out.println("Answer:"+num);

num=Math.atan(num1);
System.out.println("Answer:"+num);
num=Math.toDegrees(num1);
System.out.println("Answer:"+num);

}
}

Output:-

Enter a number
2
Answer:-0.4161468365471424
Answer:3.626860407847019
Answer:1.1071487177940904
Answer:114.59155902616465



Previous Code:-
Logarithm function in java

List Code:-
Java codes

Next Code:-


Java Programs:-
List of Java Programs

how to use logarithm function in java with example

logarithm function can be used in java using math class method

1. Natural logarithm in java

Syntax:-

Math.log(variable)

variable must be double datatype.

log method used to find natural logarithm (base e) of a variable

Math.log(2) = 0.6931471805599453

2. base 10 logarithm in java

Syntax:-

Math.log10(variable)

variable must be double datatype.

log method used to find base 10 logarithm of a variable

Math.log10(2) = 0.3010299956639812

3. base 10 logarithm in java

Syntax:-

Math.log1p(variable)

variable must be double datatype.

Here 1 is added to the variable than take natural logarithm of that sum. ie log(variable+1)

Math.log1p(2) = 1.0986122886681096

Note:-
# If the variable is NaN or less than zero, then the result is NaN.
# If the variable is positive infinity, then the result is positive infinity.
# If the variable is positive zero or negative zero, then the result is negative infinity.
# The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Example:-

import java.util.Scanner;

class group{
public static void main(String angt[]){
Scanner data = new Scanner(System.in);
double num,num1;
System.out.println("Enter a number");
num1=data.nextDouble();

num=Math.log(num1);
System.out.println("log Answer:"+num);

num=Math.log10(num1);
System.out.println("log10 Answer:"+num);

num=Math.log1p(num1);
System.out.println("log1p Answer:"+num);


}
}

Output:-

Enter a number
3
log Answer:1.0986122886681098
log10 Answer:0.47712125471966244
log1p Answer:1.3862943611198906



Previous Code:-

List Code:-

Next Code:-

Java Programs:-

Math Class Methods in java with examples

Math class has different methods for finding power, absolute value, min, max and rounding number etc

Syntax:-

Math.methods()

Here methods is replaced by different math class methods word

1. absolute value using java Math Class

Syntax:-

Math.abs(variable)

Here variable can be double,float,int and long datatype.

abs method is used to find absolute value of variable.

Math.abs(-2.3) = 2.3

2. minimum value using java Math Class

Syntax:-

Math.min(variable1,variable2)

Here variable can be double,float,int and long datatype

min method is used to find minimum value between variable1 and variable2.

Math.min(2,8) = 2

3. maximum value using java Math Class

Syntax:-

Math.max(variable1,variable2)

Here variable can be double,float,int and long datatype

max method is used to find maximum value between variable1 and variable2.

Math.max(2,8) = 8

4.Rounding number using java Math Class

Syntax:-

Math.ceil(variable)

Math.floor(variable)

Here variable is double datatype

ceil method is used round up the variable and floor method is used round down the variable.

Math.ceil(8.4) = 9

Math.floor(8.4) = 8

5. power of the number using java Math Class

Syntax:-

Math.pow(variable1,variable2)

Here variable is double datatype

here variable1 is base value and variable2 is power value.


Math.pow(2,3) = 8

6. square root of the number using java Math Class

Syntax:-

Math.sqrt(variable)

Here variable is double datatype

sqrt method is used for finding square root of a number.

Math.sqrt(9) = 3

7. cube root of the number using java Math Class

Syntax:-

Math.cbrt(variable)

Here variable is double datatype

cbrt method is used for finding cube root of a number.

Math.cbrt(27) = 3

8. copy sign of the number using java Math Class

Syntax:-

Math.copySign(variable1, variable2)

Here variable is float and double datatype

copySign method is used to copy sign of variable2 and assign that sign to variable1.

Math.copySign(2,-8.3) = -2.0

Math.copySign(2,8.3) = 2.0

9. Euler's number e to power of a number using java Math Class

Syntax:-

Math.exp(variable)

Here variable is double datatype

Euler's number e raised to the power of a variable.

Math.exp(2) = 7.38905609893065

10. Euler's number e to power of a number minus 1 using java Math Class

Syntax:-

Math.expm1(variable)

Here variable is double datatype

Euler's number e raised to the power of a variable minus 1 ie e^(variable)-1

Math.expm1(2) = 6.38905609893065


Example:-

import java.util.Scanner;

class group{
public static void main(String angt[]){
Scanner data = new Scanner(System.in);
double num1,num2,num;
System.out.println("Enter a number");
num1=data.nextInt();
System.out.println("Enter a number");
num2=data.nextInt();

num=Math.abs(-2.5);
System.out.println("absolute:"+num);

num=Math.min(num1,num2);
System.out.println("min:"+num);

num=Math.max(num1,num2);
System.out.println("max:"+num);

num=Math.ceil(8.4);
System.out.println("ceil:"+num);

num=Math.floor(8.4);
System.out.println("floor:"+num);

num=Math.pow(num1, num2);
System.out.println("power:"+num);

num=Math.sqrt(num1);
System.out.println("sqrt:"+num);

num=Math.cbrt(num2);
System.out.println("cbrt:"+num);

num=Math.copySign(num1,-8.2);
System.out.println("copysign:"+num);

num=Math.exp(2);
System.out.println("exp:"+num);

num=Math.expm1(2);
System.out.println("expm1:"+num);

}
}

Output:-

Enter a number
8
Enter a number
27
absolute:2.5
min:8.0
max:27.0
ceil:9.0
floor:8.0
power:2.4178516392292583E24
sqrt:2.8284271247461903
cbrt:3.0
copysign:-8.0
exp:7.38905609893065
expm1:6.38905609893065


Previous Code:-
Nesting loop in java

List Code:-
Java codes

Next Code:-
Logarithm function in java

Java Programs:-
List of Java Programs

Conditional operator in java with example

Conditional operator is alternative to simple if-else statement

Syntax:-

expr1 ? expr2 : expr3

Execution:-

expr1 is always evaluated, if results is true than expr2 is executed otherwise expr3 is executed.

Example:-

Program to find given number is even or odd

import java.util.Scanner;

class group{
public static void main(String arg[]){
int num;
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num = data.nextInt();
System.out.println((num%2)==0 ? "even number":"odd number");
}
}


Previous Code:-
Bitwise operators

List Code:-
Java codes

Next Code:-
if decision making statement

Java Programs:-
List of Java Programs

Bitwise operators in java with example

Bitwise used for manipulation of data in bit level.

Operator Function
` bitwise NOT
<< left shift
>> right shift
>>> shift right zero fill
& bitwise AND
^ bitwise XOR
| bitwise OR
&= bitwise AND assign
^= bitwise XOR assign
|= bitwise OR assign

Bitwise not applicable for float, double and char.

Syntax:-

syntax for & | and ^

value1 bitwise value2

Example:-
2 & 3
num1 & num2

Here value1 and value2 must be integer data type


syntax for >> and <<

value bitwise number_of_shift

Example:-
6 >> 2
num >> 3

Here value and number_of_shift  must be integer data type

Note:-
# Left shift by 2 is equal to multiply by 2
# Right shift by 2 is equal to division by 2
# <<< operator is not used

Example:-

Program for shift left

import java.util.Scanner;
class group{
public static void main(String arg[]){
int num;
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num = data.nextInt();
num=num<<2;
System.out.println("Answer:"+num);
}
}

Output:-
Enter a number
5
answer: 20

 5 is in binary 101 when we shift left we put zero at right side. Here we shifting by 2 so we put 2 zero at right side 10100 ie 20


Previous Code:-

List Code:-

Next Code:-

Java Programs:-

Logical operators in java with example

Operator Meaning
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
&& logical AND
|| logical OR
! logical NOT

The above 1st 6 are relational operators in java and last 3 are logical operators

The relation operators are used to compare 2 values. The output of relational operators is either true (1) or false (0)

Syntax:-

value1 relational_operator value2


Example:-

2 < 10     True
3 > 8       False
7.5>=5    True

value1 and value2 are can be constants, variable or combination of them.

Relation operator are used in if-else or in loop statement.

Example:-

Program to find Greatest number between 2

import java.util.Scanner;

class group{
public static void main(String arg[]){
int num1, num2;
Scanner grest = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = grest.nextInt();
System.out.println("Enter 2nd number");
num2 = grest.nextInt();
if(num1 < num2)
System.out.println("Greatest number"+num1);
else
System.out.println("Greatest number"+num2);
}
}

Logical operators:-

logical operators && and || are used to when we want to check 2 or more condition statement.

Example

8 > 7 && 5 < 8      True
8 < 5 || 8 < 9           False
7 > 2 && 19 < 2    False

AND (&&) operator output is true only when either condition statement is true.
OR (||) operator output is false only when either condition statement is false.

Truth table:-


A B AND
0 0 0
0 1 0
1 0 0
1 1 1


A B OR
0 0 0
0 1 1
1 0 1
1 1 1



NOT (!)  logical operator is used for compliment condition statement outputs.

Truth table:-

A NOT
0 1
1 0


Example:-

Program to solve following problem
The candidate is selected if his age and height is 18 and 5 and more respectively.

import java.util.Scanner;
class group{
public static void main(String arg[]){
int age, height;
Scanner data = new Scanner(System.in);
System.out.println("Enter candidate age");
age = data.nextInt();
System.out.println("Enter cadidate height");
height = data.nextInt();
if((age >= 18) && (height >= 5))
System.out.println("The cadidate is selected");
else
System.out.println("sorry, candidate not selected");
}
}

Java program for logical NOT
Program to find max number between 2

import java.util.Scanner;
class group{
public static void main(String arg[]){
int num1,num2;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
if(!(num1>num2))
System.out.println("max nuber between 2 is:"+num2);
else
System.out.println("max nuber between 2 is:"+num1);
}
}




Previous Code:-
Arithmetic operators

List Code:-
Java codes

Next Code:-
Bitwise operators

Java Programs:-
List of Java Programs

Arithmetic operators in java with example

Different Arithmetic operators used java are shown in table.

Operator function
* Multiplication
/ Division
% Remainder
+ Addition
- Subtraction

* operator is used give product of 2 numbers.

/ Operator gives Quotient after dividing Divisor and Dividend. Divisor should be nonzero number.

% operator gives remainder after division of 2 numbers. % operator can be used only for integer data type.

+ operator gives sum of 2 numbers.

- operator used to subtract one number from another number.

Note:-

# If both number are positive than answer will be in positive number.
# If either or both number are negative than answer will be either in positive or in negative, it is machine dependent.

Example:-

Program to find sum of 2 number.

import java.util.Scanner;

class find_sum{
public static void main(String arg[]){
int num1, num2, sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
sum=num1+num2;
System.out.println("sum of 2 number:"+sum);
}
}

Program to find product of 2 number.

import java.util.Scanner;

class find_sum{
public static void main(String arg[]){
int num1, num2, sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
sum=num1*num2;
System.out.println("Product of 2 number:"+sum);
}
}

Program to find subtract 2 number.

import java.util.Scanner;

class find_sum{
public static void main(String arg[]){
int num1, num2, sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
sum=num1-num2;
System.out.println("answer after subtract 2 number:"+sum);
}
}

Program to find division 2 number.

import java.util.Scanner;

class group{
public static void main(String arg[]){
int num1, num2, sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
if(num2!=0)
{
sum=num1/num2;
System.out.println("Quotient:"+sum);
}
else
System.out.println("Divisor should be nonzero number");
}
}

Program to find remainder.
import java.util.Scanner;

class group{
public static void main(String arg[]){
float num1, num2;
double sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
if(num2!=0)
{
sum=num1%num2;
System.out.println("Remainder:"+sum);
}
else
System.out.println("Divisor should be nonzero number");
}
}


Previous Code:-
User Input

List Code:-
Java codes

Next Code:-
Logical operators

Java Programs:-
List of Java Programs

How to run java program in notepad

Before writing code and run we have to install java JDK. Click here to download Java JDK

Download Java SE JDK.

Install JDK in default setting and check javac.exe present in this dir C:\Program Files\Java\jdk1.6.0_26\bin if its not there, find its dir.

Note:- jdk1.6.0_26 is changing depending on version of Java JDK.

After install JDK follow this steps

Go to Mycomputer and right click on mycomputer, goto properties -> Advanced system settings -> Select Advanced tab -> click on Environment Variables -> click on New

Write following data in New User Variable window

Variable name:path
Variable value:C:\Program Files\Java\jdk1.6.0_26\bin

Note:- Variable value is dir of javac.exe.

Now save above settings.

Now open run -> type cmd -> now cmd window will open.

Type javac in cmd window, if you got following info as show in figure than your JDK software installed properly.



Now open notepad and write following codes

class disp{
public static void main(String arg[]){

System.out.println("Welcome to aboutcodes");

}
}

Save notepad in c drive by giving name example.java


Now again open cmd window and change dir to c drive by typing following command

dir c:\

Now type following command to run java program

javac example.java
java disp

Now you see Welcome to aboutcodes on black screen.

you can use other compiler like Netbeans, Eclipse etc.

Eclipse
Ellipse software is used for Java Codes. it can also be used for other languages such as C, C++, PHP, Perl, Python, Ruby, Ada or COBOL.
Download Now

Netbeans
Netbeans used to run java programs.
Download Now



Previous Code:-

List Code:-

Next Code:-

Java Programs:-

User input in java with examples

For Integer:-

import java.util.Scanner;

class group{
public static void main(String arg[]){
int num;
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num = data.nextInt();
System.out.println("Entered number is:"+num);
}
}

Above program is simple example for user input where user enter a number which is assigned to variable num.

where Scanner takes information from user keyboard and saved in variable.For using Scanner we have to import Scanner.

Syntax for import a Scanner:-

import java.util.Scanner;

data is Scanner variable. when we press a key, data store value of that key.

num = data.nextInt();
In above line we assigning value of data to num. where num is integer so we used a commend data.nextInt() which Scans input as integer.

Scanner_variable.nextInt(); - The int scanned from the input.

Example:-

Program to find sum of 2 number.

import java.util.Scanner;

class find_sum{
public static void main(String arg[]){
int num1, num2, sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
sum=num1+num2;
System.out.println("sum of 2 number:"+sum);
}
}

Here data is Scanner variable. int is scanned from input and assigned to num1 and num2 variables.

For Floating Point:-

Scanner_variable.nextFloat(); - The float scanned from the input


Scanner_variable.nextDouble(); - The double scanned from the input.

Example:-

Program to find sum of 2 number.

import java.util.Scanner;

class Find_sum{
public static void main(String arg[]){
double num1, num2, sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextDouble();
System.out.println("Enter 2nd number");
num2 = data.nextDouble();
sum=num1+num2;
System.out.println("sum of 2 number:"+sum);
}
}


For Boolean:-

Scanner_variable.nextBoolean(); - The boolean scanned from the input

For Characters:-

Scanner_variable.nextLine(); - The character Scanned from the input.

Example:-

Program to display user input.

import java.util.Scanner;

class disp{
public static void main(String arg[]){
Scanner data = new Scanner(System.in);
System.out.println(data.nextLine());
}
}

or


import java.util.Scanner;

class group{
public static void main(String arg[]){
System.out.println("Enter your text");
Scanner data = new Scanner(System.in);
String text = data.nextLine();
System.out.println(text);
}
}


Previous Code:-
User output

List Code:-
Java codes

Next Code:-
Arithmetic operators

Java Programs:-
List of Java Programs

Back to Top