Java Articles

Page 119 of 450

Single level inheritance in Java

Ramu Prasad
Ramu Prasad
Updated on 11-Mar-2026 15K+ Views

Single Level inheritance - A class inherits properties from a single class. For example, Class B inherits Class A.Exampleclass Shape {    public void display() {       System.out.println("Inside display");    } } class Rectangle extends Shape {    public void area() {       System.out.println("Inside area");    } } public class Tester {    public static void main(String[] arguments) {       Rectangle rect = new Rectangle();       rect.display();       rect.area();    } }OutputInside display Inside areaHere Rectangle class inherits Shape class and can execute two methods, display() and area() as shown.

Read More

Runtime Polymorphism in Java

Priya Pallavi
Priya Pallavi
Updated on 11-Mar-2026 19K+ 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 −class 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"); ...

Read More

Runtime Polymorphism in Java

Priya Pallavi
Priya Pallavi
Updated on 11-Mar-2026 19K+ 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 −class 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"); ...

Read More

Runtime Polymorphism in Java

Priya Pallavi
Priya Pallavi
Updated on 11-Mar-2026 19K+ 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 −class 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"); ...

Read More

How to match a line not containing a word in Java Regex

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 534 Views

Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class NoRegTest {    public static void main(String[] args) {       String s="^fun";       Pattern pattern = Pattern.compile(s);       Matcher matcher = pattern.matcher("Java is fun");       if(!matcher.find()) {          System.out.println("not found");       }    } }Outputnot found

Read More

How to Initialize and Compare Strings in Java?

George John
George John
Updated on 11-Mar-2026 255 Views

You can compare Strings using compareTo() method or, equals() method or, or == operator. Following example demonstrates how to initialize and compare strings in Java. Example public class StringDemo { public static void main(String[] args) { String str1 = "tutorials"; String str2 = "point"; // comparing str1 and str2 int retval = str1.compareTo(str2); // prints the return value of the comparison if (retval < 0) { System.out.println("str1 is greater than str2"); } else if (retval == 0) { System.out.println("str1 is equal to str2"); } else { System.out.println("str1 is less than str2"); } } } Output str1 is less than str2

Read More

How to convert a Java String to an Int?

Alankritha Ammu
Alankritha Ammu
Updated on 11-Mar-2026 416 Views

The parseXxx() method is used to get the primitive data type of a certain String. In this case to convert a String to integer you could use the parseInt() method.Examplepublic class Test {    public static void main(String args[]) {       int x =Integer.parseInt("9");       double c = Double.parseDouble("5");       int b = Integer.parseInt("444",16);       System.out.println(x);       System.out.println(c);       System.out.println(b);    } }Output9 5.0 1092

Read More

Write a Java program to capitalize each word in the string?

Akshaya Akki
Akshaya Akki
Updated on 11-Mar-2026 2K+ Views

You can capitalize words in a string using the toUpperCase() method of the String class. This method converts the contents of the current string to Upper case letters.Examplepublic class StringToUpperCaseEmp {    public static void main(String[] args) {       String str = "string abc touppercase ";       String strUpper = str.toUpperCase();       System.out.println("Original String: " + str);       System.out.println("String changed to upper case: " + strUpper);    } }OutputOriginal String: string abc touppercase String changed to upper case: STRING ABC TOUPPERCASE

Read More

Java Program to check whether one String is a rotation of another.

Ayyan
Ayyan
Updated on 11-Mar-2026 613 Views

To find weather a string is rotation of another, concat the first string to itself twice and find weatherExamplepublic class Sample {    public static void main(String args[]){       String str1 = "gala";       String str2 = "alag";       String s3 = str1+str1;       if(s3.contains(str2)) {          System.out.println("str1 is rotation of str2");       } else {          System.out.println("str1 is not rotation of str2");       }    } }

Read More

Write a java program reverse tOGGLE each word in the string?

Manikanth Mani
Manikanth Mani
Updated on 11-Mar-2026 901 Views

To perform a reverse toggle split the words of a string, reverse each word using the split() method, change the first letter of each word to lower case and remaining letters to upper case.Exampleimport java.lang.StringBuffer; public class ToggleReverse {    public static void main(String args[]){       String sample = "Hello How are you";       String[] words = sample.split(" ");       String result = "";       for(String word:words){          StringBuffer s = new StringBuffer(word);          word = s.reverse().toString();          String firstSub = word.substring(0, 1);          String secondSub = word.substring(1);          result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";       }       System.out.println(result);    } }OutputoLLEH wOH eRA uOY

Read More
Showing 1181–1190 of 4,498 articles
« Prev 1 117 118 119 120 121 450 Next »
Advertisements