Found 272 Articles for Java8

Blank final in Java

karthikeya Boyini
Updated on 18-Jun-2020 12:40:21

584 Views

In Java, a final variable can a be assigned only once. It can be assigned during declaration or at a later stage. A final variable if not assigned any value is treated as a blank final variable. Following are the rules governing initialization of a blank final variable.A blank instance level final variable cannot be left uninitialized.The blank Instance level final variable must be initialized in each constructor.The blank Instance level final variable cannot be initialized in class methods.A blank static final variable cannot be left uninitialized.The static final variable must be initialized in a static block.A static final variable cannot ... Read More

Bitwise right shift operator in Java

Samual Sam
Updated on 29-Jul-2021 15:53:33

13K+ Views

Java supports two types of right shift operators. The >> operator is a signed right shift operator and >>> is an unsigned right shift operator. The left operands value is moved right by the number of bits specified by the right operand.Signed right shift operatorThe signed right shift operator '>>' uses the sign bit to fill the trailing positions. For example, if the number is positive then 0 will be used to fill the trailing positions and if the number is negative then 1 will be used to fill the trailing positions.Assume if a = 60 and b = -60; ... Read More

Automatic resource management in Java

Samual Sam
Updated on 18-Jun-2020 12:13:37

576 Views

automatic resource management or try-with-resources is a new exception handling mechanism that was introduced in Java 7, which automatically closes the resources used within the try-catch block.ResourceA resource is an object which is required to be closed once our program finishes. For example, a file is read, database connection and so on.UsageTo use the try-with-resources statement, you simply need to declare the required resources within the parenthesis, and the created resource will be closed automatically at the end of the block. Following is the syntax of the try-with-resources statement.Syntaxtry(FileReader fr = new FileReader("file path")) {        // use the resource ... Read More

Association, Composition and Aggregation in Java

Samual Sam
Updated on 18-Jun-2020 10:41:15

7K+ Views

AssociationAssociation refers to the relationship between multiple objects. It refers to how objects are related to each other and how they are using each other's functionality. Composition and aggregation are two types of association.CompositionThe composition is the strong type of association. An association is said to composition if an Object owns another object and another object cannot exist without the owner object. Consider the case of Human having a heart. Here Human object contains the heart and heart cannot exist without Human.AggregationAggregation is a weak association. An association is said to be aggregation if both Objects can exist independently. For ... Read More

Pollard’s Rho Algorithm for Prime Factorization in java

Ankith Reddy
Updated on 25-Jun-2020 12:59:32

160 Views

It is an algorithm to perform factorization on given integers. Following is the program implementing the Rho Algorithm for Prime Factorization.ProgramLive Demopublic class PollardsRho {    int num = 65;    public int gcd(int a, int b) {       int gcd = 0;       for(int i = 1; i

Sieve of Eratosthenes in java

George John
Updated on 25-Jun-2020 12:55:11

4K+ Views

Sieve of Eratosthenes is the ancient algorithm to find prime numbers up to a given number.Algorithm1. Generate integers from 2 to n (Given number).2. Counting from 2 mark every 2nd integer. (multiples of 2)3. Now, starting from 3 mark every third integer. (multiples of 3)4. Finally, marking from 5 mark every 5th integer.(multiples of 5)Programimport java.util.Scanner; public class SievePrimeFactors  {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number");       int num = sc.nextInt();       boolean[] bool = new boolean[num];       ... Read More

Euler’s Totient function for all numbers smaller than or equal to n in java

Chandu yadav
Updated on 25-Jun-2020 12:51:01

743 Views

Following is a program to get the result of Euler’s Totient function for all numbers smaller than or equal to n when n is given.Programimport java.util.Scanner; public class EulerTotient {    public static int gcd(int a,int b){       int i, hcf = 0;          for(i = 1; i

Euler’s criterion in java

Arjun Thakur
Updated on 25-Jun-2020 12:50:12

341 Views

According to Euler’s criterion a square root of n under modulo p exists if and only if a number num exists such that num%p is equal to n%p.Programimport java.util.Scanner; public class EulersCriterion {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter n value :");             int n = sc.nextInt();       System.out.println("Enter p value :");       int p = sc.nextInt();       n = n % p;       int flag = 0;            for ... Read More

Divisors of factorials of a number in java

Chandu yadav
Updated on 25-Jun-2020 12:47:39

140 Views

Following is a Java program to find the divisors of factorials of a number.Programimport java.util.Scanner; public class DivisorsOfFactorial {    public static long fact(int i) {       if(i

Legendre’s Formula in java

George John
Updated on 25-Jun-2020 12:46:49

167 Views

You can calculate the exponent of the largest power of a PrimeNumber that divides the factorial n! using Legendre's formula.Programimport java.util.Scanner; public class LegendresFormula {    static int Largestpower(int n, int p) {       int ans = 0;       while (n > 0) {          n /= p;          ans += n;       }       return ans;    }    public static void main (String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the n value :");     ... Read More

Previous 1 ... 7 8 9 10 11 ... 28 Next
Advertisements