Java Tutorial

by Dinesh Thakur Category: Operators and Expressions

The left-shift, right-shift, and zero-fill-right-shift operators <<, >>, and >>> shift the individual bits of an integer by a specified integer amount.

 
by Dinesh Thakur Category: Operators and Expressions

The bitwise AND, OR, and XOR operators (&, |,and /\) all act on the individual bits of an integer. These operators are useful when an integer is being used as a bit field.

 
by Dinesh Thakur Category: Operators and Expressions

The bitwise complement operator (~), which perform a bitwise negation of an integer value. Bitwise negation means that each bit in the number is toggled. In other words, all the binary 0s become 1s and all the binary 1s become 0s.

 
by Dinesh Thakur Category: Operators and Expressions

This Java continue statement with label example shows how to use java continue statement to skip to next iteration of the labeled loop.



 
by Dinesh Thakur Category: Operators and Expressions

Like the break statement, the continue statement also skips the remaining statements of the body of the loop where it is defined but instead of terminating the loop, the control is transferred to the beginning of the loop for next iteration. The loop continues until the test condition of the loop becomes false.

 
by Dinesh Thakur Category: Operators and Expressions

This Java break statement with label example shows how to use java break statement to terminate the labeled loop.



 
by Dinesh Thakur Category: Operators and Expressions

This Java break statement example shows how to use java break statement to terminate the loop. The Java break statement has two forms labeled and unlabeled. in the below example You can see unlabeled form.



 
by Dinesh Thakur Category: Operators and Expressions

Constants: Constants in java are fixed values those are not changed during the Execution of program. A literal is a constant value that can be classified as integer literals, string literals and boolean literals. To make a static field constant in Java, make a variable as both static and final. java supports several types of Constants  those are  



 
by Dinesh Thakur Category: Operators and Expressions

Identifiers :- The Identifiers are those which are used for giving a name to a variable  ,class and method ,packages ,interfaces etc There Are Some Rules against for using the Identifiers

 
by Dinesh Thakur Category: Operators and Expressions

An operator is a special symbol that tells the compiler to perform a specific mathematical or logical operation on one or more operands where an operand can be an expression. An operation is an action(s) performed on one or more operands that evaluates mathematical expressions or change the data.

 
by Dinesh Thakur Category: Control Structures

Currently the definition of Greatest Common Divisor (GDC) can be formalized as well:

Let a, b and c nonzero integers, we say that c is a common divisor of a and b to c divides (write c | a) and c divides b (c | b). We call D (a, b) the set of all common divisors of a and b.

The code snippet below shows how to calculate the GDC two reported numbers:

 
by Dinesh Thakur Category: Control Structures

Finalizer methods are almost the opposite of constructor methods. A constructor method is used to initialize an object, while finalizer methods are called just before the object is garbage-collected and its memory reclaimed. The syntax of the finalizer method is simply finalize(). The Object class defines a default finalizer method. To create a finalizer method, override the finalize() method using the following signature:

 
by Dinesh Thakur Category: Control Structures

In the switch statement, since same statements has to be executed corresponding to different cases ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U') so we write the statements with the last case. The break statement causes the switch statement to terminate when any of the vowels is entered.

 
by Dinesh Thakur Category: Control Structures

In this example, the continue statement is placed in the body of inner for loop. While executing inner for loop, if the condition (m==n) evaluates to true then the continue statement is executed and the remaining statement for displaying values of m and n is skipped and control is transferred to the increment expression (n++) of inner for loop. This increments the value of n by 1 and test condition (n<=2) is evaluated again for this incremented value of n. This process continues. We have used continue statement in this program so that the same value of m and n should not be displayed.

 
by Dinesh Thakur Category: Control Structures

This example computes the sum of positive numbers input by the user. When a negative number is input, the condition (num<0) become true and break statement is executed which leads to the termination of the while loop and the next statement following the loop is executed which displays the sum of positive numbers. The condition of the while loop always remains true as we have specified a non-zero value 1 which makes it run infinitely. The only way to exit this loop is to. use break statement.

 
by Dinesh Thakur Category: Control Structures

Like the nesting of if and other looping statements, the for loop can also be nested. In other words, we can have a for loop in the body of other for loop. There is no restriction on the level of nesting of loops but one should take care while nesting at multiple levels otherwise unexpected results may arise.

 
by Dinesh Thakur Category: Control Structures

In this example, we input the interval values a and b. Each time loop is executed the value of fx is calculated and displayed on the screen for the value of x which ranges from a to b. On each iteration, x is incremented by 0.05. This continues until x is less than equal to the value of b that user inputs.

 
by Dinesh Thakur Category: Control Structures

In this example, we input number of elements n (i.e.) whose average is to be calculated. When for loop begins executing, the loop control variable i is declared and initialized to 1. Then the test condition (i<=n) is checked. As it is true in this case because (1<=5) and the statements in the body of the loop are executed which inputs the first number (5 in our case) and add this value to variable sum. Then the increment expression i++ increases the value of variable i by 1 (i+ 1=2). After one complete iteration, the test condition in the for loop is checked again which is true again as (2<=5) and the body of the loop is executed again. This process continues until the loop control variable (i) is incremented to 6. Now when the test condition (6<=5) is evaluated again it becomes false and the execution of for loop terminates and control transfers to the next statement following the for loop that calculates the average of n (5) numbers which is then displayed.

 
by Dinesh Thakur Category: Control Structures

In this example, the sum of first 10 natural numbers is displayed. First, input the value of n (10 in this case) i.e. number of natural numbers whose sum is to be calculated. Then, after initializing the variables i to 1 and sum to 0, we enter the do-while loop. The execution of the body of loop continues as long as condition (i<=n) evaluates to true. When variable 1’s is value becomes 11, the condition becomes false and this terminates the do-while loop and program execution continues with the next statement after the loop which displays the sum of first 10 natural numbers.

 
by Dinesh Thakur Category: Control Structures

In this program, we first input the number (Say num = 12345). Next the control reaches the while loop where it checks the condition (num>0) which is true as (12345>0) so the body of the loop is executed.

 

Page 6 of 40



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.