Generics in C#

Chandu yadav
Updated on 21-Jun-2020 14:43:04

273 Views

Generics allow you to write a class or method that can work with any data type.Write the specifications for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type. Generics is a technique that enriches your programs in the following ways −It helps you to maximize code reuse, type safety, and performance.You can create generic collection classes. The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. You may use these generic ... 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 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

Find free disk space using Java

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

596 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

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

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

354 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

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

File Handling in C#

George John
Updated on 21-Jun-2020 14:34:14

516 Views

A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.In C#, you need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows −FileStream = new FileStream( , , , );Here, the file operations are also included as shown below −The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are −Append − It opens an existing ... Read More

Find all substrings in a string using C#

karthikeya Boyini
Updated on 21-Jun-2020 14:25:18

330 Views

Use the substring() method in C# to find all substrings in a string.Let’s say our string is −pqrLoop through the length of the string and use the Substring function from the beginning to the end of the string −for (int start = 0; start

Floating point operators and associativity in Java

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

362 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

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

Advertisements