Programming Articles

Page 1580 of 2547

How to get last 2 characters from string in C# using Regex?

Sreemaha
Sreemaha
Updated on 11-Mar-2026 1K+ Views

Set the string −string str = "Cookie and Session";Use the following Regex to get the last 2 characters from string −Regex.Match(str,@"(.{2})\s*$")The following is the code −Exampleusing System; using System.Text.RegularExpressions; public class Demo {    public static void Main() {       string str = "Cookie and Session";       Console.WriteLine(Regex.Match(str,@"(.{2})\s*$"));    } }Outputon

Read More

Addition and Concatenation in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

'+' operator in java can be used to add numbers and concatenate strings. Following rules should be considered.Only numbers as operands then result will be a number.Only strings as operands then result will be a concatenated string.If both numbers and strings as operands, then numbers coming before string will be treated as numbers.If both numbers and strings as operands, then numbers coming after string will be treated as a string.Above rule can be overridden using brackets().ExampleCreate a java class named Tester.Tester.javapublic class Tester {    public static void main(String args[]) {             //Scenario 1: Only ...

Read More

Should a constructor always have the same name as the class in java?

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 3K+ Views

Yes, the constructor should always have the same name as the class. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. Mostly it is used to instantiate the instance variables of a class. If the programmer doesn’t write a constructor the compiler writes a constructors on his behalf. Example public class Sample{ public Sample(){ System.out.println("This is a constructor"); } public static void main(String args[]){ Sample obj = new Sample(); } } Output This is a constructor

Read More

What are final, abstract, synchronized non-access modifiers in Java?

Jai Janardhan
Jai Janardhan
Updated on 11-Mar-2026 530 Views

The abstract keyword is used to declare methods abstract methods and abstract classes. Once a method is declared abstract we should not specify body for those. And once a class is declared abstract it cannot be instantiated. Example abstract class Employee { private String name; private String address; private int number; public Employee(String name, String address, int number) { System.out.println("Constructing an Employee"); this.name = name; ...

Read More

How to Initialize and Compare Strings in Java?

George John
George John
Updated on 11-Mar-2026 265 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

What does synchronized modifier do in Java?

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 1K+ Views

The synchronized keyword used to indicate that a method can be accessed by only one thread at a time. The synchronized modifier can be applied with any of the four access level modifiers. Example public class TestThread { public static Object Lock1 = new Object(); public static Object Lock2 = new Object(); public static void main(String args[]) { ThreadDemo1 T1 = new ThreadDemo1(); ThreadDemo2 T2 = new ThreadDemo2(); ...

Read More

What is the class "class" in Java?

seetha
seetha
Updated on 11-Mar-2026 616 Views

The Java.lang.Class class instance represent classes and interfaces in a running Java application. It has no public constructor. Example Following is the example demonstrates the usage of the class Class. The java.lang.Class.getCanonicalName() method returns the canonical name of the underlying class as defined by the Java Language Specification. It returns null if the class does not have a canonical name. import java.lang.*; public class ClassDemo { public static void main(String[] args) { ClassDemo c = new ClassDemo(); Class cls = c.getClass(); ...

Read More

How to convert a Java String to an Int?

Alankritha Ammu
Alankritha Ammu
Updated on 11-Mar-2026 426 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

What is the difference between super and this, keywords in Java?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 11-Mar-2026 766 Views

The this is a keyword in Java which is used as a reference to the object of the current class. Using it you can − Differentiate the instance variables from local variables if they have same names, within a constructor or a method. Call one type of constructor (parametrized constructor or default) from other in a class. It is known as explicit constructor invocation. Example class Superclass { int age; Superclass(int age) { this.age = age; } public void ...

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
Showing 15791–15800 of 25,466 articles
Advertisements