Found 272 Articles for Java8

Generating password in Java

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

11K+ 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

Garbage collection in Java

Fendadis John
Updated on 21-Jun-2020 15:04:53

643 Views

Java Garbage collector tracks the live object and objects which are no more need are marked for garbage collection. It relieves developers to think of memory allocation/deallocation issues.JVM uses the heap, for dynamic allocation. In most of the cases, the operating systems allocate the heap in advance which is then to be managed by the JVM while the program is running. It helps in following ways −Faster object creation as Operating system level synchronization is no more needed for each object. Object Allocation takes some memory and increases the offset.When an object is not required, garbage collector reuses the object's ... Read More

Formatted Output in Java

Fendadis John
Updated on 21-Jun-2020 15:10:18

2K+ Views

String provides format() method can be used to print formatted output in java.System.out.printf() method can be used to print formatted output in java.Following example returns a formatted string value by using a specific locale, format and arguments in format() methodExampleimport java.util.*; public class StringFormat {    public static void main(String[] args) {       double e = Math.E;       System.out.format("%f%n", e);       System.out.format(Locale.GERMANY, "%-10.4f%n%n", e);    } }OutputThe above code sample will produce the following result.2.718282 2, 7183The following is an another sample example of format strings.Examplepublic class HelloWorld {    public static ... Read More

Flexible nature of java.lang.object

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

86 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

353 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

591 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

5K+ 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

final local variable in Java

Vikyath Ram
Updated on 21-Jun-2020 14:41:03

2K+ Views

Local VariableLocal variables are declared in methods, constructors, or blocks.Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.Access modifiers cannot be used for local variables.Local variables are visible only within the declared method, constructor, or block.Local variables are implemented at stack level internally.There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.final Local Variablefinal is the only allowed access modifier for local variables.final local variable is not required ... Read More

final, finally and finalize in Java

Fendadis John
Updated on 29-Jul-2021 14:01:06

11K+ Views

The final keyword can be used with class method and variable. A final class cannot be inherited, a final method cannot be overridden and a final variable cannot be reassigned.The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred. Using a finally block allows you to run any cleanup-type statements that you just wish to execute, despite what happens within the protected code.The finalize() method is used just before object is destroyed and can be called just prior to object ... Read More

Final Arrays in Java

Rishi Raj
Updated on 21-Jun-2020 14:07:08

1K+ Views

A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object.However, the data within the object can be changed. So, the state of the object can be changed but not the reference. As an array is also an object and it is referred by a reference variable which if set as final then cannot be reassigned. Let's see the examples for further explanation.Examplepublic class Tester {    public static void main(String []args) {           final int[] arr = {1, 2, 3};   ... Read More

Advertisements