Loading lesson path
Java
Java How To's focused on Java How To's and related concepts.
These examples show how to solve common tasks in Java.
Explanation: We create two integer variables ( x and y ) and assign them values. The expression x + y is stored in the variable sum . Finally, we print the result with System.out.println() .
Exchange the values of two variables using a temporary variable:
Find out if a number is even or odd:
Take an integer and print it in reverse order:
Find out if a number is positive or negative:
You can use Math.sqrt() to find the square root of a number:
The area of a rectangle can be found by multiplying the length of the rectangle by the width:
Use the formula F = C * 9/5 + 32 to convert temperatures:
Add up all digits (e.g., 352: 3 + 5 + 2 = 10):
An Armstrong number is equal to the sum of its digits raised to the power of the number of digits (e.g. 153).
Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive):
You can easily count the number of words in a string with the following example:
Loop through each character and count a, e, i, o, u (case-insensitive).
Delete all vowels ( a, e, i, o, u ) from a string (case-insensitive).
Go through each character and count how many are digits (0-9).
You can easily reverse a string by characters with the following example:
Explanation: We compare the first character with the last, the second with the second-last, and so on. - If all pairs match, the string is a
Two strings are anagrams if they contain the same characters in a different order:
There are many ways to convert a string to an array. The simplest way is to use the toCharArray() method:
There are two common ways to remove whitespace in Java: using trim() and using replaceAll() .
Use a HashMap to count how many times each character appears:
Get the sum of array elements:
Create a program that calculates the average of different ages:
You can use the sort() method, found in java.util.Arrays , to sort an array:
Create a program that finds the lowest age among different ages:
Scan the array and keep track of the biggest value found so far:
Find the second highest number without sorting the whole array:
Go through the array and keep track of the highest and lowest values:
Combine two int arrays into a single array:
Convert an array into a Set to remove duplicates:
Print which elements appear more than once:
Randomly change the order of elements in an array using Collections.shuffle() :
Use a loop to calculate the factorial of a given number:
Print the first 10 numbers of the Fibonacci sequence:
The GCD (Greatest Common Divisor) is the largest number that divides two numbers without leaving a remainder.
A prime number is only divisible by 1 and itself.
Loop through the elements of an ArrayList :
Loop through the items of a HashMap with a for-each loop.
The enum type has a values() method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum: