Found 34494 Articles for Programming

Compare two Strings lexicographically in Java programming

George John
Updated on 26-Jun-2020 07:30:58

274 Views

We can compare two strings lexicographically using following ways in Java.Using String.compareTo(String) method. It compares in case sensitive manner.Using String.compareToIgnoreCase(String) method. It compares in case insensitive manner.Using String.compareTo(Object) method. It compares in case sensitive manner.These methods returns the ascii difference of first odd characters of compared strings.Example Live Demopublic class Tester {    public static void main(String args[]) {       String str = "Hello World";       String anotherString = "hello world";       Object objStr = str;       System.out.println( str.compareTo(anotherString) );       System.out.println( str.compareToIgnoreCase(anotherString) );       System.out.println( str.compareTo(objStr.toString()));    } }Output-32 0 0

overriding method different package in java

Chandu yadav
Updated on 26-Jun-2020 07:32:47

1K+ Views

TestedThe benefit of overriding is ability to define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.In object-oriented terms, overriding means to override the functionality of an existing method.Example Live Democlass Animal {    public void move() {       System.out.println("Animals can move");    } } class Dog extends Animal {    public void move() {       System.out.println("Dogs can walk and run");    } } public class TestDog {    public static void main(String args[]) {       Animal a = new Animal(); // ... Read More

Overloading in java programming

Arjun Thakur
Updated on 26-Jun-2020 07:35:00

228 Views

Method overloading is a type of static polymorphism. In Method overloading, we can define multiple methods with the same name but with different parameters. Consider the following example program.Example Live Demopublic class Tester {    public static void main(String args[]) {       Tester tester = new Tester();       System.out.println(tester.add(1, 2));       System.out.println(tester.add(1, 2, 3));    }    public int add(int a, int b) {       return a + b;    }    public int add(int a, int b, int c) {       return a + b + c;    } }Output3 ... Read More

Object Serialization with inheritance in Java

George John
Updated on 25-Jun-2020 14:27:13

2K+ Views

In Serialization when inheritance is introduced then on the basis of superclass and subclass certain cases have been defined which make the understanding of Serialization in each case much simpler. The fundamental rules which should be followed are as below.1. When super class is implements Serializable interface and subclass is not.In this case the object of subclass get serialized by default when superclass get serialize, even if subclass doesn't implements Serializable interface.Examplepublic class TestSerialization {    public static void main(String[] args) throws IOException, ClassNotFoundException {       B obj = new B();       FileOutputStream fos = new ... Read More

Java Object Creation of Inherited Class

Chandu yadav
Updated on 25-Jun-2020 14:27:41

2K+ Views

In java constructor is something which is responsible for object creation of a particular class.Along with other functions of constructor it also instantiate the properties/instances of its class.In java by default super() method is used as first line of constructor of every class, here the purpose of this method is to invoke constructor of its parent class so that the properties of its parent get well instantiated before subclass inherits them and use.The point that should remember here is when you create a object the constructor is called but it is not mandatory that whenever you called a constructor of ... Read More

Multithreading in Java

Arjun Thakur
Updated on 25-Jun-2020 14:32:18

437 Views

Java is a multi-threaded programming language which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.By definition, multitasking is when multiple processes share common processing resources such as a CPU. Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing ... Read More

Moving a file from one directory to another using Java

George John
Updated on 25-Jun-2020 14:32:50

5K+ Views

We can use Files.move() API to move file from one directory to another. Following is the syntax of the move method.public static Path move(Path source, Path target, CopyOption... options) throws IOExceptionWheresource − Source path of file to be movedtarget − Target path of file to be movedoptions − options like REPLACE_EXISTING, ATOMIC_MOVEExampleimport java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Tester {    public static void main(String[] args) {       //move file from D:/temp/test.txt to D:/temp1/test.txt       //make sure that temp1 folder exists       moveFile("D:/temp/test.txt", "D:/temp1/test.txt");    }    private static void moveFile(String ... Read More

Local inner class in Java

Arjun Thakur
Updated on 25-Jun-2020 14:38:35

2K+ Views

In Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class.SyntaxFollowing is the syntax to write a nested class. Here, the class Outer_Demo is the outer class and the class Inner_Demo is the nested class.class Outer_Demo {    class Inner_Demo {    } }Nested classes are divided into two types.Non-static nested classes − These are the non-static members of a class.Static nested classes ... Read More

Literals in Java programming

George John
Updated on 30-Jul-2019 22:30:23

299 Views

A literal is a source code representation of a fixed value. They are represented directly in the code without any computation. Literals can be assigned to any primitive type variable. For example. byte a = 68; char a = 'A'; byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number systems as well. Prefix 0 is used to indicate octal, and prefix 0x indicates hexadecimal when using these number systems for literals. For example − int decimal = 100; int octal = 0144; int hexa = 0x64; String ... Read More

LinkedList in Java

Chandu yadav
Updated on 25-Jun-2020 14:40:33

345 Views

The LinkedList class extends AbstractSequentialList and implements the List interface. It provides a linked-list data structure.Following are the constructors supported by the LinkedList class.Sr.No.Constructor & Description1LinkedList( )This constructor builds an empty linked list.2LinkedList(Collection c)This constructor builds a linked list that is initialized with the elements of the collection c.Apart from the methods inherited from its parent classes, LinkedList defines following methods.Sr.No.Method & Description1void add(int index, Object element)Inserts the specified element at the specified position index in this list. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index > size()).2boolean add(Object o)Appends the specified element ... Read More

Advertisements