Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Maruthi Krishna
Page 40 of 50
How to Get a slice of a primitive array in Java?
You can get a part of a Java array in between two specified indexes in various ways.By Copying contents:One way to do so is to create an empty array and copy the contents of the original array from the start index to the endIndex.Example Live Demoimport java.util.Arrays; public class SlicingAnArray { public static int[] sliceArray(int array[], int startIndex, int endIndex ){ int size = endIndex-startIndex; int part[] = new int[size]; //Copying the contents of the array for(int i=0; iarray[i]); part = stream.toArray(); //Copying the contents of the array for(int i=0; i
Read MoreDifference between the list() and listFiles() methods in Java
The class named File of the java.io package represents a file or directory (path names) in the system. To get the list of all the existing files in a directory this class provides the list() and ListFiles() methods.The main difference between them is thatThe list() method returns the names of all files in the given directory in the form of a String array.The ListFiles() method returns the objects (File) of the files in the given directory, in the form of an array of type File.i.e. If you just need the names of the files within a particular directory you can ...
Read MoreByte Streams in Java
These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits. Using these you can store characters, videos, audios, images etc.The InputStream and OutputStream classes (abstract) are the super classes of all the input/output stream classes: classes that are used to read/write a stream of bytes. Following are the byte array stream classes provided by Java −InputStreamOutputStreamFIleInputStreamFileOutputStreamByteArrayInputStreamByteArrayOutputStreamObjectInputStreamObjectOutputStreamPipedInputStreamPipedOutputStreamFilteredInputStreamFilteredOutputStreamBufferedInputStreamBufferedOutputStreamDataInputStreamDataOutputStreamExampleFollowing Java program reads data from a particular file using FileInputStream and writes it to another, using FileOutputStream.import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IOStreamsExample { public static void main(String args[]) throws IOException { ...
Read MoreWhat is loose coupling how do we achieve it using Java?
Coupling refers to the dependency of one object type on another, if two objects are completely independent of each other and the changes done in one doesn’t affect the other both are said to be loosely coupled.You can achieve loose coupling in Java using interfaces -Example Live Demointerface Animal { void child(); } class Cat implements Animal { public void child() { System.out.println("kitten"); } } class Dog implements Animal { public void child() { System.out.println("puppy"); } } public class LooseCoupling { public static void main(String args[]) { Animal obj = new Cat(); obj.child(); } }Outputkitten
Read MoreWhile overriding can the subclass choose not to throw an exception in java?
If the super-class method throws certain exception, you can override it without throwing any exception.ExampleIn the following example the sampleMethod() method of the super-class throws FileNotFoundException exception and, the sampleMethod() method does not throw any exception at all. Still this program gets compiled and executed without any errors. Live Demoimport java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super { public void sampleMethod()throws FileNotFoundException { System.out.println("Method of superclass"); } } public class ExceptionsExample extends Super { public void sampleMethod() { System.out.println("Method of Subclass"); } public static void main(String args[]) ...
Read MoreCan we initialize static variables in a default constructor in Java?
Class/static variables belong to a class, just like instance variables they are declared within a class, outside any method, but, with the static keyword.They are available to access at the compile time, you can access them before/without instantiating the class, there is only one copy of the static field available throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword.If you declare a static variable in a class, if you haven’t initialized it, just like with instance variables compiler initializes these with default values ...
Read MoreHow to make elements of array immutable in Java?
No, you cannot make the elements of an array immutable.But the unmodifiableList() method of the java.util.Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object. The user has only read-only access to the obtained list.And the asList() method of the ArrayList class accepts an array and returns a List object.Therefore, to convert an array immutable −Obtain the desired array.Convert it into a list object using the asList() method.Pass the obtained list as a parameter to the unmodifiableList() method.Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class UnmodifiableExample ...
Read MoreCan a final keyword alone be used to define a constant in Java?
A constant variable is the one whose value is fixed and only one copy of it exists in the program. Once you declare a constant variable and assign value to it, you cannot change its value again throughout the program.Unlike other languages java does not support constants directly. But, you can still create a constant by declaring a variable static and final.Static − Once you declare a variable static they will be loaded into the memory at the compile time i.e. only one copy of them is available.Final − once you declare a variable final you cannot modify its value ...
Read MoreWhy Java wouldn't allow initialization of static final variable in a constructor?
If you declare a variable static and final you need to initialize it either at declaration or, in the static block. If you try to initialize it in the constructor, the compiler assumes that you are trying to reassign value to it and generates a compile time error −Example Live Democlass Data { static final int num; Data(int i) { num = i; } } public class ConstantsExample { public static void main(String args[]) { System.out.println("value of the constant: "+Data.num); } }Compile time errorConstantsExample.java:4: error: cannot assign a value to ...
Read MoreWhy should a blank final variable be explicitly initialized in all Java constructors?
A final variable which is left without initialization is known as blank final variable.Generally, we initialize instance variables in the constructor. If we miss out they will be initialized by the constructors by default values. But, the final blank variables will not be initialized with default values. So if you try to use a blank final variable without initializing in the constructor, a compile time error will be generated.Example Live Demopublic class Student { public final String name; public void display() { System.out.println("Name of the Student: "+this.name); } public static void main(String args[]) { ...
Read More