Found 9150 Articles for Object Oriented Programming

Generating random numbers in Java

Vikyath Ram
Updated on 21-Jun-2020 15:16:08

1K+ Views

We can generate random numbers using three ways in Java.Using java.util.Random class − Object of Random class can be used to generate random numbers using nextInt(), nextDouble() etc. methods.Using java.lang.Math class − Math.random() methods returns a random double whenever invoked.Using java.util.concurrent.ThreadLocalRandom class − ThreadLocalRandom.current().nextInt() method and similar othjer methods return a random numbers whenever invoked.Exampleimport java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Tester {    public static void main(String[] args) {       generateUsingRandom();       generateUsingMathRandom();       generateUsingThreadLocalRandom();    }    private static void generateUsingRandom() {       Random random = new Random(); ... Read More

Generating password in Java

Rishi Raj
Updated on 21-Jun-2020 15:20:45

13K+ Views

Generate temporary password is now a requirement on almost every website now-a-days. In case a user forgets the password, system generates a random password adhering to password policy of the company. Following example generates a random password adhering to following conditions −It should contain at least one capital case letter.It should contain at least one lower-case letter.It should contain at least one number.Length should be 8 characters.It should contain one of the following special characters: @, $, #, !.Exampleimport java.util.Random; public class Tester{    public static void main(String[] args) {       System.out.println(generatePassword(8));    }    private ... Read More

Generating OTP in Java

Fendadis John
Updated on 21-Jun-2020 15:00:56

5K+ Views

Generate OTP is now a requirement on most of the website now-a-days. In case of additional authentication, system generates a OTP password adhering to OTP policy of the company. Following example generates a unique OTP adhering to following conditions −It should contain at least one number.Length should be 4 characters.Exampleimport java.util.Random; public class Tester {    public static void main(String[] args) {       System.out.println(generateOTP(4));    }    private static char[] generateOTP(int length) {       String numbers = "1234567890";       Random random = new Random();       char[] otp = new ... Read More

Formatted Output in Java

Revathi Satya Kondra
Updated on 26-Dec-2024 20:45:17

3K+ Views

In Java, the formatted output refers to a process of generating well-structured data by specifying how data should be displayed. It involves using format specifiers to control the appearance (visibility) of various data types, such as integers, floating-point numbers, strings, characters, and dates. In this article, we will discuss two key concepts about formatted output in Java as follows: Format Specifiers: Strings that define how some kinds of data should be formatted. Flags and Width: Options to control the run of the output, including aligning it, setting its padding, and regulating its width. Common Format Specifiers Format specifiers in Java ... Read More

Flow control in a try catch finally in Java

Vikyath Ram
Updated on 21-Jun-2020 14:57:44

7K+ Views

A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following −Syntaxtry {    // Protected code } catch (ExceptionName e1) {    // Catch block }The code which is prone to exceptions is placed in the try block. When an exception occurs, that exception occurred is handled by catch block associated with it. Every try block should be immediately followed either by ... Read More

Floating point operators and associativity in Java

Fendadis John
Updated on 21-Jun-2020 14:24:45

568 Views

Following programs shows the float arithmetic can cause dubious result if integer values are used using float variables.Examplepublic class Tester {    public static void main(String[] args) {       float a = 500000000;       float b = -500000000;       float c = 1;       float sumabc1 = a+(b+c);       float sumabc2 =(a+b)+c;       System.out.println("Floating Point Arithmetic");       System.out.println("a + ( b + c ) : " + sumabc1);       System.out.println("(a + b) + c : " + sumabc2);       ... Read More

Flexible nature of java.lang.object

Fendadis John
Updated on 21-Jun-2020 14:35:44

186 Views

The java.lang.Object class is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.Class DeclarationFollowing is the declaration for java.lang.Object class −public class ObjectClass constructorsSr.No.Constructor & Description1Object()This is the Single Constructor.Class methodsSr.No.Method & Description1protected Object clone()This method creates and returns a copy of this object.2boolean equals(Object obj)This method indicates whether some other object is "equal to" this one.3protected void finalize()This method is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.4Class getClass()This method returns the ... Read More

Find max and min values in an array of primitives using Java

Arushi
Updated on 21-Jun-2020 14:36:33

532 Views

This example shows how to search the minimum and maximum element in an array by using Collection.max() and Collection.min() methods of Collection class.Exampleimport java.util.Arrays; import java.util.Collections; public class Main {    public static void main(String[] args) {       Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};       int min = (int) Collections.min(Arrays.asList(numbers));       int max = (int) Collections.max(Arrays.asList(numbers));       System.out.println("Min number: " + min);       System.out.println("Max number: " + max);    } }ResultThe above code sample will produce the following result.Min number: 1 Max ... Read More

Find free disk space using Java

Vikyath Ram
Updated on 21-Jun-2020 14:37:34

940 Views

java.io.File class provides following useful methods to figure out the free disk space available.Sr.No.Method & Description1public long getFreeSpace()Returns the number of unallocated bytes in the partition named by this abstract path name.2public long getTotalSpace()Returns the size of the partition named by this abstract pathname.3public long getUsableSpace()Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname.Following example showcases the use of above methods.Example Finalimport java.io.File; import java.text.NumberFormat; public class Tester {    public static void main(String[] args) {       NumberFormat numberFormat = NumberFormat.getInstance();       numberFormat.setMaximumFractionDigits(2);     ... Read More

Final static variables in Java

Vikyath Ram
Updated on 21-Jun-2020 14:39:59

6K+ Views

Final Static VariablesClass variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.There would only be one copy of each class variable per class, regardless of how many objects are created from it.Static variables are normally declared as constants using the final keyword. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value.Static variables are stored in the static memory, mostly declared as final and used as either public or private constants.Static variables are created when the program ... Read More

Advertisements