Found 346 Articles for Java Programming

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

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

355 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

597 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

File Handling in Java using FileReader and FileWriter

Rishi Raj
Updated on 21-Jun-2020 14:23:52

1K+ Views

Java Byte streams are used to perform input and output of 8-bit bytes, whereas Java Character streams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are, FileReader and FileWriter. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here the major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.Following example, which makes the use of these two classes to copy an input file (having unicode characters) into an output file −Exampleimport java.io.*; ... Read More

Factory method to create Immutable Set in Java SE 9

Paul Richard
Updated on 21-Jun-2020 13:58:41

72 Views

With Java 9, new factory methods are added to Set interface to create immutable instances. These factory methods are convenience factory methods to create a collection in less verbose and in concise way.Old way to create collectionsExampleimport java.util.Collections; import java.util.HashSet; import java.util.Set; public class Tester {    public static void main(String []args) {       Set set = new HashSet();       set.add("A");       set.add("B");       set.add("C");       Set readOnlySet = Collections.unmodifiableSet(set);       System.out.println(readOnlySet);       try {          readOnlySet.remove(0);       ... Read More

Factory method to create Immutable Map in Java SE 9

Vikyath Ram
Updated on 21-Jun-2020 14:01:01

121 Views

With Java 9, new factory methods are added to Map interface to create immutable instances. These factory methods are convenience factory methods to create a collection in less verbose and in concise way.Old way to create collectionsExampleimport java.util.Collections; import java.util.HashMap; import java.util.Map; public class Tester {    public static void main(String []args) {       Map map = new HashMap();       map.put("A", "Apple");       map.put("B", "Boy");       map.put("C", "Cat");       Map readOnlyMap = Collections.unmodifiableMap(map);       System.out.println(readOnlyMap);       try {          readOnlyMap.remove(0); ... Read More

Factory method to create Immutable List in Java SE 9

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

148 Views

With Java 9, new factory methods are added to List interface to create immutable instances. These factory methods are convenience factory methods to create a collection in less verbose and in concise way.Old way to create collectionsExampleimport java.util.ArrayList; import java.util.Collections; import java.util.List; public class Tester {    public static void main(String []args) {       List list = new ArrayList();       list.add("A");       list.add("B");       list.add("C");       List readOnlylist = Collections.unmodifiableList(list);       System.out.println(readOnlylist);       try {          readOnlylist.remove(0);       ... Read More

Previous 1 ... 5 6 7 8 9 ... 35 Next
Advertisements