Found 338 Articles for Java Programming

Match all occurrences of a regex in Java

Arnab Chakraborty
Updated on 23-Jun-2020 14:46:06

164 Views

public class RegexOccur {    public static void main(String args[]) {       String str = "java is fun so learn java";       String findStr = "java";       int lastIndex = 0;       int count = 0;       while(lastIndex != -1) {          lastIndex = str.indexOf(findStr,lastIndex);          if(lastIndex != -1) {             count ++;             lastIndex += findStr.length();          }       }       System.out.println(count);    } }Output2

Java regex to exclude a specific String constant

Arnab Chakraborty
Updated on 24-Jun-2020 07:26:33

980 Views

regex ^((?!kk).)*$ returns true if a line does not contain kk, otherwise returns falseExamplepublic class RegTest {    public static void main(String[] args) {       // TODO Auto-generated method stub       String s="tutorials";       boolean i=s.matches("^((?!kk).)*$");       System.out.println(i);    } }

How to test if a Java String contains a case insensitive regex pattern

Arnab Chakraborty
Updated on 24-Jun-2020 07:29:15

221 Views

the syntax? i:x makes the string search case-insensitive. for egpublic class RegCaseSense {    public static void main(String[] args) {       String stringSearch = "HI we are at java class.";       // this won't work because the pattern is in upper-case       System.out.println("Try this 1: " + stringSearch.matches(".*CLASS.*"));         // the magic (?i:X) syntax makes this search case-insensitive, so it returns true       System.out.println("Try this 2: " + stringSearch.matches("(?i:.*CLASS.*)"));    } }

Nested interface in Java

Nancy Den
Updated on 17-Jun-2020 07:36:17

3K+ Views

We can declare an interface in another interface or class. Such an interface is termed as a nested interface.The following are the rules governing a nested interface.A nested interface declared within an interface must be public.A nested interface declared within a class can have any access modifier.A nested interface is by default static.Following is an example of a nested interface.ExampleLive Democlass Animal {    interface Activity {       void move();    } } class Dog implements Animal.Activity {    public void move() {       System.out.println("Dogs can walk and run");    } } public class Tester { ... Read More

Interface enhancements in Java 8

varun
Updated on 30-Jul-2019 22:30:21

391 Views

Java 8 introduces a new concept of default method implementation in interfaces. This capability is added for backward compatibility so that old interfaces can be used to leverage the lambda expression capability of Java 8.For example, ‘List’ or ‘Collection’ interfaces do not have ‘forEach’ method declaration. Thus, adding such method will simply break the collection framework implementations. Java 8 introduces default method so that List/Collection interface can have a default implementation of forEach method, and the class implementing these interfaces need not implement the same. An interface can also have static helper methods from Java 8 onwards

Abstraction vs Encapsulation in Java

radhakrishna
Updated on 17-Jun-2020 07:32:57

807 Views

EncapsulationEncapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.Encapsulation in Java is a mechanism for wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.To achieve encapsulation in Java −Declare the variables of a class as private.Provide public setter and getter methods to modify and view the variables values.AbstractionAbstraction is the quality of ... Read More

Downcasting in Java

Ramu Prasad
Updated on 17-Jun-2020 07:32:16

221 Views

Yes, a variable can be downcast to its lower range substitute by casting. It may lead to data loss although. See the example below −Example Live Demopublic class Tester {    public static void main(String[] args) {       int a = 300;       byte b = (byte)a;       System.out.println(b);    } }OutputIt will print output as44

Runtime Polymorphism in Java

Priya Pallavi
Updated on 17-Jun-2020 07:28:06

14K+ Views

Method overriding is an example of runtime polymorphism. In method overriding, a subclass overrides a method with the same signature as that of in its superclass. During compile time, the check is made on the reference type. However, in the runtime, JVM figures out the object type and would run the method that belongs to that particular object.ExampleSee the example below to understand the concept − 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 ... Read More

Can we inherit a final method in Java?

Johar Ali
Updated on 30-Jul-2019 22:30:21

500 Views

Yes, a final method is inherited but cannot be overridden.

Can we initialize blank final variable in Java

Amit Sharma
Updated on 30-Jul-2019 22:30:21

1K+ Views

Yes! You can initialize a blank final variable in constructor or instance initialization block.

Advertisements