All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

String Methods In Java With Examples

Last Updated : Mar 11, 2024

String Methods In Java With Examples

In this article we will show you the solution of string methods in java with examples, here you have to import packages, in case you using eclipse then each program have itself.

After public class definition you can easily define any type of string methods under ‘public static void main()’.

To use predefined functions in java has some rules that allow you to get the results. Below shown example will guide to achieve expecting result by your own.

Step By Step Guide On String Methods In Java With Examples :-

Package is must, otherwise they cannot understand the code what you defined.

The class and void main definition are common and necessary thing actually for each program. Here shown example explains you about concat()- concatenation and equals()-comparison methods uses.

And how to approach them to get right result. Below given only sample you can modify as per your wish and you get according result in terminal.

package java_program;
public class Sample{
    public static void main(String[] args)
    {
        String s1="Hi";
        String s2="Everyone";
        String result=s1.concat(s2);
        system.out.println("String concatenation result is = "+result);
        String cs="Hi";
        boolean result2=s1.equals(cs);
        system.out.println("The String1 = "+s1+" and String2 = "+cs+" Then Comparison Result is "+result2);
    }
}
  1. The ‘Package java_program’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
  3. Without this you can compile but not possible to run successfully, because it throws error ‘main method not found’.
  4. Firstly we declares two variables ‘s1,s2’ with texts ‘Hi, Everyone’ then the concat() method joins the s1 string with s2. If you wants to add simultaneously, make it you will get result accordingly.
  5. Then printed through ‘System.out.println()’ function with sentence ‘String concatenation result is’ and result ‘HiEveryone’.
  6. To display compare result of two string they provides equals() function that will returns the Boolean value. Here variable ‘cs’ defined with same string of s1 ‘Hi’ for disclose ‘true’ result through ‘equals()’ method.
  7. When you wants to compare two string to verify whether both are alike or not. Then follow this, in this example ‘s1’ string ‘Hi’ is compared with ‘cs’ string ‘Hi’ with the help of ‘equals()’ method.
  8. Result stored on Boolean variable ‘result2’ then printed on result with whole content text and Boolean result. Like ‘The String1 = Hi and String2 = Hi Then Comparison Result is true’, because both are same text values.
  9. The ‘system.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  10. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know string methods of concat(), equals() uses in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac Sample.java’.

After successful compilation you need to execute ‘java Sample‘ line for gets the output.

Where you can see concatenation method result ‘String concatenation result is = HiEveryone’ and comparison result message ‘The String1= Hi and String2 = Hi Then Comparison Result is true’.

The output may vary based on your string value, try to explore differently.

On eclipse just press of run button then you will get output inside terminal.

I hope this article on string methods in java with examples helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪